BlitzODE Physics Thread

Blitz3D Forums/Blitz3D Userlibs/BlitzODE Physics Thread

Recycling some code for this jeep demo, Grab psionic jeep model first, then try it out.

http://www.psionic3d.co.uk/cgi-bin/imageFolio.cgi?direct=Free%20Stuff/3D%20Models


Graphics3D 800,600,16,1

Include "BlitzODE.bb"

Global Cvis=False
cmass#=150; chassis mass
tmass#=75 ; 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(.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)
;-------------------------------

;Chasis
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#=.108 ; Increase to make shocks soft and springy

;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, 25, 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, 25

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


millis#=MilliSecs()
millis2#=MilliSecs()
elapsed#=0


While Not KeyDown(1)

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

	ODE_dBodyEnable ode_body1
	ODE_dBodyEnable ode_body2
	ODE_dBodyEnable ode_body3
	
	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/(80/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(.9);
		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)
	;-------------

	ODE_dWorldQuickStep .1
	ODE_dWorldQuickStep .1
	
	;
	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
	
	RenderWorld
	
;	Text 10,10,elapsed
;	Text 10,20,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,"Steer Mouse or Arrow keys"	
	Text 10,80,"use ARROWS to move"
	
	Flip
Wend

ODE_dCloseODE()

MoveMouse 400,300:End