Hi there! I'm going to give you some help with car physics.
Ok, first off, place these variables at the start of your code (before the "do" main loop command)
rem speed for car
spd# = 0
spd_max# = 10
spd_acc# = 0.02
so, we have "spd#" to tell the car object how fast to move. "spd_max#" stops the car from accelerating to infinity and "spd_acc#" is how fast the car builds up speed.
So, with that done we must put this command just after the main loop (do).
"car" is the number of you car object so just change it to match. At this point, the object will not move because "spd#" is set to 0.
Now we start to control the car movement. Put this code within the main loop...
if upkey()=1
if spd# < spd_max#
inc spd#,(spd_acc#*1)
endif
endif
This will increase the speed and thus move the car object. This also makes sure the car doesn't go too fast.
To make it so the car slows downs when you aren't pressing the upkey, put this code underneath...
if upkey()=0
if downkey()=0
if spd# > 0
dec spd#,(spd_acc#/0.3)
endif
if spd# < 0
spd# = 0
endif
endif
endif
To reverse, put this in code in...
if downkey()=1
if spd# > -5
dec spd#,(spd_acc#*2)
endif
endif
And, for a handbrake using the spacekey, use this code...
``````hand brake```````````````````
if spacekey()=1
dec spd#,(spd_acc#*5)
if spd#<0 then spd#=0
endif
For steering, use this code. The variable "cy#" is the angle of your vehicle so make sure you have a command like this in your code "cy# = object angle y(car)".
Remember, "car" is the object number of your car object.
rem rotate car based on speed but make sure the rotation has a limit
if leftkey()=1
if spd#<2
Yrotate object car,Wrapvalue(cY#-spd#)
endif
endif
if spd#>2 and leftkey()=1 then Yrotate object car,Wrapvalue(cY#-2)
if rightkey()=1
if spd#<2
Yrotate object car,Wrapvalue(cY#+spd#)
endif
endif
if spd#>2 and rightkey()=1 then Yrotate object car,Wrapvalue(cY#+2)
And here's a working example which you can manipulate to suit your needs. Hope this helps you.
sync on
sync rate 60
rem speed for car
spd# = 0
spd_max# = 10
spd_acc# = 0.02
rem make car model
car=1
make object box car,10,10,20
color object car,rgb(255,0,0)
rem make landscape
make object box 2,1000,1,1000
color object 2,rgb(0,100,0)
rem main loop
do
rem print object speed
set cursor 5,100
print "speed: ";spd#
move object car,spd#
cy# = object angle y(car)
if upkey()=1
if spd# < spd_max#
inc spd#,(spd_acc#*1)
endif
endif
if upkey()=0
if downkey()=0
if spd# > 0
dec spd#,(spd_acc#/0.3)
endif
if spd# < 0
spd# = 0
endif
endif
endif
if downkey()=1
if spd# > -5
dec spd#,(spd_acc#*2)
endif
endif
``````hand brake```````````````````
if spacekey()=1
dec spd#,(spd_acc#*5)
if spd#<0 then spd#=0
endif
rem rotate car based on speed but make sure the rotation has a limit
if leftkey()=1
if spd#<2
Yrotate object car,Wrapvalue(cY#-spd#)
endif
endif
if spd#>2 and leftkey()=1 then Yrotate object car,Wrapvalue(cY#-2)
if rightkey()=1
if spd#<2
Yrotate object car,Wrapvalue(cY#+spd#)
endif
endif
if spd#>2 and rightkey()=1 then Yrotate object car,Wrapvalue(cY#+2)
rem this is only to control the camera so it follows the car object
position camera object position x(car),object position y(car)+10,object position z(car)
rotate camera object angle x(car),object angle y(car),object angle z(car)
pitch camera down 10
move camera -100
sync
loop