tokamak physics thread 3:

Blitz3D Forums/Blitz3D Programming/tokamak physics thread 3:

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.

cool,... you beat me to it,... I guess I'll post this anyways,... sorry to fill the thread with this stuff, but I think it's working out pretty neat and wanted to share.

By the way, I race RC cars, and that is heavily influencing my adjustments here. Might explain why all my stuff is moving at faster speeds. I added Joystick control into this version (might need to tweak for your own joystick) I have one that is like an RC controller with trigger for gas/brake and a small wheel,... feels almost perfect with that.

As before, I take no credit for the code,... this is just a hack cut and paste from some other demos with some parameter changes. This version does not require the extra files either, so it will work with the ones included in the .4 wrapper.


; Thanks to Pongo for the Racetrack and Carmodel.

Const FPS=90

Graphics3D 1024,768
SetBuffer BackBuffer()

WireFrame False

camera = CreateCamera()
PositionEntity camera,70,30,0
CameraZoom camera,2

TOKSIM_CreateSimulator(0,-45,0)

TOKSIM_SetMaterial 1,30.0,12.0 
TOKSIM_SetMaterial 2,10.0,10.0 

;fence=LoadMesh("track_fence.b3d")
;ScaleMesh fence,0.25,0.25,0.25

Terrain = LoadMesh("oval_racetrack.b3d")
ScaleMesh Terrain,0.25,0.25,0.25

MakeTokCollider(terrain)

Ground = CreateCube()
ScaleMesh Ground,0.5,0.5,0.5
ScaleEntity Ground,300,10,300 
PositionEntity Ground,0,-5,0
EntityColor Ground,10,100,10
 
ABGround = TOKAB_Create()
geom = TOKAB_AddBox(ABGround,300,10,300)
TOKGEOM_SetMaterialIndex geom,2
TOKAB_SetPosition ABGround,0,-5,0

;::::::::::::::::::::::::::: added this block from other demo
Global ents= 40

Dim obj(ents)
Dim rb(ents)

For i=1 To ents ; entity count
rot#=rot#+8.2

 ; change the line below to 1,3 to include "cylinders".
 ; I render them as cylinders but the rigidbody is really a capsule, so
 ; the bodies will behave like capsules and not like cylinders.

 nr=Rnd(1,3)		
 If nr=1 Then
  obj(i) = CreateCube()
  ScaleEntity obj(i),1,1,1 
  rb(i) = TOKRB_Create()
  TOKRB_AddBox rb(i),2.0,2.0,2.0
  TOKRB_SetPosition rb(i),0,i*2,0
  TOKRB_SetLinearDamping rb(i),0.001
  TOKRB_SetAngularDamping rb(i),0.02 	
  TOKRB_SetMass rb(i),10.0
  TOKRB_SetBoxInertiaTensor rb(i),2.0,2.0,2.0,2.0  
 ElseIf nr=2 
  obj(i) = CreateSphere()
  rb(i) = TOKRB_Create()
  TOKRB_AddSphere rb(i),2
  TOKRB_SetPosition rb(i),0,i*2,0
  TOKRB_SetLinearDamping rb(i),0.001 
  TOKRB_SetAngularDamping rb(i),0.02 
  TOKRB_SetMass rb(i),3.0
  TOKRB_SetSphereInertiaTensor rb(i),2,1.5 
 ElseIf nr=3 
  obj(i) = CreateCylinder()
  rb(i) = TOKRB_Create()
  TOKRB_AddCylinder rb(i),2.0,1.0
  TOKRB_SetPosition rb(i),0,i*2,0
  TOKRB_SetLinearDamping rb(i),0.001 
  TOKRB_SetAngularDamping rb(i),0.01 
  TOKRB_SetMass rb(i),2.0
  TOKRB_SetCylinderInertiaTensor rb(i),2.0,1.0,2.0  
 EndIf

 EntityColor obj(i),Rnd(100,230),Rnd(100,230),Rnd(100,230) 

Next

;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Jump = CreateCube()
ScaleMesh Jump,0.5,0.5,0.5
ScaleEntity Jump,10,10,10 
PositionEntity Jump,0,-4.5,20
RotateEntity Jump,0,0,-25
EntityColor Jump,30,130,30
 
ABJump = TOKAB_Create()
geom = TOKAB_AddBox(ABJump,10,10,10)
TOKGEOM_SetMaterialIndex geom,2
TOKAB_SetPosition ABJump,0,-4.5,20
TOKAB_SetRotation ABJump,0,0,-25

YOffset#=2.0

;Build Chassi
Chassi_Mesh = LoadMesh("truck_body.b3d")
ScaleMesh Chassi_Mesh,0.3,0.3,0.3
RotateMesh Chassi_Mesh,0,180,0 
PositionMesh Chassi_Mesh,0,-1.5,0 

Chassi_RB = TOKRB_Create()
TOKRB_AddBox Chassi_RB,2.0,1.3,4.0
TOKRB_SetMass Chassi_RB,40.0
TOKRB_SetBoxInertiaTensor Chassi_RB,1.5,1.0,3.0,50.0 
TOKRB_SetLinearDamping Chassi_RB,0.003
TOKRB_SetAngularDamping Chassi_RB,0.005
TOKRB_SetPosition Chassi_RB,0,0.0+YOffset#,0

;Build RearLeft Wheel
RLWheel_Mesh = LoadMesh("truck_wheel.b3d")
ScaleMesh RLWheel_Mesh,0.32,0.32,0.32
RotateMesh RLWheel_Mesh,0,180,0 

RLWheel_RB = TOKRB_Create()
geom = TOKRB_AddSphere(RLWheel_RB,1.1)
TOKGEOM_SetMaterialIndex geom,1
TOKRB_SetMass RLWheel_RB,3.0
TOKRB_SetSphereInertiaTensor RLWheel_RB,1.2,1.5 
TOKRB_SetPosition RLWheel_RB,-1.2,-0.7+YOffset#,-1.5 
TOKRB_SetLinearDamping RLWheel_RB,0.001
TOKRB_SetAngularDamping RLWheel_RB,0.02

;Build RearRight Wheel
RRWheel_Mesh = LoadMesh("truck_wheel.b3d")
ScaleMesh RRWheel_Mesh,0.32,0.32,0.32
RotateMesh RRWheel_Mesh,0,180,0 

RRWheel_RB = TOKRB_Create()
geom=TOKRB_AddSphere(RRWheel_RB,1.1)
TOKGEOM_SetMaterialIndex geom,1
TOKRB_SetMass RRWheel_RB,3.0
TOKRB_SetSphereInertiaTensor RRWheel_RB,1.2,1.5 
TOKRB_SetPosition RRWheel_RB,1.2,-0.7+YOffset#,-1.5 
TOKRB_SetLinearDamping RRWheel_RB,0.001
TOKRB_SetAngularDamping RRWheel_RB,0.02

;Build FrontLeft Wheel
FLWheel_Mesh = LoadMesh("truck_wheel.b3d")
ScaleMesh FLWheel_Mesh,0.32,0.32,0.32
RotateMesh FLWheel_Mesh,0,180,0 

FLWheel_RB = TOKRB_Create()
geom=TOKRB_AddSphere(FLWheel_RB,1.1)
TOKGEOM_SetMaterialIndex geom,1
TOKRB_SetMass FLWheel_RB,3.0
TOKRB_SetSphereInertiaTensor FLWheel_RB,1.2,1.5 
TOKRB_SetPosition FLWheel_RB,-1.2,-0.7+YOffset#,1.7 
TOKRB_SetLinearDamping FLWheel_RB,0.001
TOKRB_SetAngularDamping FLWheel_RB,0.02
TOKRB_CollideConnected FLWheel_RB,False

;Build FrontRight Wheel
FRWheel_Mesh = LoadMesh("truck_wheel.b3d")
ScaleMesh FRWheel_Mesh,0.32,0.32,0.32
RotateMesh FRWheel_Mesh,0,180,0 

FRWheel_RB = TOKRB_Create()
geom=TOKRB_AddSphere(FRWheel_RB,1.1)
TOKGEOM_SetMaterialIndex geom,1
TOKRB_SetMass FRWheel_RB,3.0
TOKRB_SetSphereInertiaTensor FRWheel_RB,1.2,1.5 
TOKRB_SetPosition FRWheel_RB,1.2,-0.7+YOffset#,1.7 
TOKRB_SetLinearDamping FRWheel_RB,0.001
TOKRB_SetAngularDamping FRWheel_RB,0.02
TOKRB_CollideConnected FRWheel_RB,False

; Create Rear Axles
RLAxle = TOKJOINT_Create(2,RLWheel_RB,Chassi_RB)
TOKJOINT_SetType RLAxle,3 ; Hinge
TOKJOINT_SetPositionAndRotationWorld RLAxle,-1.2,-0.7+YOffset#,-1.5,0,0,90
TOKJOINT_Enable RLAxle,True 
TOKJOINT_SetDampingFactor RLAxle,0.5 
TOKJOINT_SetEpsilon RLAxle,0.0
TOKJOINT_SetIterations RLAxle,10  

RRAxle = TOKJOINT_Create(2,RRWheel_RB,Chassi_RB)
TOKJOINT_SetType RRAxle,3 ; Hinge
TOKJOINT_SetPositionAndRotationWorld RRAxle,1.2,-0.7+YOffset#,-1.5,0,0,90
TOKJOINT_Enable RRAxle,True 
TOKJOINT_SetDampingFactor RRAxle,0.5 
TOKJOINT_SetEpsilon RRAxle,0.0
TOKJOINT_SetIterations RRAxle,10  

; Create Steering Axles

FLJointConnector = TOKRB_Create()
TOKRB_SetPosition FLJointConnector,-1.2,-0.7+YOffset#,1.7
FLSteering = TOKJOINT_Create(2,FLJointConnector,Chassi_RB)
TOKJOINT_SetType FLSteering,3 ; Hinge
TOKJOINT_SetPositionAndRotationWorld FLSteering,-1.2,-0.7+YOffset#,1.7,0,0,0
TOKJOINT_Enable FLSteering,True 
TOKJOINT_SetLowerLimit FLSteering,-3.14*0.25 ; -45 Degrees
TOKJOINT_SetUpperLimit FLSteering,3.14*0.25 ; +45 Degrees
TOKJOINT_EnableLimit FLSteering,False 
;TOKJOINT_SetDampingFactor FLSteering,5.0
TOKJOINT_SetEpsilon FLSteering,0.0
TOKJOINT_SetIterations FLSteering,10  

FRJointConnector = TOKRB_Create()
TOKRB_SetPosition FRJointConnector,1.2,-0.7+YOffset#,1.7
FRSteering = TOKJOINT_Create(2,FRJointConnector,Chassi_RB)
TOKJOINT_SetType FRSteering,3 ; Hinge
TOKJOINT_SetPositionAndRotationWorld FRSteering,1.2,-0.7+YOffset#,1.7,0,0,0
TOKJOINT_Enable FRSteering,True 
TOKJOINT_SetLowerLimit FRSteering,-3.14*0.25 ; -45 Degrees
TOKJOINT_SetUpperLimit FRSteering,3.14*0.25 ; +45 Degrees
TOKJOINT_EnableLimit FRSteering,False
;TOKJOINT_SetDampingFactor FRSteering,5.0
TOKJOINT_SetEpsilon FRSteering,0.0
TOKJOINT_SetIterations FRSteering,10  

; Create Front Axles

FLAxle = TOKJOINT_Create(2,FLWheel_RB,FLJointConnector)
TOKJOINT_SetType FLAxle,3 ; Hinge
TOKJOINT_SetPositionAndRotationWorld FLAxle,-1.2,-0.7+YOffset#,1.7,0,0,90
TOKJOINT_Enable FLAxle,True 
TOKJOINT_SetDampingFactor FLAxle,0.5
TOKJOINT_SetEpsilon FLAxle,0.0
TOKJOINT_SetIterations FLAxle,10  

FRAxle = TOKJOINT_Create(2,FRWheel_RB,FRJointConnector)
TOKJOINT_SetType FRAxle,3 ; Hinge
TOKJOINT_SetPositionAndRotationWorld FRAxle,1.2,-0.7+YOffset#,1.7,0,0,90
TOKJOINT_Enable FRAxle,True 
TOKJOINT_SetDampingFactor FRAxle,0.5
TOKJOINT_SetEpsilon FRAxle,0.0
TOKJOINT_SetIterations FRAxle,10  

Centerpivot = CreatePivot()
light=CreateLight(2)
PositionEntity light,300,700,1000
PointEntity light,Centerpivot


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

temppivot=CreatePivot()

; MainLoop
While Not KeyHit(1)

; Not sure wether to put this here or inside the for/next above...
For i=1 To ents
; If TOKRB_IsIdle(rb(i)) Then
;  EntityAlpha obj(i),0.5
; Else
;  EntityAlpha obj(i),1.0
; EndIf
 PositionEntity obj(i),TOKRB_GetX#(rb(i)),TOKRB_GetY#(rb(i)),TOKRB_GetZ#(rb(i)) 
 RotateEntity obj(i),TOKRB_GetPitch#(rb(i)),TOKRB_GetYaw#(rb(i)),TOKRB_GetRoll#(rb(i)),False
Next

	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

; Steering

  If KeyDown(203) And SteeringAngle#<18 Then
   SteeringAngle#=SteeringAngle#+1
  ElseIf KeyDown(205) And SteeringAngle#>-18 Then
   SteeringAngle#=SteeringAngle#-1
  ElseIf JoyX()>.1 Or JoyX()<-.1 ;joystick control added by Pongo
	steeringangle#=-JoyX()*25

 Else
   If SteeringAngle#>0 Then
     SteeringAngle#=SteeringAngle#-0.5
   ElseIf SteeringAngle#<0 Then
     SteeringAngle#=SteeringAngle#+0.5
   EndIf
  EndIf

  RotateEntity temppivot,TOKJOINT_GetFrameBPitch(FLSteering),TOKJOINT_GetFrameBYaw(FLSteering),TOKJOINT_GetFrameBRoll(FLSteering)
  TurnEntity temppivot,0,SteeringAngle#,0,False   
  TOKRB_SetRotation FLJointConnector,EntityPitch(temppivot),EntityYaw(temppivot),EntityRoll(temppivot)
  RotateEntity temppivot,TOKJOINT_GetFrameBPitch(FRSteering),TOKJOINT_GetFrameBYaw(FRSteering),TOKJOINT_GetFrameBRoll(FRSteering)
  TurnEntity temppivot,0,SteeringAngle#,0,False   
  TOKRB_SetRotation FRJointConnector,EntityPitch(temppivot),EntityYaw(temppivot),EntityRoll(temppivot)

; Acceleration

 If JoyY()>.1 Or JoyY()<-.1 ;joystick control by Pongo
	torque#=-JoyY()*300
 ElseIf KeyDown(200) Or JoyDown(2) Then
  Torque#=300.0
 ElseIf KeyDown(208) Or JoyDown(1) Then
  Torque#=-300.0

 Else
  Torque#=Torque#*0.05
 EndIf



; Faking some kind of differential (For better steering)
   LWheelBoost#=1-SteeringAngle#*0.02
   RWheelBoost#=1+SteeringAngle#*0.02

  RotateEntity temppivot,TOKJOINT_GetFrameAPitch(RLAxle),TOKJOINT_GetFrameAYaw(RLAxle),TOKJOINT_GetFrameARoll(RLAxle)-90
  TFormVector Torque#*LWheelBoost#,0,0,temppivot,0  
  TOKRB_SetTorque RLWheel_RB,TFormedX#(),TFormedY#(),TFormedZ#()
  RotateEntity temppivot,TOKJOINT_GetFrameAPitch(RRAxle),TOKJOINT_GetFrameAYaw(RRAxle),TOKJOINT_GetFrameARoll(RRAxle)-90
  TFormVector Torque#*RWheelBoost#,0,0,temppivot,0  
  TOKRB_SetTorque RRWheel_RB,TFormedX#(),TFormedY#(),TFormedZ#()

  If KeyDown(57) Then
   TOKRB_SetForce2 Chassi_RB,0,2000,0,TOKRB_GetX#(Chassi_RB)+1,TOKRB_GetY#(Chassi_RB),TOKRB_GetZ#(Chassi_RB)
  Else
   TOKRB_SetForce2 Chassi_RB,0,0,0,0,0,0
  EndIf

	Next

 PositionEntity Chassi_Mesh,TOKRB_GetX(Chassi_RB),TOKRB_GetY(Chassi_RB),TOKRB_GetZ(Chassi_RB) 
 RotateEntity Chassi_Mesh,TOKRB_GetPitch(Chassi_RB),TOKRB_GetYaw(Chassi_RB),TOKRB_GetRoll(Chassi_RB),False
 PositionEntity RLWheel_Mesh,TOKRB_GetX(RLWheel_RB),TOKRB_GetY(RLWheel_RB),TOKRB_GetZ(RLWheel_RB) 
 RotateEntity RLWheel_Mesh,TOKRB_GetPitch(RLWheel_RB),TOKRB_GetYaw(RLWheel_RB),TOKRB_GetRoll(RLWheel_RB),False
 PositionEntity RRWheel_Mesh,TOKRB_GetX(RRLWheel_RB),TOKRB_GetY(RRWheel_RB),TOKRB_GetZ(RRWheel_RB) 
 RotateEntity RRWheel_Mesh,TOKRB_GetPitch(RRWheel_RB),TOKRB_GetYaw(RRWheel_RB),TOKRB_GetRoll(RRWheel_RB),False
 PositionEntity FLWheel_Mesh,TOKRB_GetX(FLWheel_RB),TOKRB_GetY(FLWheel_RB),TOKRB_GetZ(FLWheel_RB) 
 RotateEntity FLWheel_Mesh,TOKRB_GetPitch(FLWheel_RB),TOKRB_GetYaw(FLWheel_RB),TOKRB_GetRoll(FLWheel_RB),False
 PositionEntity FRWheel_Mesh,TOKRB_GetX(FRLWheel_RB),TOKRB_GetY(FRWheel_RB),TOKRB_GetZ(FRWheel_RB) 
 RotateEntity FRWheel_Mesh,TOKRB_GetPitch(FRWheel_RB),TOKRB_GetYaw(FRWheel_RB),TOKRB_GetRoll(FRWheel_RB),False

; RotateEntity camera,TOKRB_GetPitch(Chassi_RB),TOKRB_GetYaw(Chassi_RB),TOKRB_GetRoll(Chassi_RB),False
; PositionEntity camera,TOKRB_GetX(Chassi_RB),TOKRB_GetY(Chassi_RB),TOKRB_GetZ(Chassi_RB)
; MoveEntity camera,0,1,0.1
 PointEntity camera,Chassi_Mesh

	RenderWorld tween

Text 0,0,"Physics Time:"+TOKSIM_GetPhysicsTime()*1000.0+ " milliseconds"
Text 0,10,"Render Time:"+Str(elapsed)+ " milliseconds"
Text 0,20,"Left:  "+Str(LWheelBoost#)
Text 0,30,"Right:"+Str(RWheelBoost#)
	Flip False

Wend

TOKSIM_DestroySimulator()

End

Function MakeTokCollider(mesh)
 scount=CountSurfaces(mesh)
 For ind=1 To scount
  surface=GetSurface(mesh,ind)
  ttltris=ttltris+CountTriangles(surface)
  ttlvert=ttlvert+CountVertices(surface)
 Next
 vertices=CreateBank(16*ttlvert)
 triangles=CreateBank(24*ttltris)
 offsetv=0
 offsett=0
 For ind=1 To scount
  surface = GetSurface(mesh,ind)
  ctr=CountTriangles(surface)
  tric=tric+cvt
  cvt=CountVertices(surface)
  ;fill bank with vertices
  For v=0 To cvt-1
   PokeFloat vertices,offsetv,VertexX#(surface,v)
   PokeFloat vertices,offsetv+4,VertexY#(surface,v)
   PokeFloat vertices,offsetv+8,VertexZ#(surface,v)
   PokeFloat vertices,offsetv+12,0.0
   offsetv=offsetv+16
  Next
  ;fill bank with triangles
  For v=0 To ctr-1
   PokeInt triangles,offsett,tric+TriangleVertex(surface,v,0)
   PokeInt triangles,offsett+4,tric+TriangleVertex(surface,v,1)
   PokeInt triangles,offsett+8,tric+TriangleVertex(surface,v,2)
   PokeInt triangles,offsett+12,2	; Material ID
   PokeInt triangles,offsett+16,0
   PokeInt triangles,offsett+20,0
   offsett=offsett+24
  Next
 Next

 ;Hand over the terrain data to Tokamak
 TOKSIM_SetStaticMesh vertices,ttlvert,triangles,ttltris
 ; Now we can free the banks as Tokamak has copied all data
 FreeBank vertices
 FreeBank triangles
End Function