JV-ODE Physics Wrapper

Blitz3D Forums/Blitz3D Userlibs/JV-ODE Physics Wrapper

Thanks for the support fellas, both Jedive and myself really appreciate it :)

Here is another little demo to play around with, this one shows a simple rope effect using ball joints. Notice the top sphere joint is attached to '0', which means it's a static joint and won't move from its set position.

; ###################################################################################################
; #										 JV-ODE - Rope Demo											#
; #									Code by Jim Williams (VIP3R)									#
; ###################################################################################################

AppTitle "JV-ODE - Rope Demo"

Include "ode.bb"

Graphics3D 800,600,0,2

Type ODEGeom
	Field body
	Field geom
	Field mesh
End Type

Dim Sphere(10)
Dim Joint(10)

; ###################################################################################################

; ### Setup ODE

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

dWorldSetGravity(World,0,-1,0)
dContactSetMode(dContactBounce)
dContactSetMu(48)

; ### Create Rope

For count=1 To 10
	ode.ODEGeom=New ODEGeom
	ode\body=dBodyCreate(World)
	Sphere(count)=ode\body
	dBodySetRotation(ode\body,0,0,0)
	dBodySetPosition(ode\body,-30,90-(count*8),0)
	ode\geom=dCreateSphere(Space,4)
	dGeomSetBody(ode\geom,ode\body)
	ode\mesh=CreateSphere()
	ScaleMesh ode\mesh,4,4,4
	RotateMesh ode\mesh,0,0,0
	PositionMesh ode\mesh,0,0,0
	EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
	EntityAlpha ode\mesh,1
	EntityShininess ode\mesh,0.7
	Joint(count)=dJointCreateBall(World,0)
	If count=1
		dJointAttach(Joint(count),0,Sphere(count))
		Else
		dJointAttach(Joint(count),Sphere(count-1),Sphere(count))
		End If
	dJointSetBallAnchor(Joint(count),dBodyGetPositionX(Sphere(count)),dBodyGetPositionY(Sphere(count)),dBodyGetPositionZ(Sphere(count)))
Next

; ### Create light

Global Light=CreateLight()

RotateEntity Light,45,-90,0
LightColor Light,255,255,255
AmbientLight 130,130,130

; ### Create camera

Global Camera=CreateCamera()
CameraClsColor Camera,0,0,0
CameraRange Camera,1,1000
PositionEntity Camera,0,90,-100
RotateEntity Camera,30,0,0

; ### Create plane

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

Plane=CreatePlane()

EntityAlpha Plane,0.8

PlaneTexture=CreateTexture(128,128,9)

ClsColor 0,200,80
Cls

Color 255,255,255

Rect 0,0,64,64,1
Rect 64,64,64,64,1

CopyRect 0,0,128,128,0,0,BackBuffer(),TextureBuffer(PlaneTexture)
ScaleTexture PlaneTexture,20,20
EntityTexture Plane,PlaneTexture,0,0

Mirror=CreateMirror()

; ###################################################################################################

While Not KeyHit(1)

	UpdateKeys()

	PTime=MilliSecs()

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

	UpdateGeoms()

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

	PhysicsTime#=MilliSecs()-PTime

	UpdateWorld

	RenderWorld

	Text 0,0,"JV-ODE Version "+dGetVersion()
	Text 0,15,"Physics Time:"+PhysicsTime
	Text 640,0,"Add Force To Rope"
	Text 640,15,"Using Arrow Keys"

	Flip

Wend

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

End

; ###################################################################################################

Function AddObject()

ode.ODEGeom=New ODEGeom
ode\body=dBodyCreate(World)
dBodySetRotation(ode\body,0,0,0)
dBodySetPosition(ode\body,30,80,0)
ode\geom=dCreateBox(Space,10,5,10)
dGeomSetBody(ode\geom,ode\body)
ode\mesh=CreateCube()
ScaleMesh ode\mesh,5,2.5,5
EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
EntityAlpha ode\mesh,1
EntityShininess ode\mesh,0.7

End Function

; ###################################################################################################

Function UpdateKeys()

If KeyDown(203) Then dBodyAddForce(Sphere(10),-10,0,0)
If KeyDown(205) Then dBodyAddForce(Sphere(10),10,0,0)
If KeyDown(200) Then dBodyAddForce(Sphere(10),0,0,10)
If KeyDown(208) Then dBodyAddForce(Sphere(10),0,0,-10)

End Function

; ###################################################################################################

Function UpdateGeoms()

For ode.ODEGeom=Each ODEGeom
	RotateEntity ode\mesh,dGeomGetPitch#(ode\geom),dGeomGetYaw#(ode\geom),dGeomGetRoll#(ode\geom)
	PositionEntity ode\mesh,dGeomGetPositionX#(ode\geom),dGeomGetPositionY#(ode\geom),dGeomGetPositionZ#(ode\geom)
Next

End Function

; ###################################################################################################