JV-ODE Physics Thread 3

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

@Slenkar: Have you tried Psychic Parrots demo? It uses a slightly different method which also limits the speed of the sphere.

Here it is...
; JV-ODE - Key Controlled Sphere Demo Using Angular Velocities
; Code by Jim Williams (VIP3R)
; Arrow key control by Jeff (PsychicParrot)

AppTitle "JV-ODE - Key Controlled sphere Demo"

Include "jv-ode.bb"

Graphics3D 800,600,0,2

Type ODEGeom

	Field body
	Field geom
	Field mesh
	Field pivot ; To find direction vectors
	Field head  ; Just a stupid head to show which direction it's going in!
	Field inair ; To tell if player is on the ground or not!!
	
End Type

; ### Setup ODE

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

dWorldSetGravity(World,0,-1,0)
dContactSetMode(dContactBounce)
dContactSetBounce(0.2)
dContactSetMu(38)

; ### 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

; ### Control vars

Global potato#=0 ; y rotation of thingy (rotater,rotato,potato!!)
Global spud#
Global fwd_key
Global back_key
Global left_key
Global right_key
Global jump_key

; ### 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
EntityPickMode plane,2

Mirror=CreateMirror()

AddObject() ; Add our thingy to the world

While Not KeyHit(1)

	PTime=MilliSecs()

	; Update controls
	
	c.ODEGeom=First ODEGeom
	Getkeys(c)
	movethingy(c)
	lookforground(c)
	
	PointEntity (camera,c\mesh)
	
	UpdateGeoms()

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

	PhysicsTime#=MilliSecs()-PTime

	UpdateWorld

	RenderWorld

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

	Flip

Wend

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

End

Function getkeys(c.ODEGeom)

	fwd_key=False
	back_key=False
	left_key=False
	right_key=False
	jump_key=False

	If c\inair=False Then ; Only let player control 'thrust' when on the ground
	
		If KeyDown(200) Then fwd_key=True
		If KeyDown(208) Then back_key=True

	EndIf
	
		If KeyDown(205) Then left_key=True
		If KeyDown(203) Then right_key=True
		If KeyDown(57) Then jump_key=True

	;EndIf

End Function

Function lookforground(c.ODEGeom)

	If LinePick(EntityX(c\mesh,True),EntityY(c\mesh,True)+1,EntityZ(c\mesh,True),0,-4,0)<>0 Then ; Check for anything 'under' player
		c\inair=False
	  Else
		c\inair=True
	EndIf

End Function

Function movethingy(c.ODEGeom)

	If fwd_key=True Then
		spud=spud+.1
	EndIf

	If back_key=True Then
		spud=spud-.1
	EndIf
	
	If left_key=True Then
		potato=potato-2
	End If
	
	If right_key=True Then
		potato=potato+2
	End If

	If jump_key=True And c\inair=False Then		
		dBodyAddForce(c\body,0,16 ,0)		
	End If

	RotateEntity c\head,0,potato#,0
	
	; Add forward force

	TFormVector 0,0,spud,c\head,0
	
	vx# = dBodyGetAngularVelX(c\body) + TFormedX()
	vy# = dBodyGetAngularVelY(c\body) + TFormedY()
	vz# = dBodyGetAngularVelZ(c\body) + TFormedZ()

	damping#=.8
	dBodySetAngularVel(c\body,vx#*damping#,vy#*damping#,vz#*damping#)

	spud=spud*.9
	
End Function

Function AddObject()

xp=Rand(-8,8)
zp=Rand(-8,8)
ode.ODEGeom=New ODEGeom
ode\body=dBodyCreate(World)
dBodySetRotation(ode\body,0,0,0)
dBodySetPosition(ode\body,xp,30,zp)
dBodySetAutoDisableFlag(ode\body,0)
ode\geom=dCreateSphere(Space,3)
dGeomSetBody(ode\geom,ode\body)
ode\mesh=CreateSphere()
ScaleMesh ode\mesh,3,3,3
EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
EntityAlpha ode\mesh,1
EntityShininess ode\mesh,0.7
ode\head=CreateCube()

ScaleMesh ode\head,1.5,1.5,1.5

ode\pivot=CreatePivot() ; Create pivot to get direction vectors

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)
		
		PositionEntity ode\head,dGeomGetPositionX#(ode\geom),dGeomGetPositionY#(ode\geom)+4.5,dGeomGetPositionZ#(ode\geom)
	
Next

End Function