BlitzODE Physics Thread

Blitz3D Forums/Blitz3D Userlibs/BlitzODE Physics Thread

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here is an optimization I believe it has to be used when having many ode objects.
It shows how to use better the autodisable flags.

;Efficient use of the autodisable flags.
;For better performance change the ODE_UpdateGeoms() in the BlitzODE.bb file to this :


;Function ODE_UpdateGeoms()
;	For g.TODEGeom = Each TODEGeom
;	If ODE_dBodyIsEnabled(g\body)
;		PositionEntity g\mesh, ODE_dGeomGetPositionX(g\geom), ODE_dGeomGetPositionY(g\geom), ODE_dGeomGetPositionZ(g\geom)
;		RotateEntity g\mesh, ODE_dGeomGetPitch(g\geom), ODE_dGeomGetYaw(g\geom), ODE_dGeomGetRoll(g\geom), 1
;	EndIf
;	Next
;End Function



Include "BlitzODE.bb"

;------ Initialize Graphics --------

Graphics3D 800, 600
SetBuffer BackBuffer()

SetFont LoadFont("Arial", 14, 1, 1, 0)

PV = CreatePivot()
cam = CreateCamera(PV)
PositionEntity cam, 0, 20, -30
PointEntity cam, PV
lh = CreateLight()

plane = CreatePlane()
mirror = CreateMirror(plane)
EntityColor plane, 0, 0, 150
EntityAlpha plane, .8

SeedRnd MilliSecs()
;--------- Initialize ODE ----------

Global space = ODE_dWorldCreate()


ODE_dWorldSetAutoDisableFlag(1)
ODE_dWorldSetAutoDisableSteps(1)
ODE_dWorldSetAutoDisableLinearThreshold(0.003)
ODE_dWorldSetAutoDisableAngularThreshold(0.003)

;---- Setting contact parameters ---

ODE_dSetContactMode(dContactBounce + dContactSoftERP + dContactSoftCFM + dSlip1 + dSlip2)

;-------- Create many box ---------

For i = 1 To 150
	g.TODEGeom = New TODEGeom
	g\body = ODE_dBodyCreate()
	g\geom = ODE_dCreateBox(space, 2, 2, 2, 1)
	ODE_dGeomSetBody g\geom, g\body
	ODE_dBodySetPosition(g\body, Rnd(-15, 15), Rnd(10, 20), Rnd(-15, 15))
	ODE_dBodySetRotation(g\body, Rand(-180, 180), Rand(-180, 180), Rand(-180, 180))
	
	g\mesh = CreateCube()
	EntityColor g\mesh, 255, 255, 0
	EntityShininess g\mesh, 0.7
Next

;-----------------------------------

ODE_UpdateGeoms()

;-----------------------------------

gameFPS = 50.0
framePeriod = 1000 / gameFPS
frameTime = MilliSecs () - framePeriod

While Not KeyHit(1)
	Repeat
		frameElapsed = MilliSecs () - frameTime
	Until frameElapsed
	
	frameTicks = frameElapsed / framePeriod
	frameTween# = Float(frameElapsed Mod framePeriod) / Float(framePeriod)
	For frameLimit = 1 To frameTicks
		If frameLimit = frameTicks Then CaptureWorld
		frameTime = frameTime + framePeriod
		
		;------------ Add Force and Torque ----------

		If KeyHit(57)
			For g.TODEGeom = Each TODEGeom
				ODE_dBodyEnable g\body
				ODE_dBodyAddForce g\body, 0, Rnd(20, 50), 0
				ODE_dBodyAddTorque g\body, Rnd(-10, 10), Rnd(-10, 10), Rnd(-10, 10)
			Next
		EndIf

		;------------ Update physics world ----------		

		ODE_dWorldQuickStep 0.1

		;--------------------------------------------
		
	Next
	
	;---- Set geometry position and rotation ----

	ODE_UpdateGeoms()

	;--------------------------------------------

	RenderWorld frameTween#
	
	; FPS counter
	fr = fr + 1
	If MilliSecs() - frTime >= 1000
		fps = fr
		fr = 0
		frTime = MilliSecs()
	EndIf
	
	Color 255, 255, 255
	Text 5, 5, "FPS " + fps
	Text 5, 580, "Space - Add Force and Torque"
	
	
	ku=0:ka=0
	For g.TODEGeom = Each TODEGeom
	If ODE_dBodyIsEnabled(g\body)=0
	ka=ka+1
	EntityColor g\mesh,255,100,100
	Else
	EntityColor g\mesh, 255, 255, 0
	ku=ku+1
	EndIf
		
	Next
	
	Text 5,30,"Enabled: "+ku
	Text 5,50,"Disabled: "+ka

	Flip 0
Wend

ODE_dCloseODE()
End