Yes for the 1,000,000th time!!

Blitz3D Forums/Blitz3D Programming/Yes for the 1,000,000th time!!

Craig H. Nisbet(Posted 2006) [#1]
I need to make a vehicle move forward with a a keydown command, but I don't want the motion to be a linear movement. I'll like it to accelerate from a stop then decellerate after I release the key. I know I've asked this question before. I just don't remember what the result was.


Picklesworth(Posted 2006) [#2]
The best solution in my opinion is to use a physics sim engine like Tokamak or ODE.
If you don't want to do that, I can't really help you because I have no experience in the field of car physics...


octothorpe(Posted 2006) [#3]
Keep track of the velocity vector of your entity (using a type.) Each frame, apply a little force in the correct direction if the player is holding down the accelerator. Also, multiply the vector by a drag coefficient to achieve decceleration.


Ross C(Posted 2006) [#4]
You could use Sin() function. Have a counter variable:

speed# = 0
speed_factor# = 2.0


while not keyhit(1)

if keydown(200) then
   speed = speed + 0.1
elseif keydown(208) then
   speed = speed - 0.1
else
   if speed < 0 then
      speed = speed + 0.05
      if speed >0 then speed = 0
   elseif speed > 0 then
      speed = speed - 0.05
      if speed <0 then speed = 0
   end if
end if

MoveEntity car, 0, 0, sin(speed) * speed_factor

updateworld
renderworld
flip

wend

end



bradford6(Posted 2006) [#5]
ok, without giving you the answer it's best to think of it in these terms:

you need a few variables

1. the car
2. the amount of thrust when key is pressed
3. the cars velocity
4. the friction coefficient that will slow the car down

Friction = .99

if key is down then thrust = 0.01

car_velocity = car_velocity + thrust

car_velocity = car_velocity * friction

add the car velocity to the car position

in b3d you can just rotateentity and moventity. the sin/cos is done for you


Ross C(Posted 2006) [#6]
The Sin used above, is to give a gradual acceleration and friction :o)


Craig H. Nisbet(Posted 2006) [#7]
Sweet! That was it. Pretty easy!


Rob Farley(Posted 2006) [#8]
http://www.blitzbasic.com/Community/posts.php?topic=47422#527131


Ross C(Posted 2006) [#9]
Very nice code Rob!