tokamak physics thread 3:

Blitz3D Forums/Blitz3D Programming/tokamak physics thread 3:

try this one guys:

make sure you have Pongo's files in the same dir...
("track_fence.b3d") & ("mud_infield.b3d")



I put some interesting variables right at the top of the code

Press f1 for a 3rd person chase cam...
f2 changes the distance

; Thanks to Pongo for the Racetrack and Carmodel.

; play around with these values to tweak

chassi_mass# = 30
chassis_linear_damping# = 0.0001
chassis_angular_damping# = 0.1
wheel_linear_damping# = 0.0001
wheel_angular_damping# = 0.02
wheelmass# = 10
accel# = .1
top_torque# = 80
; ***************************************

Const FPS=90

Graphics3D 1024,768
SetBuffer BackBuffer()

WireFrame False

Global TOKS

Dim obj(100)
Dim rb(100)
camdistance# = 12

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

TOKSIM_CreateSimulator(0,-15,0)

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

cubetex = create_cube_texture()

For columns = 1 To 4
	For rows = 1 To 4
	
		create_TOK_BOX(3,3,3,(columns*4)-55,(rows*3)+0.1,26,0.001,0.01,2.5,3,3,3,2.5,cubetex,"car")
	Next
Next


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


infield=LoadMesh("mud_infield.b3d")
ScaleMesh infield,0.25,0.25,0.25


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

AddMesh terrain,infield
FreeEntity terrain

MakeTokCollider(infield)


;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,10,100
 
;ABGround = TOKAB_Create()
;geom = TOKAB_AddBox(ABGround,300,10,300)
;TOKGEOM_SetMaterialIndex geom,2
;TOKAB_SetPosition ABGround,0,-5,0

Jump = CreateCube()
ScaleMesh Jump,0.5,0.5,0.5
ScaleEntity Jump,15,15,15 
PositionEntity Jump,-45,-4.5,10
RotateEntity Jump,5,-90,-15
EntityColor Jump,30,130,30
 
ABJump = TOKAB_Create()
geom = TOKAB_AddBox(ABJump,15,15,15)
TOKGEOM_SetMaterialIndex geom,2
TOKAB_SetPosition ABJump,-45,-4.5,10
TOKAB_SetRotation ABJump,5,-90,-15

YOffset#=2.0

;Build Chassi

chassi_mass# = 40
chassis_linear_damping# = 0.0001
chassis_angular_damping# = 0.02

wheel_linear_damping# = 0.0001
wheel_angular_damping# = 0.02
wheelmass# = 2.0



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_pivot = CreatePivot(chassi_mesh)
MoveEntity chassi_pivot,0,0,-camdistance#

Chassi_RB = TOKRB_Create()
TOKRB_AddBox Chassi_RB,2.0,1.3,4.0
TOKRB_SetMass Chassi_RB,chassi_mass#
TOKRB_SetBoxInertiaTensor Chassi_RB,1.5,2.0,3.0,chassi_mass#
TOKRB_SetLinearDamping Chassi_RB,chassis_linear_damping#
TOKRB_SetAngularDamping Chassi_RB,chassis_angular_damping#
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,wheelmass#
TOKRB_SetSphereInertiaTensor RLWheel_RB,1.2,wheelmass#

TOKRB_SetPosition RLWheel_RB,-1.2,-0.7+YOffset#,-1.5 
TOKRB_SetLinearDamping RLWheel_RB,wheel_linear_damping#
TOKRB_SetAngularDamping RLWheel_RB,wheel_angular_damping#

;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,wheelmass#
TOKRB_SetSphereInertiaTensor RRWheel_RB,1.2,wheelmass#

TOKRB_SetPosition RRWheel_RB,1.2,-0.7+YOffset#,-1.5 
TOKRB_SetLinearDamping RRWheel_RB,wheel_linear_damping#
TOKRB_SetAngularDamping RRWheel_RB,wheel_angular_damping#

;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,wheelmass#
TOKRB_SetSphereInertiaTensor FLWheel_RB,1.2,wheelmass#

TOKRB_SetPosition FLWheel_RB,-1.2,-0.7+YOffset#,1.7 
TOKRB_SetLinearDamping FLWheel_RB,wheel_linear_damping#
TOKRB_SetAngularDamping FLWheel_RB,wheel_angular_damping#
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,wheelmass#
TOKRB_SetSphereInertiaTensor FRWheel_RB,1.2,wheelmass#

TOKRB_SetPosition FRWheel_RB,1.2,-0.7+YOffset#,1.7 
TOKRB_SetLinearDamping FRWheel_RB,wheel_linear_damping#
TOKRB_SetAngularDamping FRWheel_RB,wheel_angular_damping#
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()
PositionEntity light,7,15,-5
PointEntity light,Centerpivot

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

temppivot=CreatePivot()

; 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

; Steering

  If KeyDown(203) And SteeringAngle#<20 Then
   SteeringAngle#=SteeringAngle#+(((top_torque+9)-torque#) / 20)
  ElseIf KeyDown(205) And SteeringAngle#>-20 Then
   SteeringAngle#=SteeringAngle#-(((top_torque+9)-torque#) / 20)

  Else
   If SteeringAngle#>0 Then
     SteeringAngle#=SteeringAngle#*0.97
   ElseIf SteeringAngle#<0 Then
     SteeringAngle#=SteeringAngle#*0.97
   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 KeyDown(200) Then
  Torque#=torque# + accel#
	If torque# < 0 Then torque# = 0
	If torque# > top_torque# Then torque# = top_torque#
 ElseIf KeyDown(208) Then
  Torque#=torque - accel#
	If torque# < -40 Then torque# = -40
	If torque# > 0 Then torque# = 0

 Else
  Torque#=Torque#*0.997
 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,1500,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

camdist# = 2.0
camhgt# = 4
camspd# = 20

If KeyDown(60)
	cam1 = (1 - cam1)
	If cam1 = 0
		MoveEntity chassi_pivot,0,0,10
		camspd# = 30
	EndIf
	If cam1 = 1
		MoveEntity chassi_pivot,0,0,-10
		camspd# = 80

	EndIf

EndIf
 
If KeyDown(59) 
 freecam = (1 - freecam)
	Repeat : Until KeyDown(59) = 0
EndIf	
If freecam = 0
		PointEntity camera,Chassi_Mesh
EndIf
If freecam = 1
		smoothcam(camera,chassi_pivot,chassi_mesh,camspd#,camhgt#)
EndIf
For i=1 To TOKS
		 ;If TOKRB_IsIdle(rb(i)) Then
		 ; EntityAlpha obj(i),0.5
		 ;Else
		 ; EntityAlpha obj(i),1.0
		 ; TOKRB_Active(rb(i),True)
		
		 ;EndIf
		 
		 If EntityName(obj(i)) = "ball"
			; do stuff
		 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

	RenderWorld tween
	
Color 0,0,0 : Rect 0,0,200,90
Color 255,255,0
Text 0,0,"Physics Time:"+TOKSIM_GetPhysicsTime()*1000.0+ " ms"
Text 0,10,"Render Time:"+Str(elapsed)+ " ms"
Text 0,20,"Left:  "+Str(LWheelBoost#)
Text 0,30,"Right:"+Str(RWheelBoost#)
Text 0,40,"torque:"+Str(torque#)
Text 0,50,"cam y :"+Str(EntityY(camera))

If freecam = 0 Then nextcam$ = "Chase Cam"
If freecam = 1 Then nextcam$ = "Fixed Cam"
Text 0,60,"F1 => "+nextcam$
	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

Function smoothcam(cam,pivot,target,camspeed#,camheight#)


	curx#=EntityX(cam)
	curz#=EntityZ(cam)
	cury#=EntityY(cam)
	destx#=EntityX(pivot,True)
	destz#=EntityZ(pivot,True)
	desty#=EntityY(pivot,True)+camheight#

	
		curx#=curx#+((destx#-curx#)/camspeed#);+ camdistance#
		curz#=curz#+((destz#-curz#)/camspeed#);+ camdistance#
		cury#=cury#+((desty#-cury#)/camspeed#/3);+ camdistance#

	;cury#=EntityY(target)+camheight#

	PositionEntity cam,curx#,cury,curz#
	
	PointEntity cam,target
End Function

Function create_TOK_BOX(xs#,ys#,zs#,xp#,yp#,zp#,damp#,angdamp#,mass#,iw#,ih#,id#,imass#,texture,name$)

			TOKS = TOKS+1
			;If TOKS>ENTS 
				;TOKS = ENTS
				;Return
			;EndIf
			obj(TOKS) = CreateCube()
			NameEntity obj(toks),name$
			ScaleEntity obj(TOKS),xs#/2,ys#/2,zs#/2
			EntityPickMode obj(TOKS),2
			EntityTexture obj(TOKS),texture
			rb(TOKS) = TOKRB_Create()
			TOKRB_SetSleepingParameter(rb(TOKS),-100)
			TOKRB_AddBox rb(TOKS),xs#,ys#,zs#
			TOKRB_SetPosition(rb(TOKS),xp#,yp#,zp#)
			TOKRB_SetLinearDamping rb(TOKS),damp#
			TOKRB_SetAngularDamping rb(TOKS),angdamp# 

			TOKRB_SetMass rb(TOKS),mass#
			TOKRB_SetBoxInertiaTensor rb(TOKS),iw#,ih#,id#,imass# 
			
			

End Function

Function create_cube_texture()
	tex = CreateTexture(64,64)
	ScaleTexture tex,.5,.5
	SetBuffer TextureBuffer(tex)
		Color 20,20,200
		Rect 0,0,64,64
		Color 255,255,255
		Rect 0,0,32,32
		Rect 32,32,64,64
		Color 0,0,0
		Rect 0,0,64,64,0
	SetBuffer BackBuffer()
	Return tex
	

End Function