JV-ODE Physics Thread 6

Blitz3D Forums/Blitz3D Userlibs/JV-ODE Physics Thread 6

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.

You want JV-ODE to work with 2D projects eh?

No problem :)

Here's a 2D JV-ODE Spheres demo...
; ###################################################################################################
; #									  JV-ODE - 2D Spheres Demo										#
; #									Code by Jim Williams (VIP3R)									#
; ###################################################################################################

AppTitle "JV-ODE - 2D Spheres Demo"

Include "JV-ODE.bb"

Graphics 800,600,0,2

Type ODEGeom
	Field body
	Field geom
	Field r
	Field g
	Field b
End Type

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

; ### Setup ODE

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

dWorldSetAutoDisableFlag(World,1)
dWorldSetGravity(World,0,-9.8,0)
dContactSetMode(dContactBounce)
dContactSetBounce(0.8)
dContactSetMu(48)

; ### Create plane

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

; ### ODE STATIC BARRIER (Near)

ode.ODEGeom=New ODEGeom
ode\geom=dCreateBox(Space,1000,1000,1)
dGeomSetRotation(ode\geom,0,0,0)
dGeomSetPosition(ode\geom,0,500,-15)
dGeomContactSetMu(ode\geom,0.0)

; ### ODE STATIC BARRIER (Far)

ode.ODEGeom=New ODEGeom
ode\geom=dCreateBox(Space,1000,1000,1)
dGeomSetRotation(ode\geom,0,0,0)
dGeomSetPosition(ode\geom,0,500,15)
dGeomContactSetMu(ode\geom,0.0)

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

SetBuffer BackBuffer()

While Not KeyHit(1)

	Cls

	PTime=MilliSecs()

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

	UpdateGeoms()

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

	PhysicsTime#=MilliSecs()-PTime

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

	Flip

Wend

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

End

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

Function AddObject()

xp=Rand(-80,80)

ode.ODEGeom=New ODEGeom
ode\body=dBodyCreate(World)
dBodySetRotation(ode\body,0,0,0)
dBodySetPosition(ode\body,xp,500,0)
dBodySetAutoDisableFlag(ode\body,1)
ode\geom=dCreateSphere(Space,14)
dGeomSetBody(ode\geom,ode\body)
ode\r=Rand(255)
ode\g=Rand(255)
ode\b=Rand(255)

End Function

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

Function UpdateGeoms()

For ode.ODEGeom=Each ODEGeom
	If ode\body
		Color ode\r,ode\g,ode\b
		xpos#=400+dGeomGetPositionX#(ode\geom)
		ypos#=580-dGeomGetPositionY#(ode\geom)
		Oval xpos,ypos,28,28,1

		Color 255,255,255
		angle#=-dGeomGetRoll#(ode\geom)
		px#=Cos(angle)*10+xpos
		py#=Sin(angle)*10+ypos
		Oval 10+px,10+py,8,8,1
		End If
Next

End Function

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


All I've done is restrict motion on the z-axis using two very large slim boxes (barriers). You can think of these two boxes as two sheets of glass in a double glazed window, then the spheres are dropped between them. To make sure the spheres don't snag on the insides of the two barriers, their friction has been set to zero. There is also a plane at the base to stop them falling below ground level.

Hopefully this will help anyone trying to work out how to use JV-ODE in 2D, let me know if you have any questions.

:)