JV-ODE Physics Wrapper

Blitz3D Forums/Blitz3D Userlibs/JV-ODE Physics Wrapper

New and improved light on rope.


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

AppTitle "JV-ODE - Rope Demo"

Include "ode.bb"

Graphics3D 800,600,0,2

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)

dWorldSetGravity(World,0,-1,0)
dContactSetMode(dContactBounce)
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)
	ode\geom=dCreateSphere(Space,4)
	dGeomSetBody(ode\geom,ode\body)
	ode\mesh=CreateSphere()
		If count=10 Then
			c1=CreateCone(16,0,ode\mesh)
			ScaleEntity c1,5,5,5
			
			c2=CreateCone(16,0,ode\mesh)
			FlipMesh c2
			ScaleEntity c2,5,5,5
			
			c3=CreateCone(16,0,ode\mesh)
			ScaleEntity c3,50,50,50
			EntityAlpha c3,.1 
			EntityColor c3,255,255,255
			MoveEntity c3,0,-50,0
			
			c4=CreateCone(16,0,ode\mesh)
			FlipMesh c4
			ScaleEntity c4,50,50,50
			EntityAlpha c4,.1 
			EntityColor c4,255,255,255
			MoveEntity c4,0,-50,0

		End If
	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)),0)
	
	If count=10 Then
		lgt=CreateLight(3,ode\mesh)
		RotateEntity lgt,90,0,0
		
	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()

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

MoveMouse 400,0

While Not KeyHit(1)

	PTime=MilliSecs()

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

	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)

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

	UpdateGeoms()

	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"

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

End Function

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