Does exist a lib to simulate magnet ?

Blitz3D Forums/Blitz3D Userlibs/Does exist a lib to simulate magnet ?

Hi,
I just came up with a quick magnet demo using my Box2d Wrapper..
AppTitle "box 2d wrapper (magnet demo)"
;
; Box 2d Wrapper for Blitz3d Magnet Demo
; Copyright 2007 Mid Atlantic Engineering
; <a href="https://order.kagi.com/cgi-bin/store.cgi?storeID=6XD" target="_blank">https://order.kagi.com/cgi-bin/store.cgi?storeID=6XD</a>
;
; download:
; <a href="http://skillroll.com/b2dwrapper/box2dwrapper.zip" target="_blank">http://skillroll.com/b2dwrapper/box2dwrapper.zip</a>
;



Graphics3D 800,600,0,2
Color 34,206,54

Global bodies[10]
Global world = b2d_create_world (-1000,-1000,1000,1000,0,0,0)

Const wall_rx 		= 800
Const wall_lx 		= 0
Const wall_rl_y		= 300
Const wall_by 		= 600
Const wall_ty 		= 0
Const wall_bt_x		= 400
Const wall_w		= 400
Const wall_h		= 20

Const num_objs		= 10
Const obj_radius	= 25

Const mag_radius	= 10
Const mag_strength#	= 10
Const mag_size#		= 5

mag_x# = GraphicsWidth ()/2
mag_y# = GraphicsHeight ()/2

sd = b2d_create_box_shape_def (wall_w, wall_h)
b2d_shape_def_set_density sd,0.0
bd = b2d_create_body_def ()
b2d_add_shape bd,sd
b2d_free_shape_def sd

b2d_body_def_set_position bd, wall_bt_x, wall_by
b2d_create_body world, bd
b2d_body_def_set_position bd, wall_bt_x, wall_ty
b2d_create_body world, bd

b2d_body_def_set_rotation bd, Pi/2
b2d_body_def_set_position bd, wall_lx, wall_rl_y
b2d_create_body world, bd
b2d_body_def_set_position bd, wall_rx, wall_rl_y
b2d_create_body world, bd
b2d_free_body_def bd


sd = b2d_create_circle_shape_def (obj_radius)
b2d_shape_def_set_density sd,1.0

bd = b2d_create_body_def ()
b2d_add_shape bd,sd
b2d_free_shape_def sd
For i = 0 To num_objs-1
	b2d_body_def_set_position bd, Rand (wall_lx+200,wall_rx-200), Rand (wall_ty+100,wall_by-100)
	b = b2d_create_body (world, bd)
	b2d_set_linear_vel b,Rand(-10,10),Rand (-10,10)
	bodies[i] = b
Next 
b2d_free_body_def bd


sd = b2d_create_circle_shape_def (mag_radius)
b2d_shape_def_set_density sd, .5

bd = b2d_create_body_def ()
b2d_body_def_set_position bd, mag_x, mag_y
b2d_add_shape bd,sd
b2d_free_shape_def sd
magnet = b2d_create_body (world, bd)



While Not KeyHit (1)

	If MouseHit (1)
		mx = MouseX ()
		my = MouseY ()
		b2d_mouse_down (world,mx,my)
	End If 

	If MouseDown (1)
		mx = MouseX ()
		my = MouseY ()
		b2d_mouse_move mx,my
		mouse_pressed_flag = True
	Else If mouse_pressed_flag
		b2d_mouse_up world
		mouse_pressed_flag 	= False
	End If

	Cls

	For i=0 To num_objs-1
		mag_x   = b2d_get_body_x (magnet)
		mag_y   = b2d_get_body_y (magnet)
		x# 		= b2d_get_body_x (bodies[i])
		y# 		= b2d_get_body_y (bodies[i])
		vx#		= b2d_get_body_linear_velocity_x (bodies[i])
		vy#		= b2d_get_body_linear_velocity_y (bodies[i])
		dx# 	= x - mag_x
		dy#		= y - mag_y
		dist# 	= Sqr((x-mag_x)*(x-mag_x)+(y-mag_y)*(y-mag_y))
		ratio#  = dist/(1.5*mag_size)
		force#	= mag_strength*Exp(-0.2/ratio)/(ratio*ratio*56) * 100
		vx		= (vx - dx * force/dist)
		vy		= (vy - dy * force/dist)

		b2d_set_linear_vel bodies[i],vx,vy
	Next 

	b2d_step world, .5, 10

	DrawBodies()

	;
	; draw magnet
	;
	Color 255,0,0
	x# = b2d_get_body_x (magnet) 
	y# = b2d_get_body_y (magnet) 
	Oval x-mag_radius,y-mag_radius, mag_radius*2, mag_radius*2, 1

	Flip 
Wend 


b2d_delete_world world
world = 0

EndGraphics


Function DrawBodies()
	Color 34,206,54
	For i=0 To num_objs-1
		x# 		= b2d_get_body_x (bodies[i]) 
		y# 		= b2d_get_body_y (bodies[i]) 
		Oval x-obj_radius, y-obj_radius, obj_radius*2, obj_radius*2, 0
	Next
End Function 



Mike
http://skillroll.com/b2dwrapper/