JV-ODE Physics Thread 11

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

And now it works! :)'
(still need some work, but at least it's decent!)

The capsule demo:

; JV-ODE - Key Controlled Sphere Demo Using Angular Velocities
; Code by Jim Williams (VIP3R)
; Arrow key control by Jeff (PsychicParrot)
; Character control by Juuso (www.GameProducer.net)

AppTitle "JV-ODE - Key Controlled capsule Demo"

Include "jv-ode.bb"

Graphics3D 800,600,0,2

SeedRnd(MilliSecs())

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.1)
dContactSetMu(10)

; ### 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
EntityAlpha plane, .7

;Mirror=CreateMirror()

AddObject() ; Add our thingy to the world

For i=1 To 10
	AddObject()
Next


While Not KeyHit(1)

	PTime=MilliSecs()

	; Update controls
	
	c.ODEGeom=First ODEGeom
	Getkeys(c)
	movethingy(c)
	lookforground(c)
	
	
	;PositionEntity 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

Global counter
Function movethingy(c.ODEGeom)

	speed = 2
	If fwd_key=True Then
		zforce# = speed 
	EndIf

	If back_key=True Then
		zforce# = -speed 
	EndIf
	
	If left_key=True Then
		xforce# = speed 
	End If
	
	If right_key=True Then
		xforce# = -speed 

	End If


	dBodySetAngularVel(c\body,0, 0, 0)
	dBodySetTorque (c\body,0, 0, 0)
	dBodySetRotation(c\body,90, 0, 0)

	
	;xforce# = leftforce-rightforce
	;zforce# = forwardforce-backforce

	dBodySetPosition(c\body, dGeomGetPositionX#(c\geom), 2.9 ,dGeomGetPositionZ#(c\geom) )

	dBodyAddForce(c\body, Int(xforce#), 0, Int(zforce#) )

	;DebugLog "get force:"+dBodyGetForce(c\body)
		

	If jump_key=True And c\inair=False Then		
		xforce = 10 * (Rnd(-1, 1))
		zforce = 10 * (Rnd(-1, 1))
		
		dBodyAddForce(c\body, 0 , 10 , 0 )		
	End If
	
End Function

Function AddObject()

xp=Rand(-8,8)
zp=Rand(-8,8)
ode.ODEGeom=New ODEGeom
ode\body=dBodyCreate(World)
dBodySetRotation(ode\body,90,0,0)
dBodySetPosition(ode\body,xp,0,zp)
dBodySetAutoDisableFlag(ode\body,0)
;ode\geom=dCreateSphere(Space,3)
;ode\geom=dCreateBox(Space, 2, 6, 2)
ode\geom=dCreateCapsule(space,1,8)

dGeomContactSetBounce(ode\geom, 1.000)



dGeomSetBody(ode\geom,ode\body)
;ode\mesh=CreateSphere()
;ode\mesh=CreateCube()
ode\mesh=CreateCylinder()
ScaleMesh ode\mesh,1,5,1
RotateMesh ode\mesh,90,0,0	; ### Align Blitz3D Cylinder To ODE Capsule

;RotateMesh ode\mesh,90,0,0

EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
EntityAlpha ode\mesh,1
EntityShininess ode\mesh,0.7
ode\head=CreateCube()


mass=dMassCreate()
;dMassSetBoxTotal(mass, 2, 1,1,1)
;dBodySetMass(ode\body,mass)


ScaleMesh ode\head,1.5,1.5,1.5

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


PointEntity (camera,ode\mesh)

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)+14.5,dGeomGetPositionZ#(ode\geom)
	
Next

End Function


Sorry for quadruple posting - stopping now;)