Const FPS=90
Type vector
	Field x#
	Field y#
	Field z#
End Type

Global CProd.vector=New vector

Type csys
  Field cx#,cy#,cz#
  Field mx#,my#,sps
End Type

TOKSIM_CreateSimulator(0,-9.9,0)
TOKSIM_SetMaterial 0,0.0,0.6

Global ColBank=CreateBank(104 * 10)
TOKSIM_SetCInfoBank ColBank ; банк коллизии
TOKSIM_SetCollisionResponse -1,1,3 ; импульс и обратная связь 
TOKSIM_SetCollisionResponse -1,2,0 ; без импульса или обратной связи

CamTargetPivot=CreatePivot()

Global speed#=0 ; ск. по умолчанию

GripFactor#=0.2 ; показатель тр.

Global Body_RB=TOKRB_Create()
Global RR_Spr_RB=TOKRB_Create(); создать объект
Global RL_Spr_RB=TOKRB_Create()
Global FR_Spr_RB=TOKRB_Create()
Global FL_Spr_RB=TOKRB_Create()

Global FR_Wheel=LoadMesh("Data\Models\Truck_wheel.b3d")
ScaleMesh FR_Wheel,0.3,0.3,0.3
Global FL_Wheel=LoadMesh("Data\Models\Truck_wheel.b3d")
ScaleMesh FL_Wheel,0.3,0.3,0.3
Global RR_Wheel=LoadMesh("Data\Models\Truck_wheel.b3d")
ScaleMesh RR_Wheel,0.3,0.3,0.3
Global RL_Wheel=LoadMesh("Data\Models\Truck_wheel.b3d")
ScaleMesh RL_Wheel,0.3,0.3,0.3

Function Tokamak_Prepare()

; УСТАНОВКИ
;ScaleMesh PlayerMesh,2.0,1.0,3.0
TOKRB_AddBox Body_RB,2.0,1.0,3.0
TOKRB_SetPosition Body_RB,0,5,0
TOKRB_SetMass Body_RB,1.0 ; установка массы
TOKRB_SetBoxInertiaTensor Body_RB,2.0,1.0,3.0,1.0 ; установка инерции 
;TOKRB_SetAngularDamping Body_RB,0.01 ; установка углового ослабления
;TOKRB_SetLinearDamping Body_RB,0.001 ; установка линейного ослабления
ExtraWeight=TOKRB_Create(); создание дополнительного веса
TOKRB_SetCollisionID ExtraWeight,2 ; установить коллизию дополнительного веса
TOKRB_SetMass ExtraWeight,0.4 ; установить массу дополнительного веса 
TOKRB_SetSphereInertiaTensor ExtraWeight,0.5,0.4 ; установить сферическую инерцию дополнительного веса
TOKRB_SetPosition ExtraWeight,0,5,1.5 ; установить позицию дополнительного веса
ConnJoint=TOKJOINT_Create(2,ExtraWeight,Body_RB); создание шарнира
TOKJOINT_SetType ConnJoint,1 ; установить тип шарнира
TOKJOINT_SetPositionAndRotationWorld ConnJoint,0,5.0,1.5,0,0,0 ; установить позтцию и вращение шарнира
TOKJOINT_Enable ConnJoint,True ; позволить шарнир

 ; УСТАНОВКИ ДЛЯ ЗАДНЕГО ПРАВОГО КОЛЕСА
;RR_Spr_Mesh=CreateSphere()
;RR_Spr_Mesh=LoadMesh("c:\code\truck_wheel.b3d")
;ScaleMesh RR_Spr_Mesh,0.3,0.3,0.3
;EntityColor RR_Spr_Mesh,Rnd(100,230),Rnd(100,230),Rnd(100,230)
TOKRB_AddSphere RR_Spr_RB,1.0 ; добавить сферу к колесу
TOKRB_SetPosition RR_Spr_RB,1.2,3.0,-1.50 ; установить позицию колеса
TOKRB_SetMass RR_Spr_RB,1.0 ; установить массу колеса
TOKRB_SetSphereInertiaTensor RR_Spr_RB,1.0,1.0 ; установить сферическую инерцию
TOKRB_SetCollisionID RR_Spr_RB,1 ; установить коллизию колеса
RR_SprJoint=TOKJOINT_Create(2,RR_Spr_RB,Body_RB); установить шарнир колеса
TOKJOINT_SetType RR_SprJoint,4 ; установить тип шарнира(амортизация)
TOKJOINT_SetPositionAndRotationWorld RR_SprJoint,1.2,3.0,-1.50,0,0,0 ; установить позтцию и вращение(шарнира)
TOKJOINT_Enable RR_SprJoint,True

 ; УСТАНОВКИ ДЛЯ ЗАДНЕГО ЛЕВОГО КОЛЕСА
;RL_Spr_Mesh=CreateSphere()
;ScaleMesh RL_Spr_Mesh,0.5,0.5,0.5
;EntityColor RL_Spr_Mesh,Rnd(100,230),Rnd(100,230),Rnd(100,230)
TOKRB_AddSphere RL_Spr_RB,1.0
TOKRB_SetPosition RL_Spr_RB,-1.2,3.0,-1.5
TOKRB_SetMass RL_Spr_RB,1.0
TOKRB_SetSphereInertiaTensor RL_Spr_RB,1.0,1.0
TOKRB_SetCollisionID RL_Spr_RB,1
RL_SprJoint=TOKJOINT_Create(2,RL_Spr_RB,Body_RB)
TOKJOINT_SetType RL_SprJoint,4
TOKJOINT_SetPositionAndRotationWorld RL_SprJoint,-1.2,3.0,-1.5,0,0,0 
TOKJOINT_Enable RL_SprJoint,True

; УСТАНОВКИ ДЛЯ ПЕРЕДНЕГО ПРАВОГО КОЛЕСА
;FR_Spr_Mesh=CreateSphere()
;ScaleMesh FR_Spr_Mesh,0.5,0.5,0.5
;EntityColor FR_Spr_Mesh,Rnd(100,230),Rnd(100,230),Rnd(100,230)
TOKRB_AddSphere FR_Spr_RB,1.0
TOKRB_SetPosition FR_Spr_RB,1.2,3.0,1.65
TOKRB_SetMass FR_Spr_RB,1.0
TOKRB_SetSphereInertiaTensor FR_Spr_RB,1.0,1.0
TOKRB_SetCollisionID FR_Spr_RB,1 
FR_SprJoint=TOKJOINT_Create(2,FR_Spr_RB,Body_RB)
TOKJOINT_SetType FR_SprJoint,4 
TOKJOINT_SetPositionAndRotationWorld FR_SprJoint,1.2,3.0,1.65,0,0,0 
TOKJOINT_Enable FR_SprJoint,True

; УСТАНОВКИ ДЛЯ ПЕРЕДНЕГО ЛЕВОГО КОЛЕСА
;FL_Spr_Mesh=CreateSphere()
;ScaleMesh FL_Spr_Mesh,0.5,0.5,0.5
;EntityColor FL_Spr_Mesh,Rnd(100,230),Rnd(100,230),Rnd(100,230)
TOKRB_AddSphere FL_Spr_RB,1.0
TOKRB_SetPosition FL_Spr_RB,-1.2,3.0,1.65
TOKRB_SetMass FL_Spr_RB,1.0
TOKRB_SetSphereInertiaTensor FL_Spr_RB,1.0,1.0
TOKRB_SetCollisionID FL_Spr_RB,1 
FL_SprJoint=TOKJOINT_Create(2,FL_Spr_RB,Body_RB)
TOKJOINT_SetType FL_SprJoint,4
TOKJOINT_SetPositionAndRotationWorld FL_SprJoint,-1.2,3.0,1.65,0,0,0 
TOKJOINT_Enable FL_SprJoint,True

;HideEntity FR_Wheel :  HideEntity FL_Wheel
;HideEntity RR_Spr_Mesh :  HideEntity RL_Spr_Mesh :  HideEntity FR_Spr_Mesh :  HideEntity FL_Spr_Mesh

CamPoint=TOKRB_Create()
TOKRB_SetMass CamPoint,1.0
TOKRB_SetSphereInertiaTensor FR_Spr_RB,1.0,1.0 ; установка иерции
TOKRB_SetLinearDamping CamPoint,0.01 ; установка линейного трения

End Function


Function UpdateSpring(px#,py#,pz#,Wheel_RB,Stiffness#,Damping#,SpringLength#)
TFormPoint px#,py#,pz#,PlayerMesh,0
wpx#=TFormedX#()
wpy#=TFormedY#()
wpz#=TFormedZ#()

diffX#=wpx# - TOKRB_GetX#(Wheel_RB)
diffY#=wpy# - TOKRB_GetY#(Wheel_RB)
diffZ#=wpz# - TOKRB_GetZ#(Wheel_RB)
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#)

;Vx#=TOKRB_GetVelocityAtPointX#(Body_RB,TFormedX#(),TFormedY#(),TFormedZ#())-TOKRB_GetVelocityX#(Wheel_RB)
;Vy#=TOKRB_GetVelocityAtPointY#(Body_RB,TFormedX#(),TFormedY#(),TFormedZ#())-TOKRB_GetVelocityY#(Wheel_RB)
;Vz#=TOKRB_GetVelocityAtPointZ#(Body_RB,TFormedX#(),TFormedY#(),TFormedZ#())-TOKRB_GetVelocityZ#(Wheel_RB)
GetPointVel Body_RB,wpx#,wpy#,wpz#
Vx#=CProd\x#-TOKRB_GetVelocityX#(Wheel_RB)
Vy#=CProd\y#-TOKRB_GetVelocityY#(Wheel_RB)
Vz#=CProd\z#-TOKRB_GetVelocityZ#(Wheel_RB)

speed#=DotProduct#(nx#,ny#,nz#,Vx#,Vy#,Vz#)
;damperForce#=damperForce#*u#
dampX#=nx#*speed#*Damping#
dampY#=ny#*speed#*Damping#
dampZ#=nz#*speed#*Damping#

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

TOKRB_ApplyImpulse2 Body_RB,-fx#*timestep#,-fy#*timestep#,-fz#*timestep#,wpx#,wpy#,wpz#
TOKRB_ApplyImpulse Wheel_RB,fx#*timestep#,fy#*timestep#,fz#*timestep#

End Function

Function PlaceMesh(Mesh,RB)
  PositionEntity Mesh,TOKRB_GetX#(RB),TOKRB_GetY#(RB),TOKRB_GetZ#(RB)
  RotateEntity Mesh,TOKRB_GetPitch#(RB),TOKRB_GetYaw#(RB),TOKRB_GetRoll#(RB),False
End Function

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

Function CrossProduct(x1#,y1#,z1#,x2#,y2#,z2#)
	CProd\x# =(y1#*z2#)-(z1#*y2#)
	CProd\y# =(z1#*x2#)-(x1#*z2#)
	CProd\z# =(x1#*y2#)-(y1#*x2#)
End Function

Function GetPointVel(RB,px#,py#,pz#)
 vx#=TOKRB_GetVelocityX#(RB)
 vy#=TOKRB_GetVelocityY#(RB)
 vz#=TOKRB_GetVelocityZ#(RB)
 avx#=TOKRB_GetAngularVelocityX#(RB)
 avy#=TOKRB_GetAngularVelocityY#(RB)
 avz#=TOKRB_GetAngularVelocityZ#(RB)
 CrossProduct avx#,avy#,avz#,px#-TOKRB_GetX#(RB),py#-TOKRB_GetY#(RB),pz#-TOKRB_GetZ#(RB)
 CProd\x#=CProd\x#+vx#
 CProd\y#=CProd\y#+vy#
 CProd\z#=CProd\z#+vz#
End Function

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)

 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

 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	
  PokeInt triangles,offsett+16,0
  PokeInt triangles,offsett+20,0
  offsett=offsett+24
 Next
 Next

 TOKSIM_SetStaticMesh vertices,ttlvert,triangles,ttltris

 FreeBank vertices
 FreeBank triangles
End Function

Function RemoveComponent(x1#,y1#,z1#,x2#,y2#,z2#)
 tmpdot#=DotProduct#(x1#,y1#,z1#,x2#,y2#,z2#)
 CProd\x#=x1#-x2#*tmpdot#
 CProd\y#=y1#-y2#*tmpdot#
 CProd\z#=z1#-z2#*tmpdot#
End Function

Function GripFactor#(Vel#)
	If Vel#>= 0.0 And Vel<3.0 Then
	GripFactor#=0.50
	EndIf
	If Vel#>= 3.0 And Vel<6.0 Then
	GripFactor#=0.95
	EndIf
	If Vel#>= 6.0 And Vel<10.0 Then
	GripFactor#=0.97
	EndIf
	If Vel#>= 10.0 And Vel<15.0 Then
	GripFactor#=0.98
	EndIf
	If Vel#>= 15.0 Then
	GripFactor#=0.99
	EndIf
	Return 0.999
	Return GripFactor#
End Function

Function UpdateCamera(Camera,Target,Rate#)

CamX#=EntityX#(Camera)
CamY#=EntityY#(Camera)
CamZ#=EntityZ#(Camera)
DiffX#=EntityX#(Target)-CamX#
DiffY#=EntityY#(Target)-CamY#
DiffZ#=EntityZ#(Target)-CamZ#
TranslateEntity Camera,DiffX#*Rate#,DiffY#*Rate#,DiffZ#*Rate#

End Function

;~IDEal Editor Parameters:
;~F#AC#B1#B6#BC#C9#F3#FA#10E
;~C#Blitz3D