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.

Updated program to only call ODE physics code every 15ms.

Graphics3D 800,600,16,1

Include "BlitzODE.bb"

Global Cvis=False
cmass#=150; chassis mass
tmass#=80 ; tire mass
ice=False


jeep=LoadMesh("jeep1.3ds")
ScaleEntity jeep,.4,.4,.4 
TranslateEntity jeep,0,-1.6,-1.5

cam=CreateCamera()
	PositionEntity cam,0,2,-8
	
l=CreateLight(2)
	PositionEntity l,-5,10,-5
	LightRange l,10
	
AmbientLight 200,200,200
 
pl=CreatePlane(16)
	EntityAlpha pl,.5
	texpl=CreateTexture(128,128,1+8)
	
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(texpl)
	ScaleTexture texpl,6,6
	EntityTexture pl,texpl,0,0
m=CreateMirror()

;---- Setting contact parameters ---
;ODE_dSetContactMode(dContactBounce + dContactSoftERP + dContactSoftCFM + dSlip1 + dSlip2)

ODE_dSetMU(1.9); Friction  (smaller slippery)
ODE_dSetBOUNCE(.5)
ODE_dSetSOFT_ERP(.5)
ODE_dSetSOFT_CFM(.01)
;-------------------------------

;Crear un Espacio para ODE:
Global space = ODE_dWorldCreate(1)
;-------------------------------

;Chassis
ode_body1=ODE_dBodyCreate()
	geom=ODE_dCreateBox(space,.3,.5,4,10)
	ODE_dGeomSetBody geom,ode_body1
	
	ODE_dBodySetPosition ode_body1,0,4,-2
	ODE_dBodySetRotation ode_body1,0,0,0
	ODE_dBodySetMass ode_body1,cmass
vis1=CreateCube()
	ScaleEntity vis1,.15,.25,2
	EntityColor vis1,255,100,0
	
	
;rueda der
ode_body2=ODE_dBodyCreate()
	;geom=ODE_dCreateBox(space,.25,1,1,10)
	geom=ODE_dCreateSphere(space,1,10)
	ODE_dGeomSetBody geom,ode_body2
	
	ODE_dBodySetPosition ode_body2,2,3,0
	ODE_dBodySetRotation ode_body2,0,0,0
	ODE_dBodySetMass ode_body2,tmass
vis2=CreateCube()
	ScaleEntity vis2,.125,.5,.5
	EntityColor vis2,100,255,0
;rueda izq
ode_body3=ODE_dBodyCreate()
	;geom=ODE_dCreateBox(space,.25,1,1,10)
	geom=ODE_dCreateSphere(space,1,10)
	ODE_dGeomSetBody geom,ode_body3
	
	ODE_dBodySetPosition ode_body3,-2,3,0
	ODE_dBodySetRotation ode_body3,0,0,0
	ODE_dBodySetMass ode_body3,tmass
vis3=CreateCube()
	ScaleEntity vis3,.125,.5,.5
	EntityColor vis3,100,255,0

;Traseras
;rueda der
ode_body4=ODE_dBodyCreate()
	;geom=ODE_dCreateBox(space,.25,1,1,10)
	geom=ODE_dCreateSphere(space,1,10)
	ODE_dGeomSetBody geom,ode_body4
	
	ODE_dBodySetPosition ode_body4,2,3,-4
	ODE_dBodySetRotation ode_body4,0,0,0
	ODE_dBodySetMass ode_body4,tmass
vis4=CreateCube()
	ScaleEntity vis4,.125,.5,.5
	EntityColor vis4,0,0,255

;rueda izq
ode_body5=ODE_dBodyCreate()
	;geom=ODE_dCreateBox(space,.25,1,1,10)
	geom=ODE_dCreateSphere(space,1,10)
	ODE_dGeomSetBody geom,ode_body5
	
	ODE_dBodySetPosition ode_body5,-2,3,-4
	ODE_dBodySetRotation ode_body5,0,0,0
	ODE_dBodySetMass ode_body5,tmass
vis5=CreateCube()
	ScaleEntity vis5,.125,.5,.5
	EntityColor vis5,0,0,255
	


;Joints
joint1=ODE_dJointCreateBall()
	ODE_dJointAttach joint1,0,ode_body1
	
	ODE_dJointSetBallAnchor joint1,0,5,0
	
	;ODE_dJointSetUniversalAxis1 joint1,0,0,.1
	;ODE_dJointSetUniversalAxis2 joint1,1,0,0

suspension_hard#=8
suspension_soft#=.15 ; Increase to make shocks soft and springy

;Roll cage
;joint0=ODE_dJointCreateFixed()
;	ODE_dJointAttach joint0,ode_body1,ode_body6

	
;rueda der
joint2=ODE_dJointCreateHinge2()
	ODE_dJointAttach joint2,ode_body1,ode_body2

	ODE_dJointSetHinge2Anchor joint2,1.875,3,0
	ODE_dJointSetHinge2Axis1 joint2,0,1,0
	ODE_dJointSetHinge2Axis2 joint2,1,0,0

	ODE_dJointSetHinge2Param joint2,dParamLoStop,0;-Pi/4
	ODE_dJointSetHinge2Param joint2,dParamHiStop,0;Pi/4
	ODE_dJointSetHinge2Param joint2,dParamSuspensionERP,suspension_hard
	ODE_dJointSetHinge2Param joint2,dParamSuspensionCFM,suspension_soft

;rueda izq
joint3=ODE_dJointCreateHinge2()
	ODE_dJointAttach joint3,ode_body1,ode_body3

	ODE_dJointSetHinge2Anchor joint3,-1.875,3,0
	ODE_dJointSetHinge2Axis1 joint3,0,1,0
	ODE_dJointSetHinge2Axis2 joint3,1,0,0

	ODE_dJointSetHinge2Param joint3,dParamLoStop,0;-Pi/4
	ODE_dJointSetHinge2Param joint3,dParamHiStop,0;Pi/4
	ODE_dJointSetHinge2Param joint3,dParamSuspensionERP,suspension_hard
	ODE_dJointSetHinge2Param joint3,dParamSuspensionCFM,suspension_soft

;Joints Traseros:
;rueda der
joint4=ODE_dJointCreateHinge2()
	ODE_dJointAttach joint4,ode_body1,ode_body4

	ODE_dJointSetHinge2Anchor joint4,1.875,3,-4
	ODE_dJointSetHinge2Axis1 joint4,0,1,0
	ODE_dJointSetHinge2Axis2 joint4,1,0,0

	ODE_dJointSetHinge2Param joint4,dParamLoStop,0;-Pi/4
	ODE_dJointSetHinge2Param joint4,dParamHiStop,0;Pi/4
	ODE_dJointSetHinge2Param joint4,dParamSuspensionERP,suspension_hard
	ODE_dJointSetHinge2Param joint4,dParamSuspensionCFM,suspension_soft
	
;rueda izq
joint5=ODE_dJointCreateHinge2()
	ODE_dJointAttach joint5,ode_body1,ode_body5

	ODE_dJointSetHinge2Anchor joint5,-1.875,3,-4
	ODE_dJointSetHinge2Axis1 joint5,0,1,0
	ODE_dJointSetHinge2Axis2 joint5,1,0,0

	ODE_dJointSetHinge2Param joint5,dParamLoStop,0;-Pi/4
	ODE_dJointSetHinge2Param joint5,dParamHiStop,0;Pi/4
	ODE_dJointSetHinge2Param joint5,dParamSuspensionERP,suspension_hard
	ODE_dJointSetHinge2Param joint5,dParamSuspensionCFM,suspension_soft

	

EntityParent jeep,vis1

;------- Create fixed box ----------

geom = ODE_dCreateBox(space, 15, .1, 35, 10)
ODE_dGeomSetPosition(geom, 40, 2.5, 0)
ODE_dGeomSetRotation(geom, 0, 0, 20)
mesh = CreateCube()
PositionEntity mesh, 40, 2.5, 0
RotateEntity mesh, 0, 0, 20
ScaleMesh mesh, .5, .05, .5
ScaleEntity mesh, 15, 1, 35

EntityAlpha vis1,cvis
EntityAlpha vis2,cvis
EntityAlpha vis3,cvis
EntityAlpha vis4,cvis
EntityAlpha vis5,cvis


millis#=MilliSecs()
elapsed#=millis#-millis2#
millis2#=millis1#


While Not KeyDown(1)

	millis1=MilliSecs()
	elapsed=millis1-millis
	millis=millis1

	ODE_dBodyEnable ode_body1
	ODE_dBodyEnable ode_body2
	ODE_dBodyEnable ode_body3
	ODE_dBodyEnable ode_body4
	ODE_dBodyEnable ode_body5
		
	
	mxs#=mxs#+(MouseXSpeed()-mxs#)/3
	mys#=mys#+(MouseYSpeed()-mys#)/3

	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	
	If KeyDown(203) Or KeyDown(205) Then
		mxs=0
	EndIf

	If KeyDown(200)
	ODE_dBodyAddRelTorque ode_body2,40,0,0
	ODE_dBodyAddRelTorque ode_body3,40,0,0
	EndIf
	If KeyDown(208)
	ODE_dBodyAddRelTorque ode_body2,-40,0,0
	ODE_dBodyAddRelTorque ode_body3,-40,0,0
	EndIf
	
	If mxs=0 Then
		angle#=Pi/4
	Else
		angle#=Pi/(90/Abs(mxs))
	EndIf
	

	
	If KeyDown(203) Or mxs<0
	ODE_dJointSetHinge2Param joint2,dParamLoStop,angle
	ODE_dJointSetHinge2Param joint2,dParamHiStop,angle

	ODE_dJointSetHinge2Param joint3,dParamLoStop,angle
	ODE_dJointSetHinge2Param joint3,dParamHiStop,angle
	
	ElseIf KeyDown(205) Or mxs>0
	
		ODE_dJointSetHinge2Param joint2,dParamLoStop,-angle
		ODE_dJointSetHinge2Param joint2,dParamHiStop,-angle
	
		ODE_dJointSetHinge2Param joint3,dParamLoStop,-angle
		ODE_dJointSetHinge2Param joint3,dParamHiStop,-angle
	
	Else
	ODE_dJointSetHinge2Param joint2,dParamLoStop,0;-Pi/4
	ODE_dJointSetHinge2Param joint2,dParamHiStop,0;Pi/4

	ODE_dJointSetHinge2Param joint3,dParamLoStop,0;-Pi/4
	ODE_dJointSetHinge2Param joint3,dParamHiStop,0;Pi/4
	EndIf

	
	If KeyHit(30)
		ODE_dJointAttach joint1,0,0
	EndIf
	
	If KeyHit(31)
		ODE_dJointAttach joint1,0,ode_body1
	EndIf
	
	; (I)ce
	If KeyHit(23) Then
		Ice=Not Ice
		If Ice Then
			ODE_dSetMU(.1);
		Else
			ODE_dSetMU(1.8);
		End If
	End If
		
	If KeyHit(19) Then
		cvis=Not cvis
		EntityAlpha vis1,cvis
		EntityAlpha vis2,cvis
		EntityAlpha vis3,cvis
		EntityAlpha vis4,cvis
		EntityAlpha vis5,cvis
	End If

	;-------------
	PositionEntity vis1,ODE_dBodyGetPositionX(ode_body1),ODE_dBodyGetPositionY(ode_body1),ODE_dBodyGetPositionZ(ode_body1)
	RotateEntity vis1,ODE_dBodyGetPitch(ode_body1),ODE_dBodyGetYaw(ode_body1),ODE_dBodyGetRoll(ode_body1)

	PositionEntity vis2,ODE_dBodyGetPositionX(ode_body2),ODE_dBodyGetPositionY(ode_body2),ODE_dBodyGetPositionZ(ode_body2)
	RotateEntity vis2,ODE_dBodyGetPitch(ode_body2),ODE_dBodyGetYaw(ode_body2),ODE_dBodyGetRoll(ode_body2)

	PositionEntity vis3,ODE_dBodyGetPositionX(ode_body3),ODE_dBodyGetPositionY(ode_body3),ODE_dBodyGetPositionZ(ode_body3)
	RotateEntity vis3,ODE_dBodyGetPitch(ode_body3),ODE_dBodyGetYaw(ode_body3),ODE_dBodyGetRoll(ode_body3)

	PositionEntity vis4,ODE_dBodyGetPositionX(ode_body4),ODE_dBodyGetPositionY(ode_body4),ODE_dBodyGetPositionZ(ode_body4)
	RotateEntity vis4,ODE_dBodyGetPitch(ode_body4),ODE_dBodyGetYaw(ode_body4),ODE_dBodyGetRoll(ode_body4)

	PositionEntity vis5,ODE_dBodyGetPositionX(ode_body5),ODE_dBodyGetPositionY(ode_body5),ODE_dBodyGetPositionZ(ode_body5)
	RotateEntity vis5,ODE_dBodyGetPitch(ode_body5),ODE_dBodyGetYaw(ode_body5),ODE_dBodyGetRoll(ode_body5)
	
	;PositionEntity vis6,ODE_dBodyGetPositionX(ode_body6),ODE_dBodyGetPositionY(ode_body6),ODE_dBodyGetPositionZ(ode_body6)
	;RotateEntity vis6,ODE_dBodyGetPitch(ode_body6),ODE_dBodyGetYaw(ode_body6),ODE_dBodyGetRoll(ode_body6)

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

	PrimeTime#=PrimeTime+elapsed#
	If PrimeTime#>15 Then 
		Ptime1#=MilliSecs()
		ODE_dWorldQuickStep .1
		ODE_dWorldQuickStep .1
		Ptime2#=MilliSecs()
		PrimeTime#=PrimeTime#-15
	End If
			
	;
	chase_cam_dist=15
	PointEntity cam,vis1
	ed#=EntityDistance(cam,vis1)
	If ed#>chase_cam_dist Then 
		MoveEntity cam,0,0,(ed#-chase_cam_dist)/2
		PositionEntity cam,EntityX(cam),EntityY(vis1)+5,EntityZ(cam)
		
	End If

	UpdateWorld
	
	If ice Then
		CameraClsColor cam,200,200,200
	Else
		CameraClsColor cam,0,0,128
	End If

	If EntityY(vis1)>4 Then
		ODE_dBodyAddForce(ode_body1, 0,650,0)
	EndIf	
	
	RenderWorld
	
	
	Text 10,10,"Time: "+ elapsed# + "ms" 
	Text 10,20,"Ang Vel: " + ODE_dBodyGetAngularVelX(ode_body2)
	Text 10,30,"press A to release the car attach"
	Text 10,40,"press S to force a re-attach of the car"
	Text 10,50,"press R to hide/show rigid body frame"
	Text 10,60,"Ice=" + ice
	Text 10,70,"Hgt: " + EntityY(vis1)	
	Text 10,90,"Physics Time: " + Str(Ptime2#-Ptime1#) + "ms"
	Text 10,100,"use ARROWS to move"
	
	If elapsed>max_elapsed Then
		max_elapsed=elapsed
	EndIf
	
	Flip
Wend

ODE_dCloseODE()

MoveMouse 400,300:End