Tokamak And Ode

Blitz3D Forums/Blitz3D Programming/Tokamak And Ode

Clarks(Posted 2004) [#1]
As far as cars are concerned, i see no possible way of building a suspension system in biltz using tokamak or whatever the physics engine is called. ODE has a suspension system but the lastest wrapper has some quirky things going on. This really sets me back on my project.

I like tokamak for its static meshes, but it handles angles and torque in world coordinates. Thats the first physics engine that i know of that handles angles and torque in world coordinates. That is so lame. Building a suspension system in tokamak seems impossible. I tried and the results are no good.

I like ode because it has a suspension system, but the lastest wrapper has some quirky bugs in it which holds me back from my project. Also it seems like ode swapped x and z.

Why cant there just be physics engine in which we dont have to deal with limitations or quirky bugs.

Im trying to build my own in blitz3d, im looking at source codes of how other people did. So far no good.

Is there any hope?

;----------------------------------------------------------After long thought and reviewing tokamak functions, there actually is a way to build up a suspension system in tokamak. One would have to just make good use of the "toksim_setcollisionresponse()" function. But the world angles and torque are still and issue.


Sweenie(Posted 2004) [#2]
Here is the spring & damping system I use.
Check the function UpdateSpring.
Const FPS=60

Graphics3D 800,600,0,2
SetBuffer BackBuffer()

WireFrame False

camera = CreateCamera()
PositionEntity camera,0,3,-9

TOKSIM_CreateSimulator(0,-9.8,0)

SeedRnd MilliSecs()  

Ground = CreateCube()
ScaleMesh Ground,0.5,0.5,0.5
ScaleEntity Ground,100,10,100
EntityColor Ground,Rnd(100,230),Rnd(100,230),Rnd(100,230) 
PositionEntity Ground,0,-5,0
Ground_AB = TOKAB_Create()
TOKAB_AddBox Ground_AB,100.0,10.0,100.0
TOKAB_SetPosition Ground_AB,0.0,-5.0,0.0

Body1_Mesh = CreateSphere()
ScaleMesh Body1_Mesh ,0.5,0.5,0.5
EntityColor Body1_Mesh ,Rnd(100,230),Rnd(100,230),Rnd(100,230) 
Body1_RB = TOKRB_Create()
TOKRB_AddSphere Body1_RB,1.0
TOKRB_SetPosition Body1_RB,0.0,10,0.0
TOKRB_SetMass Body1_RB,1.0
TOKRB_SetSphereInertiaTensor Body1_RB,1.0,1.0

Body2_Mesh = CreateSphere()
ScaleMesh Body2_Mesh ,0.5,0.5,0.5
EntityColor Body2_Mesh ,Rnd(100,230),Rnd(100,230),Rnd(100,230) 
Body2_RB = TOKRB_Create()
TOKRB_AddSphere Body2_RB,1.0
TOKRB_SetPosition Body2_RB,0.0,5.0,0.0
TOKRB_SetMass Body2_RB,1.0
TOKRB_SetSphereInertiaTensor Body2_RB,1.0,1.0

SlideJoint = TOKJOINT_Create(2,Body1_RB,Body2_RB)
TOKJOINT_SetType SlideJoint,4 ; Slidejoint
TOKJOINT_SetPositionAndRotationWorld SlideJoint,0,7.5,0,0,0,0 
TOKJOINT_Enable SlideJoint,True

Centerpivot = CreatePivot()

light=CreateLight()
PositionEntity light,255,255,-115
PointEntity light,Centerpivot

period=1000/FPS
time=MilliSecs()-period

; MainLoop
While Not KeyHit(1)

	Repeat
		elapsed=MilliSecs()-time
	Until elapsed

	ticks=elapsed/period
	tween#=Float(elapsed Mod period)/Float(period)

	For k=1 To ticks
		time=time+period
		If k=ticks Then CaptureWorld

		TOKSIM_Advance(1.5/FPS,1)


	UpdateWorld

   PositionEntity Body1_Mesh,TOKRB_GetX(Body1_RB),TOKRB_GetY(Body1_RB),TOKRB_GetZ(Body1_RB) 
   RotateEntity Body1_Mesh,TOKRB_GetPitch(Body1_RB),TOKRB_GetYaw(Body1_RB),TOKRB_GetRoll(Body1_RB),False
   PositionEntity Body2_Mesh,TOKRB_GetX(Body2_RB),TOKRB_GetY(Body2_RB),TOKRB_GetZ(Body2_RB) 
   RotateEntity Body2_Mesh,TOKRB_GetPitch(Body2_RB),TOKRB_GetYaw(Body2_RB),TOKRB_GetRoll(Body2_RB),False

;   updatespring
	UpdateSpring Body1_RB,Body2_RB,50,1.0,4.0

	;Prevent it from falling over.
	TOKRB_SetAngularMomentum Body1_RB,TOKRB_GetAngularMomentumX#(Body1_RB)*0.9,TOKRB_GetAngularMomentumY#(Body1_RB),TOKRB_GetAngularMomentumZ#(Body1_RB)*0.9

	Next


	
If KeyHit(57) Then
  TOKRB_ApplyImpulse Body1_RB,0,15,0 
EndIf

	RenderWorld tween

Text 0,10,"Render Time:"+Str(elapsed)+ " milliseconds"
	Flip False

Wend

TOKSIM_DestroySimulator()

End


Function UpdateSpring(RB1,RB2,Stiffness#,Damping#,SpringLength#)

;Add spring force
diffX#=TOKRB_GetX#(RB1) - TOKRB_GetX#(RB2)
diffY#=TOKRB_GetY#(RB1) - TOKRB_GetY#(RB2)
diffZ#=TOKRB_GetZ#(RB1) - TOKRB_GetZ#(RB2)
diffLen#=Sqr(diffX#*diffX#+diffY#*diffY#+diffZ#*diffZ#)
Displacement#=diffLen#-SpringLength#
nx#=diffx#/diffLen#
ny#=diffy#/diffLen#
nz#=diffz#/diffLen#
fx#=nx#*(Displacement#*Stiffness#)
fy#=ny#*(Displacement#*Stiffness#)
fz#=nz#*(Displacement#*Stiffness#)

;Add damping force
Vx#=TOKRB_GetVelocityX#(RB1)-TOKRB_GetVelocityX#(RB2)
Vy#=TOKRB_GetVelocityY#(RB1)-TOKRB_GetVelocityY#(RB2)
Vz#=TOKRB_GetVelocityZ#(RB1)-TOKRB_GetVelocityZ#(RB2)
speed#=DotProduct#(nx#,ny#,nz#,Vx#,Vy#,Vz#)
dampX#=nx#*speed#*Damping#
dampY#=ny#*speed#*Damping#
dampZ#=nz#*speed#*Damping#

fx#=fx#+dampX#
fy#=fy#+dampY#
fz#=fz#+dampZ#


TOKRB_SetForce RB1,-fx#,-fy,-fz#
TOKRB_SetForce RB2,fx#,fy,fz#

;Stop

End Function



Function DotProduct#(x1#,y1#,z1#,x2#,y2#,z2#)
	DProd#=((x1*x2)+(y1*y2)+(z1*z2))
	Return DProd#
End Function



Clarks(Posted 2004) [#3]
Thanks for the code sweenie, i didnt know that tokamak had a sliderjoint, sliders are very useful for suspension as well as hinges too. Your code uses a universal spring. Universal springs wont work well with racing simulations.

As far as i know, suspension forces that act on a car and wheel must always act in body coordinates. Which of course youll have to transform to world coordinates when using tokamak.

The suspension problem is solved, but the world torque and world angle are a big issue. I'll have to see if i can get around this.


GW(Posted 2004) [#4]
did you take a look at the verlet system by Andreas Blixt? You can find it in the Blitzcoder showcase.


Wayne(Posted 2004) [#5]
Perhaps you could define quirky ODE bugs and (x,z) swapping.


Clarks(Posted 2004) [#6]
GW->the verlet system, ill check it out.

Wayne->are you saying you want me to define the quirky bugs and swapping. if so, when i used the newer version of the ode wrapper, and i passed values in for x and z, i reallized that ode swapped them. in other words, x became z and z became x. thats really fustrating when you get used to a certain 3d axis. Quirky bugs i cant remember all of them but one i do remember is when bouncing a cube on a plane, part of the cube always rest under the plane to the point where as if you have the plane on alpha you can see it. In other words it wouldnt look realistic if part of your car tire sat under the plane if you get where im coming from. Thats not really quirky, perhaps if i played around with it more i might get a solution, but i played around with it alot and never solved the problem.


Wayne(Posted 2004) [#7]
I looked at the code for bouncing cube on a plane, and yes the example shows the cube in the plane, but..

After taking a quick look it became obvious what the problem was.

This creates 1x1 cube
g\geom = ODE_dCreateBox(space, 1, 1, 1, 1)

This create 2x2 ( -1 to 1 )
g\mesh = CreateCube()

Blitz cube is -1 to 1 which is two units not one!
so change:
g\geom = ODE_dCreateBox(space, 1, 1, 1, 1)

to this:
g\geom = ODE_dCreateBox(space, 2, 2, 2, 1)
corrects the cube size.

and changing the drop height makes it work better so
ODE_dBodySetPosition(g\body, 0, 10, 0)

to this:
ODE_dBodySetPosition(g\body, 0, 20, 0)

The cube will now bounce happily on top of the plane.

The sample program was doing exactly what it was told.
8)

What command(s) did you say swapped x,z ??
cheers


Clarks(Posted 2004) [#8]
I reviewed the code, and you are right.

but what about the swapping of x and z values.


Wayne(Posted 2004) [#9]
Many ODE commands use x,y,z any chance you can narrow down the list ?


Tom(Posted 2004) [#10]
The x/z swapping was a pain back with the old wrapper. I did mention this in another thread and I think Arkon has it on his list of things to do, but you'd have to confirm that with him.

/he who has X,Y,Z headspace!


Wayne(Posted 2004) [#11]
What ode command ?
What thread ?


Clarks(Posted 2004) [#12]
I'll try and narrow down the list.

ode_dbodyvectorfromworldx()
ode_dbodyvectorfromworldz()

ode_dbodygetlinearvelx()
ode_dbodygetlinearvelz()

ode_dbodycreatebox()
ode_dbodysetposition()