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.
Yes for the 1,000,000th time!!
Blitz3D Forums/Blitz3D Programming/Yes for the 1,000,000th time!! 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...
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...
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.
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
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
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
The Sin used above, is to give a gradual acceleration and friction :o)
Sweet! That was it. Pretty easy!
Very nice code Rob!