This car hugs the ground coz it uses rays.
The car body angle code was shared by a top bloke by the name of Prime_8 about 8 or 9 years ago.
I was reminded by VN's recent terrain upload where the cam is moved around using a ray so I wanted to get this cool stuff working in AGKS.
Car model attribution....
https://md2.sitters-electronics.nl/models.htm
And I just noticed that you can drive off and back on the terrain.
// Project: Prime_8 car
// Created: 21-12-30
// the car angle code was shared by Prime_8 circa 2012
// car model attribution
// https://md2.sitters-electronics.nl/models.htm
SetErrorMode(2)
SetVSync(1)
width# = GetMaxDeviceWidth() // I had to use # to stabilise mouselook
height# = GetMaxDeviceHeight()
SetWindowSize(width#,height#,1)
SetVirtualResolution(width#,height#) // doesn't have to match the window
SetSyncRate(60,0) // 60fps
SetScissor(0,0,0,0) // use the maximum available screen space
UseNewDefaultFonts(1)
SetGenerateMipmaps(1)
SetSunActive(0)
SetAmbientColor(80,80,80)
setprintsize(height#/25)
SetSkyboxVisible(1)
SetSkyBoxHorizonColor(0,0,0)
terrain=CreateObjectFromHeightMap("landB4timeHM512_8bit.png",1000,50,1000,1,0)
terraintex=LoadImage("landB4time.png")
SetObjectImage(terrain,terraintex,1)
SetObjectPosition(terrain,-500,-50,-500)
SetObjectCollisionMode(terrain,1)
cam1=1
SetCameraRange(cam1,0.1,50000)
SetCameraposition(cam1,0,30,-200)
SetCameraRotation(cam1,0,0,0)
man1=CreateObjectsphere(2,6,12)
SetObjectposition(man1,0,30,-200)
setobjectvisible(man1,0)
jeep=loadobject("jeepbody.b3d")
jeeptex=loadimage("jeep_army.png")
setobjectscalepermanent(jeep,0.2,0.2,0.2)
setobjectimage(jeep,jeeptex,0)
setobjectcoloremissive(jeep,40,40,40)
FLwheel=loadobject("jeepwheel.b3d")
setobjectscalepermanent(FLwheel,0.2,0.2,0.2)
setobjectimage(FLwheel,jeeptex,0)
setobjectcoloremissive(FLwheel,40,40,40)
FLbox=createobjectbox(1,1,1)
fixobjecttoobject(FLbox,jeep)
setobjectposition(FLbox,-10,3,12)
fixobjecttoobject(FLwheel,FLbox)
setobjectposition(FLwheel,0,0,0)
setobjectvisible(FLbox,0)
FRwheel=loadobject("jeepwheel.b3d")
setobjectscalepermanent(FRwheel,0.2,0.2,0.2)
setobjectimage(FRwheel,jeeptex,0)
setobjectcoloremissive(FRwheel,40,40,40)
FRbox=createobjectbox(1,1,1)
fixobjecttoobject(FRbox,jeep)
setobjectposition(FRbox,9.6,3,12)
fixobjecttoobject(FRwheel,FRbox)
setobjectposition(FRwheel,0,0,0)
setobjectvisible(FRbox,0)
RLwheel=loadobject("jeepwheel.b3d")
setobjectscalepermanent(RLwheel,0.2,0.2,0.2)
setobjectimage(RLwheel,jeeptex,0)
setobjectcoloremissive(RLwheel,40,40,40)
RLbox=createobjectbox(1,1,1)
fixobjecttoobject(RLbox,jeep)
setobjectposition(RLbox,-10,3,-11)
fixobjecttoobject(RLwheel,RLbox)
setobjectposition(RLwheel,-10,3,-11)
setobjectvisible(RLbox,0)
RRwheel=loadobject("jeepwheel.b3d")
setobjectscalepermanent(RRwheel,0.2,0.2,0.2)
setobjectimage(RRwheel,jeeptex,0)
setobjectcoloremissive(RRwheel,40,40,40)
RRbox=createobjectbox(1,1,1)
fixobjecttoobject(RRbox,jeep)
setobjectposition(RRbox,9.6,3,-11)
fixobjecttoobject(RRwheel,RRbox)
setobjectposition(RRwheel,9.6,3,-11)
setobjectvisible(RRbox,0)
cambox=createobjectbox(1,1,1)
setobjectvisible(cambox,0)
fixobjecttoobject(cambox,jeep)
setobjectposition(jeep,0,-35,10)
wheelAngle#=0.0
speed#=0.0
setrawmousevisible(0)
SetRawMousePosition(width#/2,height#/2)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Screenshot setup
screenGrab = CreateRenderImage(GetMaxDeviceWidth(),GetMaxDeviceHeight(),0,1)
filesavetext=createtext("")
settextvisible(filesavetext,0)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx main loop
do
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Screenshot Section
if GetRawKeyPressed(80) // P to screenshot
SetRenderToImage(screenGrab, -1)
if gettextvisible(filesavetext) then settextvisible(filesavetext,0)
number = 1
ClearScreen()
Render()
checkFiles:
if GetFileExists("raw:"+GetReadPath()+"screenshots/Screenshot"+str(number)+".png")
number = number + 1
goto checkFiles
endif
SaveImage(screenGrab, "raw:"+GetReadPath()+"screenshots/Screenshot"+str(number)+".png")
settextstring(filesavetext,"Image saved as Screenshot"+str(number)+".png in "+ GetReadPath()+"screenshots")
SetTextSize(filesavetext,GetMaxDeviceHeight()*0.0275)
textTimer = 1
SetRenderToScreen()
if not gettextvisible(filesavetext) then settextvisible(filesavetext,1)
endif
If textTimer > 0 Then textTimer = textTimer + 1
If textTimer > 299
settextvisible(filesavetext,0)
textTimer = 0
EndIf
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx end screenshot section
mouseX# = (GetRawMouseX()-width#/2)*0.2
steerAngle#=steerAngle# + mouseX#
if steerAngle# < -40 then steerAngle# = -40
if steerAngle# > 40 then steerAngle# = 40
SetObjectRotation(FLbox,0,steerAngle#,0)
SetObjectRotation(FRbox,0,steerAngle#,0)
FLray = ObjectRayCast(terrain,getobjectworldX(FLbox),getobjectworldY(FLbox)+100,getobjectworldZ(FLbox),getobjectworldX(FLbox),getobjectworldY(FLbox)-200,getobjectworldZ(FLbox))
FLheight# = GetObjectRayCastY(0)
FRray = ObjectRayCast(terrain,getobjectworldX(FRbox),getobjectworldY(FRbox)+100,getobjectworldZ(FRbox),getobjectworldX(FRbox),getobjectworldY(FRwheel)-200,getobjectworldZ(FRbox))
FRheight# = GetObjectRayCastY(0)
RLray = ObjectRayCast(terrain,getobjectworldX(RLbox),getobjectworldY(RLbox)+100,getobjectworldZ(RLbox),getobjectworldX(RLbox),getobjectworldY(RLwheel)-200,getobjectworldZ(RLbox))
RLheight# = GetObjectRayCastY(0)
RRray = ObjectRayCast(terrain,getobjectworldX(RRbox),getobjectworldY(RRbox)+100,getobjectworldZ(RRbox),getobjectworldX(RRbox),getobjectworldY(RRwheel)-200,getobjectworldZ(RRbox))
RRheight# = GetObjectRayCastY(0)
FAHD# = FLheight# - FRheight#
RAHD# = RLheight# - RRheight#
MAHD# = (FAHD# + RAHD#) /30
LRtheta# = ATan(-MAHD#)
FAHD# = FLheight# - RLheight#
RAHD# = FRheight# - RRheight#
MADD# = (FAHD# + RAHD#) /35
FBtheta# = ATan(-MADD#)
curY# = GetObjectAngleY(jeep)
SetObjectPosition(jeep,GetObjectX(jeep),(FLheight#+FRheight#+RLheight#+RRheight#)/4,GetObjectZ(jeep))
setobjectrotation(jeep,FBtheta#,curY#,LRtheta#)
setobjectposition(FLwheel,0,FLheight#-getobjectworldY(FLbox)+4,0)
setobjectposition(FRwheel,0,FRheight#-getobjectworldY(FRbox)+4,0)
setobjectposition(RLwheel,0,RLheight#-getobjectworldY(RLbox)+4,0)
setobjectposition(RRwheel,0,RRheight#-getobjectworldY(RRbox)+4,0)
rotateobjectlocalX(FLwheel,speed#*10)
rotateobjectlocalX(FRwheel,speed#*10)
rotateobjectlocalX(RLwheel,speed#*10)
rotateobjectlocalX(RRwheel,speed#*10)
if getrawmouseleftstate()
speed#=speed#+0.05
if speed#>1.5 then speed#=1.5
endif
if getrawmouserightstate()
speed#=speed#-0.05
if speed#<-1.5 then speed#=-1.5
endif
MoveObjectLocalZ(jeep,speed#)
if speed#>0 then speed#=speed#-0.01
if speed#<0 then speed#=speed#+0.01
if speed#>-0.02 and speed#<0.02
speed#=0
endif
if speed#>0
rotateobjectlocalY(jeep,steerAngle#*0.025)
endif
if speed#<0
rotateobjectlocalY(jeep,-steerAngle#*0.025)
endif
if GetRawKeyState(188) // key <
SetObjectRotation(cambox,0,GetObjectAngleY(cambox)+1,0)
endif
if GetRawKeyState(190) // key >
SetObjectRotation(cambox,0,GetObjectAngleY(cambox)-1,0)
endif
SetCameraposition(cam1,GetObjectWorldX(cambox),GetObjectWorldY(cambox),GetObjectWorldZ(cambox))
SetCamerarotation(cam1,20,GetObjectWorldAngleY(cambox),0)
MoveCameraLocalZ(cam1,-100)
print(" mouse to drive")
print(" < > rotate cam")
SetRawMousePosition(width#/2,height#/2)
Sync()
if GetrawKeyState(27) then end
loop
BASIC appeared in May 1964. Lightning flashed, the wind roared and the Earth moved.
And nine months later I was born.
So here I am.
I am Basic.
Code is in my genes.