JV-ODE with Blitz3DSDK example

Blitz3D SDK Forums/Blitz3D SDK Programming/JV-ODE with Blitz3DSDK example

I've converted the "JV-ODE - Cubes Demo" BlitzMax JV-ODE demo to run with the Blitz3DSDK. The original code is by VIP3R, who I hope doesn't mind me posting this here. It's just to show people that it's pretty straightforward to get the SDK working with his wonderful ODE wrapper.

' #############################################################################
' #                            JV-ODE - Cubes Demo                            #
' #                        Code by Jim Williams (VIP3R)                       #
' #                    Devious Codeworks - Copyright © 2007                   #
' #############################################################################

SuperStrict

Framework BRL.GLMax2D ' only needed for "AppTerminate()"
Import BRL.Random
Import DevCode.JVODE
Import blitz3d.blitz3dsdk

AppTitle:String="JV-ODE - Cubes Demo"

bbBeginBlitz3D()
bbGraphics3D 800,600,0,2

Type ODEGeom
	Field body:Int
	Field geom:Int
	Field mesh:Int
	Field rgb:Int[]
	Field scale:Float[]
End Type

Global ODEGeomList:TList=CreateList()

' #############################################################################

' ### Setup ODE

Global World:Int=dWorldCreate()
Global Space:Int=dHashSpaceCreate(0)
Global ContactGroup:Int=dJointGroupCreate(0)

dWorldSetAutoDisableFlag(World,1)
dWorldSetGravity(World,0,-0.98,0)
dContactSetMode(dContactBounce)
dContactSetBounce(0.1)
dContactSetMu(48)

' ### Create light

Global Light:Int = bbCreateLight()

bbRotateEntity Light,45,-90,0
bbLightColor Light,255,255,255
bbAmbientLight 130,130,130

' ### Create camera

Global Camera:Int = bbCreateCamera()
bbCameraClsColor Camera,0,0,0
bbCameraRange Camera,1,1000
bbPositionEntity Camera,0,20,-50
bbRotateEntity Camera,30,0,0

' ### Create plane

dCreatePlane(Space,0,1,0,0)

Local plane:Int = bbCreatePlane()

bbEntityAlpha Plane,0.8

Local PlaneTexture:Int = bbCreateTexture(128,128,9)

bbClsColor 0,200,80
bbCls

bbColor 255,255,255

bbRect 0,0,64,64,1
bbRect 64,64,64,64,1

bbCopyRect 0,0,128,128,0,0,bbBackBuffer(),bbTextureBuffer(PlaneTexture)
bbScaleTexture PlaneTexture,20,20
bbEntityTexture Plane,PlaneTexture,0,0

Local Mirror:Int = bbCreateMirror()

' #############################################################################

Local PTime:Int
Local Timer:Int
Local PhysicsTime:Float

While Not AppTerminate() And Not bbKeyDown(1)

	If MilliSecs()-Timer>700
		AddObject()
		Timer=MilliSecs()
	End If

	UpdateGeoms()

	PTime=MilliSecs()

	dSpaceCollide(Space,World,ContactGroup)
	dWorldQuickStep(World,0.1)
	dJointGroupEmpty(ContactGroup)

	PhysicsTime=MilliSecs()-PTime

	bbRenderWorld

	bbText 0,0,"JV-ODE Version "+dGetVersion()
	bbText 0,15,"Physics Time:"+PhysicsTime

	bbFlip 

Wend

dJointGroupDestroy(ContactGroup)
dSpaceDestroy(Space)
dWorldDestroy(World)
dCloseODE()

End

' #############################################################################

Function AddObject()

Local xp:Float
Local zp:Float
Local ode:ODEGeom

xp=Rand(-10,10)
zp=Rand(-10,10)

ode:ODEGeom=New ODEGeom
ode.body=dBodyCreate(World)
dBodySetAxisAngle(ode.body,0,0,0,0)
dBodySetPosition(ode.body,xp,30,zp)
dBodySetAutoDisableFlag(ode.body,1)
ode.geom=dCreateBox(Space,5,5,5)
dGeomSetBody(ode.geom,ode.body)
ode.mesh=bbCreateCube()
bbScaleMesh ode.mesh,2.5,2.5,2.5
bbEntityColor ode.mesh,Rand(0,255),Rand(0,255),Rand(0,255)
ListAddLast(ODEGeomList,ode:ODEGeom)

End Function

' #############################################################################

Function UpdateGeoms()

Local ode:ODEGeom

' ### Update Geoms
For ode:ODEGeom=EachIn ODEGeomList

	If dBodyIsEnabled(ode.body)
		bbPositionEntity ode.mesh,dGeomGetPositionX(ode.geom),dGeomGetPositionY(ode.geom),dGeomGetPositionZ(ode.geom)
		bbRotateEntity ode.mesh,dGeomGetPitch#(ode.geom),dGeomGetYaw#(ode.geom),dGeomGetRoll#(ode.geom)
	EndIf

Next

End Function

' #############################################################################


I don't mind at all :)

Good work Avon.