JV-ODE Physics Thread 6

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

Here you go Shifty...

; ###################################################################################################
; #										JV-ODE - Slider Demo										#
; #									Code by Jim Williams (VIP3R)									#
; ###################################################################################################

AppTitle "JV-ODE - Slider Demo"

Include "JV-ODE.bb"

Graphics3D 800,600,0,2

Global SliderRange=30

Type ODEGeom
	Field body
	Field geom
	Field mesh
End Type

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

; ### Create Slider

For count=1 To 2
	ode.ODEGeom=New ODEGeom
	ode\body=dBodyCreate(World)
	Sphere(count)=ode\body
	dBodySetRotation(ode\body,0,0,0)
	dBodySetPosition(ode\body,-30,100-(count*30),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
	If count=1
		Joint(count)=dJointCreateBall(World,0)
		dJointAttach(Joint(count),0,Sphere(count))
		dJointSetBallAnchor(Joint(count),dBodyGetPositionX(Sphere(count)),dBodyGetPositionY(Sphere(count)),dBodyGetPositionZ(Sphere(count)))
		Else
		Joint(count)=dJointCreateSlider(World,0)
		dJointAttach(Joint(count),Sphere(count-1),Sphere(count))
		dJointSetSliderAxis(Joint(count),0,1,0)
		dJointSetSliderParam(Joint(count),dParamLoStop,-SliderRange)
		dJointSetSliderParam(Joint(count),dParamHiStop,SliderRange)
		End If
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()

	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 Slider"
	Text 640,15,"Using Arrow Keys"
	Text 640,55,"Add Motor Velocity"
	Text 640,70,"Using F1 & F2"

	Text 0,80,"Slider Position: "+dJointGetSliderPosition(Joint(2))
	Text 0,95,"Slider Rate: "+dJointGetSliderPositionRate(Joint(2))

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

dBodyEnable(Sphere(2))

If KeyDown(59)
	dJointSetSliderParam(Joint(2),dParamVel,-3)
	dJointSetSliderParam(Joint(2),dParamFMax,100)
	End If

If KeyDown(60)
	dJointSetSliderParam(Joint(2),dParamVel,3)
	dJointSetSliderParam(Joint(2),dParamFMax,100)
	End If

If dJointGetSliderPosition(Joint(2))=SliderRange Or dJointGetSliderPosition(Joint(2))=-SliderRange
	dJointSetSliderParam(Joint(2),dParamVel,0)
	dJointSetSliderParam(Joint(2),dParamFMax,100)
	End If

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

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

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

The demo shows how to setup the slider, it doesn't have any spring/dampening yet though.

There's no need to specify an anchor position for a slider, it actually has two anchors - anchor 1 is positioned at body 1, anchor 2 is positioned at body 2.