Spherical Gravity function for Tokamak-- need help

Blitz3D Forums/Blitz3D Beginners Area/Spherical Gravity function for Tokamak-- need help

Try this...
Const FPS=90

Graphics3D 800,600,0,2
SetBuffer BackBuffer()

camera = CreateCamera()
PositionEntity camera,0,0,-100

ENTS=100
GRAVITY#=-9.8
SPHEREMASS#=1.0	 

TOKSIM_SetRigidBodiesCount ENTS
TOKSIM_SetGeometriesCount ENTS
TOKSIM_SetOverlappedPairsCount (ENTS*(ENTS-1))/2 

TOKSIM_CreateSimulator(0,0,0)

SeedRnd MilliSecs()  

; Visualise the gravi-sphere
sphere=CreateSphere(32)
ScaleMesh sphere,50,50,50
EntityAlpha sphere,0.3
EntityColor sphere,150,150,250

Dim obj_mesh(ENTS)
Dim obj_rb(ENTS)

For i=1 To ENTS
 obj_mesh(i) = CreateSphere()
 obj_rb(i) = TOKRB_Create()
 TOKRB_AddSphere obj_rb(i),2.0
 TOKRB_SetLinearDamping obj_rb(i),0.001
 TOKRB_SetAngularDamping obj_rb(i),0.02
 TOKRB_SetMass obj_rb(i),SPHEREMASS#
 TOKRB_SetSphereInertiaTensor obj_rb(i),2.0,SPHEREMASS#  
 TOKRB_SetPosition obj_rb(i),Rnd(-5.0,5.0),Rnd(-5.0,5.0),Rnd(-5.0,5.0)
 EntityColor obj_mesh(i),Rnd(100,230),Rnd(100,230),Rnd(100,230) 
Next

Centerpivot = CreatePivot()

light=CreateLight()
PositionEntity light,255,255,-255
PointEntity light,Centerpivot

period=1000/FPS
time=MilliSecs()-period

; MainLoop
While Not KeyHit(1)

	Repeat
		elapsed=MilliSecs()-time
	Until elapsed

	ticks=elapsed/period
	tween#=Float(elapsed Mod period)/Float(period)

	For k=1 To ticks
		time=time+period
		If k=ticks Then CaptureWorld

		TOKSIM_Advance(1.5/FPS,1)

	For i=1 To ENTS

	 ;Get the directionvector
	 diffx#=0.0-TOKRB_GetX#(obj_rb(i))
	 diffy#=0.0-TOKRB_GetY#(obj_rb(i))
	 diffz#=0.0-TOKRB_GetZ#(obj_rb(i))
	 Length#=Sqr(diffx#*diffx#+diffy#*diffy#+diffz#*diffz#)	; distance from center
	 	
	 If Length#=0 Then Length#=0.001 ; To prevent divizion by zero
	
	 normx#=diffx#/Length#	; Normalize the directionvector
	 normy#=diffy#/Length#
	 normz#=diffz#/Length#
	
	 ;Prevent bodies to leave the sphere by moving them inside the sphere
	 ;and then invert their velocityvector with a little damping(0.7) to make them bounce
	 If Length#>50.0 Then
	  TOKRB_SetPosition obj_rb(i),-normx#*49.9,-normy#*49.9,-normz#*49.9
	  TOKRB_SetVelocity obj_rb(i),-TOKRB_GetVelocityX#(obj_rb(i))*0.7,-TOKRB_GetVelocityY#(obj_rb(i))*0.7,-TOKRB_GetVelocityZ#(obj_rb(i))*0.7
	 EndIf

	 ;Apply gravital force
     TOKRB_SetForce obj_rb(i),normx#*GRAVITY#*SPHEREMASS#,normy#*GRAVITY#*SPHEREMASS#,normz#*GRAVITY#*SPHEREMASS#
	
	 ;Update visible meshes
     PositionEntity obj_mesh(i),TOKRB_GetX(obj_rb(i)),TOKRB_GetY(obj_rb(i)),TOKRB_GetZ(obj_rb(i)) 
     RotateEntity obj_mesh(i),TOKRB_GetPitch(obj_rb(i)),TOKRB_GetYaw(obj_rb(i)),TOKRB_GetRoll(obj_rb(i)),False

	Next

	UpdateWorld

	Next

; Press space to invert gravity
If KeyHit(57) Then 
 Switch=1-Switch
 If Switch=0 Then GRAVITY#=-9.8
 If Switch=1 Then GRAVITY#=9.8
EndIf


	RenderWorld tween
	Text 0,10,"Press SPACE to invert gravity"
	Flip 

Wend

TOKSIM_DestroySimulator()

End