crappy code

// simple 4 wheel gound fallowing system , needs more optimisations .
// no max travel and bottoming out suspension systems , just crude wheel
// heights & chassis height
// chassis rotations are crude as well due to no bottoming systems.
// have fun .
// Truck is a dark matter mesh i have reworked for my purposes
// terrain is made by My PT3D tool .
// meshes formated / setup in my P3D for ease of use.
//some collision ids
TYPE_OBJECT = 1
//load the window
wnd = MakeWindowDX()
If Not wnd Then DebugError("Please update your DirectX")
SetWindowTitle wnd, "Nuclear Basic powered Terrain Crawler by Prime_8 "
cam = GetDefaultCamera()
vp = GetDefaultViewport()
PositionEnt cam, 0, 50, -50
PointEnt(cam , 0 ,0,0);
ResetEnt cam
Global light = GetDefaultLight()
//SetLightCastsShadows(light);
PositionEnt light, -50, 50, -50
ResetEnt light
Dim ray[4];
Dim meshWheel[4];
Dim meshWheelHub[4];
Dim meshWheelHeight#[4]; floats
Global wheelAxlelHeight# = 0.49 ;
Global AxelWidth# = 0.49 ;
Global AxelDist# = 0.49 ;
Global mesh=0;
Global hits = 0;
Global moveIt = 0;
Global dirSteer = 0 ;
//setup basic collisions`
SetCollisions TYPE_OBJECT, TYPE_OBJECT //objects collide with other objects
//setup ground plane // ground1a.n3d
file$ = "ground1a.n3d"
Global plane = LoadAnimEnt(file) ; // = MakePlane(100,100)
// AddEntBoxShape plane, 100, 0.001, 100 //this creates a collider shape in the same shape as the plane
AddEntMeshShape plane
SetEntShapeAsCharacter plane, False //turn off character mode
SetEntMass plane, 0.0 //once the mass of an entity is set it will be under the influence of physics
//(effectively infinite to prevent being knocked out of the way by the cubes)
SetEntGravity(plane, 0.0 ); // GetEntPosY(plane)+5
CollisionEnt plane, TYPE_OBJECT //make the plane into a collider object
lockmsg$ = "Locked"
viewlock = True ;
oneshot = 1;
//main loop (continues until escape key is pressed)
While Not KeyHit(1)
//update the framerate display
Print "FPS: "+GetFPS() + " Hits: " + hits;
If Not hidetext
Print ""
Print "Use the arrow keys to move and the mouse to steer"
Print "Use 'o' to load a new truck model"
Print "Use ASWD to drive model "
Print "Use 'b' to change the background color"
Print "Use 'r' to reset the view if you get lost"
Print "Use 'm' to mount/unmount cam to the truck"
Print "Use 'v' to lock and unlock the view - current mode: "+lockmsg
Print " when locked the cam tracks the truck if one is loaded "
Print "Use 'c' to togle shadows :: note, it kills terrain vert color."
Print "Use 'h' to show/hide this text"
Print ""
Print "Press '1' to enter windowed mode"
Print "Press '2' to enter fullscreen mode"
Print "Press '3' to enter fullscreen exclusive mode"
EndIf
//handle the 'o' key
If KeyHit(24) Or oneshot = 1
oneshot = 0;
file$ = "4x4Rig.n3d";
If Len(file)
mesh = LoadAnimEnt(file)
EndIf
If Not mesh Then Notice2("Could not load model", wnd)
//SetEntCastsShadows(mesh);
wheelAxlelHeight# = 0.49 ; // you may have to edit this for your mesh
meshWheel[0] = FindEnt(mesh, "Wheel FL");
meshWheel[1] = FindEnt(mesh, "Wheel FR");
meshWheel[2] = FindEnt(mesh, "Wheel BL");
meshWheel[3] = FindEnt(mesh, "Wheel BR");
meshWheelHub[0] = FindEnt(mesh, "hub FL");
meshWheelHub[1] = FindEnt(mesh, "hub FR");
meshWheelHub[2] = FindEnt(mesh, "hub RL");
meshWheelHub[3] = FindEnt(mesh, "hub RR");
loadres = meshWheel[0] + meshWheel[1] + meshWheel[2] + meshWheel[3] ;
If Not loadres Then Notice2("Could not locate model parts", wnd);
GetEntPos( meshWheel[0] , ax1# , ay1#, az1#, True );
GetEntPos( meshWheel[1] , ax2# , ay2#, az2#, True );
AxelWidth# = Abs( VecLength3D( ax1# - ax2# , ay1# - ay2#, az1# - az2# ) );
GetEntPos( meshWheel[0] , ax1# , ay1#, az1#, True );
GetEntPos( meshWheel[2] , ax2# , ay2#, az2#, True );
AxelDist# = Abs(VecLength3D( ax1# - ax2# , ay1# - ay2#, az1# - az2# ) );
Reset3DTweening
SetMousePos 100, 100, wnd
EndIf
//handle background color picker ('b' key)
If KeyHit(48)
bgcolor = MakeDialogColor2( bgcolor, wnd );
SetViewportColor vp, bgcolor
SetMousePos 100, 100, wnd
EndIf
//handle camera reset ('r' key)
If KeyHit(19)
PositionEnt cam, 0, 50, -50
RotateEnt cam, 0, 0, 0
ResetEnt cam
scale# = 1
If mesh Then ScaleEnt(mesh,scale,scale,scale)
EndIf
//handle camera reset ('c' key)
If KeyHit(46)
If mesh
If GetEntCastsShadows(mesh)
SetEntCastsShadows(mesh, 0 );
Else
SetEntCastsShadows(mesh, 1 );;
EndIf
If GetLightCastsShadows(light)
SetLightCastsShadows(light, 0 );
Else
SetLightCastsShadows(light, 1 );;
EndIf
SetEntUseVertexColor(mesh);
SetEntUseVertexColor(plane);
EndIf
EndIf
//handle camera reset ('m' key)
If KeyHit(50)
If mesh
If GetEntParent(cam) = mesh
SetEntParent(cam,0);
Else
SetEntParent(cam,mesh);
EndIf
EndIf
EndIf
//handle the hiding/showing of text ('h' key)
If KeyHit(35)
If hidetext
hidetext = False
Else
hidetext = True
EndIf
EndIf
//set window to windowed
If KeyHit(2) Then SetWindowFullscreen(wnd, False)
//set window to fullscreen standard
If KeyHit(3) Then SetWindowFullscreen(wnd, True, 0, 0, 0, 2)
//set window to fullscreen exclusive
If KeyHit(4) Then SetWindowFullscreen(wnd, True, 0, 0, 0, 1)
//if it's time to move the objects
If Do3DTweening()
If moveIt
fireRays();
moveIt = 0;
RotateEnt( meshWheelHub[0] ,0,0,0);
RotateEnt( meshWheelHub[1] ,0,0,0);
dirSteer = 0 ;
EndIf
//scale the mesh up 'a'
If KeyDown(30)
If mesh
//TurnEntLocal(mesh,0,-0.4,0);
TurnEntLocal(meshWheelHub[0], 0 , -20 ,0 );
TurnEntLocal(meshWheelHub[1], 0 , -20 ,0 );
dirSteer = 1 ;
moveIt = 1 ;
EndIf
EndIf
//handle the 'd' key
If KeyDown(32)
If mesh
//TurnEntLocal(mesh,0,0.4,0);
TurnEntLocal(meshWheelHub[0], 0 , 20 ,0 );
TurnEntLocal(meshWheelHub[1], 0 , 20 ,0 );
dirSteer = 2 ;
moveIt = 2 ;
EndIf
EndIf
//scale the mesh up 'w'
If KeyDown(17)
If mesh
MoveEnt(mesh,0,0,0.15);
If dirSteer = 1 ;
TurnEntLocal(mesh,0,-0.6,0);
EndIf
If dirSteer = 2 ;
TurnEntLocal(mesh,0,0.6,0);
EndIf
roll# = 5.0
TurnEntLocal(meshWheel[0], roll# , 0 , 0 );
TurnEntLocal(meshWheel[1], roll# , 0 , 0 );
TurnEntLocal(meshWheel[2], roll# , 0 , 0 );
TurnEntLocal(meshWheel[3], roll# , 0 , 0 );
moveIt = 1 ;
EndIf
EndIf
//handle the 's' key
If KeyDown(31)
If mesh
MoveEnt(mesh,0,0,-0.15);
If dirSteer = 1 ;
TurnEntLocal(mesh,0,0.6,0);
EndIf
If dirSteer = 2 ;
TurnEntLocal(mesh,0,-0.6,0);
EndIf
roll# = -5.0
TurnEntLocal(meshWheel[0], roll# , 0 , 0 );
TurnEntLocal(meshWheel[1], roll# , 0 , 0 );
TurnEntLocal(meshWheel[2], roll# , 0 , 0 );
TurnEntLocal(meshWheel[3], roll# , 0 , 0 );
moveIt = 1 ;
EndIf
EndIf
//if they press the "up" arrow
If KeyDown(200) Or KeyHit(200) Then MoveEnt(cam,0,0,1.5)
//if they press the "down" arrow
If KeyDown(208) Or KeyHit(208) Then MoveEnt(cam,0,0,-1.5)
//if they press the "left" arrow
If KeyDown(203) Or KeyHit(203) Then MoveEnt(cam,-1.5,0,0)
//if they press the "right" arrow
If KeyDown(205) Or KeyHit(205) Then MoveEnt(cam,1.5,0,0)
//handle animation speed up (+ key)
If KeyDown(13) Or KeyHit(13)
If mesh Then SetEntAnimSpeed( mesh, GetEntAnimSpeed(mesh)+0.05 )
EndIf
//handle animation slow down (- key)
If KeyDown(12) Or KeyHit(12)
If mesh Then SetEntAnimSpeed( mesh, GetEntAnimSpeed(mesh)-0.05 )
EndIf
//if we're in a view lock
If viewlock
If KeyHit(47) //if they hit the 'v' key
SetMousePos 100, 100, wnd //set the mouse position
viewlock = False //unset the view lock
lockmsg$ = "Unlocked"
EndIf
If mesh Then PointEnt(cam , GetEntPosX(mesh) , GetEntPosY(mesh) ,GetEntPosZ(mesh));
Else
//if they hit the 'v' key
If KeyHit(47)
lockmsg$ = "Locked"
viewlock = True //set the view lock
Else
If MouseMove()
//get the turning offsets
xOff# = (MouseX(wnd) - 100) * 0.20
yOff# = (MouseY(wnd) - 100) * 0.20
//turn the entity
TurnEnt cam, 0, xOff, 0 //turn the entity on y axis
TurnEntLocal cam, yOff, 0, 0 //turn the entity on x axis
//set the position
SetMousePos 100, 100, wnd //set the mouse position
EndIf
EndIf
EndIf
EndIf //endif - it's time to move the objects
Sync
Wend
End();
/* to remember order of wheels .
meshWheel[0] = FindEnt(mesh, "Wheel FL");
meshWheel[1] = FindEnt(mesh, "Wheel FR");
meshWheel[2] = FindEnt(mesh, "Wheel BL");
meshWheel[3] = FindEnt(mesh, "Wheel BR");
*/
Function fireRays()
// ray[4];
hits = hits +1 ;
If mesh
hits = 0 ;
wheelActive = 0;
meanRayHeight# = 0 ;
For wheelActive = 0 To 3
ray[wheelActive] = MakeRay( GetEntPosX(meshWheel[wheelActive]) , GetEntPosY(meshWheel[wheelActive])+1000, GetEntPosZ(meshWheel[wheelActive]), GetEntPosX(meshWheel[wheelActive]) , GetEntPosY(meshWheel[wheelActive])-1000, GetEntPosZ(meshWheel[wheelActive]), TYPE_OBJECT, 0 );
If GetRayHitCount( ray[wheelActive] )
meshWheelHeight#[wheelActive] = GetRayHitPosY( ray[wheelActive] , 0 ) ;
hits = hits +1 ;
EndIf
meanRayHeight# = meanRayHeight# + meshWheelHeight#[wheelActive];
// puts wheels at local terrain height
KillRay(ray[wheelActive] );
Next ;
meanRayHeight# = meanRayHeight# / 4.0;
// position the chassis at the average tire height + 1/4 axle height . note very accurate but looks ok .
PositionEnt(mesh , GetEntPosX(mesh) , meanRayHeight# + (wheelAxlelHeight#/4.0) , GetEntPosZ(mesh) ,1)
FAHD# = meshWheelHeight#[0] - meshWheelHeight#[1] ; // front axle
RAHD# = meshWheelHeight#[2] - meshWheelHeight#[3] ; // rear axle
MAHD# = (( FAHD# + RAHD# )/2.0 ) / AxelWidth# ;// mean left to right slope of veh
LRtheta# = ATan( -MAHD# );
FAHD# = meshWheelHeight#[0] - meshWheelHeight#[2] ; // left side
RAHD# = meshWheelHeight#[1] - meshWheelHeight#[3] ; // right side
MADD# = (( FAHD# + RAHD# )/2.0 ) / AxelDist# ; // mean fron to back slope of veh
FBtheta# = ATan( -MADD# );
curY# = GetEntRotY(mesh , True);
RotateEnt(mesh,FBtheta ,curY,LRtheta );
// meshWheelHub[0] stores safe xz coords for wheels .. the hub pivots attached to body .
// only need to change heights .
For wheelActive = 0 To 3
PositionEnt( meshWheel[wheelActive] , GetEntPosX(meshWheelHub[wheelActive]) , meshWheelHeight#[wheelActive] + wheelAxlelHeight# , GetEntPosZ(meshWheelHub[wheelActive]) , 1 );
Next
EndIf
EndFunction