JV-ODE Physics Thread 5

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

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.

You can use them for both AFAIK :)

[edit] Ok, you must call dBodyGetForce() and dBodyGetTorque() before dSpaceCollide() in the main loop for the functions to work correctly. The dWorldQuickStep() process will zero their value, so they must be called before it.

Here's the Rope demo showing dBodyGetForce() and dBodyGetTorque() being used correctly with both the Body ID (rope sphere) and the Joint ID (rope joint)...
; ###################################################################################################
; #										 JV-ODE - Rope Demo											#
; #									Code by Jim Williams (VIP3R)									#
; ###################################################################################################

AppTitle "JV-ODE - Rope Demo"

Include "JV-ODE.bb"

Graphics3D 800,600,0,2

Dim Joint(10)
Dim Sphere(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.1)
dContactSetMu(48)

; ### Create Rope

For count=1 To 10
	ode.ODEGeom=New ODEGeom
	ode\body=dBodyCreate(World)
	Sphere(count)=ode\body
	dBodySetRotation(ode\body,0,0,0)
	dBodySetPosition(ode\body,-30,90-(count*8),0)
	dBodySetAutoDisableFlag(ode\body,1)
	ode\geom=dCreateSphere(Space,4)
	dGeomSetBody(ode\geom,ode\body)
	ode\mesh=CreateSphere()
	ScaleMesh ode\mesh,4,4,4
	RotateMesh ode\mesh,0,0,0
	PositionMesh ode\mesh,0,0,0
	EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
	EntityAlpha ode\mesh,1
	EntityShininess ode\mesh,0.7
	Joint(count)=dJointCreateBall(World,0)
	If count=1
		dJointAttach(Joint(count),0,Sphere(count))
		Else
		dJointAttach(Joint(count),Sphere(count-1),Sphere(count))
		End If
	dJointSetBallAnchor(Joint(count),dBodyGetPositionX(Sphere(count)),dBodyGetPositionY(Sphere(count)),dBodyGetPositionZ(Sphere(count)))
Next

; ### 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,90,-100
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()

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

While Not KeyHit(1)

	UpdateKeys()

	PTime=MilliSecs()

	If MilliSecs()-Timer>700
		AddObject()
		Timer=MilliSecs()
		End If

	UpdateGeoms()

	dBodyGetForce(Joint(1)) : JFX#=dVectorX() : JFY#=dVectorY() : JFZ#=dVectorZ()
	dBodyGetTorque(Joint(1)) : JTX#=dVectorX() : JTY#=dVectorY() : JTZ#=dVectorZ()
	dBodyGetForce(Sphere(10)) : SFX#=dVectorX() : SFY#=dVectorY() : SFZ#=dVectorZ()
	dBodyGetTorque(Sphere(10)) : STX#=dVectorX() : STY#=dVectorY() : STZ#=dVectorZ()

	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,"Add Force To Rope"
	Text 640,15,"Using Arrow Keys"

	Text 0,50,"Joint Force X:"+JFX
	Text 0,65,"Joint Force Y:"+JFY
	Text 0,80,"Joint Force Z:"+JFZ

	Text 0,105,"Joint Torque X:"+JTX
	Text 0,120,"Joint Torque Y:"+JTY
	Text 0,135,"Joint Torque Z:"+JTZ

	Text 0,170,"Sphere Force X:"+SFX
	Text 0,185,"Sphere Force Y:"+SFY
	Text 0,200,"Sphere Force Z:"+SFZ

	Text 0,225,"Sphere Torque X:"+STX
	Text 0,240,"Sphere Torque Y:"+STY
	Text 0,255,"Sphere Torque Z:"+STZ

	Flip

Wend

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

End

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

Function AddObject()

ode.ODEGeom=New ODEGeom
ode\body=dBodyCreate(World)
dBodySetRotation(ode\body,0,0,0)
dBodySetPosition(ode\body,30,80,0)
dBodySetAutoDisableFlag(ode\body,1)
ode\geom=dCreateBox(Space,10,5,10)
dGeomSetBody(ode\geom,ode\body)
ode\mesh=CreateCube()
ScaleMesh ode\mesh,5,2.5,5
EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
EntityAlpha ode\mesh,1
EntityShininess ode\mesh,0.7

End Function

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

Function UpdateKeys()

If KeyDown(203) Then dBodyAddForce(Sphere(10),-10,0,0)
If KeyDown(205) Then dBodyAddForce(Sphere(10),10,0,0)
If KeyDown(200) Then dBodyAddForce(Sphere(10),0,0,10)
If KeyDown(208) Then dBodyAddForce(Sphere(10),0,0,-10)

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

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