JV-ODE Physics Thread 6

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

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

The best method is to use a fixed frame speed (30-60 fps etc) for the physics and separate timing for the rendering.

Here's a 10 Pin Bowling demo for Jeff...

; ###################################################################################################
; #									JV-ODE - 10 Pin Bowling Demo									#
; #									Code by Jim Williams (VIP3R)									#
; ###################################################################################################

AppTitle "JV-ODE - 10 Pin Bowling Demo"

Include "JV-ODE.bb"

Graphics3D 800,600,0,2

Global Ball

Global Aim=0
Global BallMass#=200

Dim PinBody(10)
Dim PinGeom(10)
Dim PinMesh(10)

Type ODEGeom
	Field body
	Field geom
	Field mesh
End Type

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

; ### Setup ODE

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

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

; ### 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,20,-50
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()

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

AddBall(0,4,-40)
AddPins()

While Not KeyHit(1)

	UpdateKeys()

	PTime=MilliSecs()

	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,"F1 - Add New Ball"
	Text 640,15,"Space - Push Ball"
	Text 640,30,"Arrows - Adjust Aim"
	Text 0,50,"Aim: "+Aim

	Flip

Wend

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

End

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

Function AddBall(xp#,yp#,zp#)

ode.ODEGeom=New ODEGeom
ode\body=dBodyCreate(World)
Ball=ode\body
dBodySetRotation(ode\body,0,0,0)
dBodySetPosition(ode\body,xp,yp,zp)
dBodySetAutoDisableFlag(ode\body,1)
mass=dMassCreate()
dMassSetSphereTotal(mass,BallMass,2.4)
dBodySetMass(ode\body,mass)
dMassDestroy(mass)
ode\geom=dCreateSphere(Space,2.4)
dGeomSetBody(ode\geom,ode\body)
ode\mesh=CreateSphere()
ScaleMesh ode\mesh,2.4,2.4,2.4
EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
EntityAlpha ode\mesh,1
EntityShininess ode\mesh,0.7

End Function

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

Function AddPins()

xp#=0
yp#=6
zp#=70

padding=7

For count=1 To 10
	ode.ODEGeom=New ODEGeom
	ode\body=dBodyCreate(World)
	PinBody(count)=ode\body
	dBodySetRotation(ode\body,0,0,0)
	dBodySetPosition(ode\body,xp,yp,zp)
	dBodySetAutoDisableFlag(ode\body,1)
	ode\geom=dCreateCylinder(Space,1.4,10)
	PinGeom(count)=ode\geom
	dGeomSetBody(ode\geom,ode\body)
	ode\mesh=CreateCylinder(8)
	PinMesh(count)=ode\mesh
	ScaleMesh ode\mesh,1.4,5,1.4
	EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
	EntityAlpha ode\mesh,1
	EntityShininess ode\mesh,0.7
	xp=xp+padding
	If count=1 Then xp=xp-((padding*1)+(padding*0.5)) : zp=zp+padding
	If count=3 Then xp=xp-((padding*2)+(padding*0.5)) : zp=zp+padding
	If count=6 Then xp=xp-((padding*3)+(padding*0.5)) : zp=zp+padding
Next

End Function

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

Function UpdateKeys()

If KeyHit(59)=1
	AddBall(0,4,-40)
	End If

If KeyDown(203)=1
	Aim=Aim-1
	End If

If KeyDown(205)=1
	Aim=Aim+1
	End If

If KeyDown(57)=1
	dBodyEnable(Ball)
	dBodyAddForce(Ball,Aim*0.5,0,400)
	End If

End Function

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

Function UpdateGeoms()

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

End Function

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