JV-ODE Physics Thread 8

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

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.

JV-ODE V1.18 Update Released

Please check your inbox :)

The new update corrects an issue found in the Joint Feedback feature of JV-ODE.

Please let me know if you experience any problems with the new update.

:)

@Danny: Here's the Collision Forces demo to work with JV-ODE V1.18
; ###################################################################################################
; #								   JV-ODE - Collision Forces Demo									#
; #									Code by Jim Williams (VIP3R)									#
; #								Devious Codeworks - Copyright © 2006								#
; ###################################################################################################

AppTitle "JV-ODE - Collision Forces Demo"

Include "JV-ODE.bb"

Graphics3D 800,600,0,2

Type ODEGeom
	Field body
	Field geom
	Field mesh
End Type

Dim Force(10)
Dim Joint(10)

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

; ### 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.95)
dContactSetMu(48)

; ### 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,40,-30
RotateEntity Camera,30,0,0

; ### Create plane

dCreatePlane(Space,0,1,0,0)

Plane=CreatePlane()

EntityAlpha Plane,0.5

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()

AddObjects()

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

While Not KeyHit(1)

	PTime=MilliSecs()

	UpdateGeoms()

	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 10,280,"Collision Force:"

	For count=1 To 10
		dJointGetFeedbackForce1(Joint(count))
		ShowBar(78+(count*58),320,dVectorY())
	Next

	Flip

Wend

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

End

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

Function AddObjects()

For count=1 To 10
	; ### Body + Geom
	ode.ODEGeom=New ODEGeom
	ode\body=dBodyCreate(World)
	ParentBody=ode\body
	dBodySetRotation(ode\body,0,0,0)
	dBodySetPosition(ode\body,-55+(10*count),45,30)
	mass=dMassCreate()
	dMassSetSphereTotal(mass,0.5,4)
	dBodySetMass(ode\body,mass)
	dMassDestroy(mass)
	dBodySetAutoDisableFlag(ode\body,0)
	ode\geom=dCreateSphere(Space,4)
	dGeomContactSetMode(ode\geom,dContactBounce)
	dGeomContactSetBounce(ode\geom,count*0.1)
	dGeomSetBody(ode\geom,ode\body)
	ode\mesh=CreateSphere()
	ScaleMesh ode\mesh,4,4,4
	EntityColor ode\mesh,Rand(255),Rand(255),Rand(255)
	EntityAlpha ode\mesh,1
	EntityShininess ode\mesh,0.7

	; ### Core Body
	ode.ODEGeom=New ODEGeom
	ode\body=dBodyCreate(World)
	CoreBody=ode\body
	dBodySetRotation(ode\body,0,0,0)
	dBodySetPosition(ode\body,-55+(10*count),45,30)
	mass=dMassCreate()
	dMassSetSphereTotal(mass,0.5,4)
	dBodySetMass(ode\body,mass)
	dMassDestroy(mass)
	dBodySetAutoDisableFlag(ode\body,0)

	; ### Join Core + Parent
	Joint(count)=dJointCreateFixed(World,0)
	dJointAttach(Joint(count),CoreBody,ParentBody)
	dJointFeedbackEnable(Joint(count))
Next

End Function

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

Function ShowBar(xpos,ypos,yvec,size=8)

If yvec<0
	Color 240,0,140
	Else
	Color 0,240,140
	End If

Rect xpos,ypos,size,size+Abs(Int(yvec*2))

Color 255,255,255

End Function

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

Function UpdateGeoms()

For ode.ODEGeom=Each ODEGeom
	If ode\geom
		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
		End If
Next

End Function

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

You will notice in the AddObjects() function I've created an extra ODE body (with no geom), it is then connected to the parent ODE body using a fixed joint. To read the force and torque, you can call Joint Feedback on these fixed joints. The benefit to this method over Joint Feedback directly on contact joints is that you only need to deal with one value, rather than one value per contact (up to 4+ contacts per body). Let me know if it works well for you ;)