Posted: Sun Mar 25, 2007 11:54 am Post subject: Acceleration
Im trying to make an acceleration program for my racing game. I'm trying to figure out how to make it so the longer i hold the button, the quicker the number rises. The only i can think of is taking time off of the delay. But i dont want the speed to be an outrageous number. So i have a second variable that is = to the counter/50, so its not as big. The reason im stuck with the delay issue is because of deceleration. Because i want "down" to be breaks, i want it to stop much quicker than it starts.
Sponsor Sponsor
Cervantes
Posted: Sun Mar 25, 2007 12:47 pm Post subject: RE:Acceleration
It's actually pretty easy. Have a variable store the car's speed. Each time through your loop, check if the accelerate button is pressed. If it is, add some constant value to the speed.
Each time through the loop, add the car's speed (velocity) to its position. Draw the car at its position.
Fashoomp
Posted: Sun Mar 25, 2007 2:11 pm Post subject: Re: Acceleration
How would i set a speed cap, because i dont want it going too fast. Also, i dont want it to have decimal numbers.
Saad85
Posted: Sun Mar 25, 2007 4:08 pm Post subject: RE:Acceleration
if speed<max then
speed +=accel
end if
^^there's your speed cap.
and just use int for all the numbers or round() before you use the value.
ericfourfour
Posted: Sun Mar 25, 2007 11:20 pm Post subject: RE:Acceleration
Err. Not really.
What if the speed is 99, the cap is 100, and the accel is 3?
It should really be:
Calculate the speed. If the speed is greater than the speed cap, make the speed equal the speed cap.
Saad85
Posted: Sun Mar 25, 2007 11:45 pm Post subject: RE:Acceleration
^^good point.
but if there's gonna be any other sort of element changing around the speed it's probably better to just have a checker at the end of the loop (or better yet, right before the speed is used to move).
like this:
if speed>cap then speed:=cap end if
then you don't need to check once for each modifier, only once per loop.
klopyrev
Posted: Mon Mar 26, 2007 12:18 am Post subject: Re: Acceleration
My guess is that you want that the longer you press the acceleration button, the faster the acceleration? During the loop, if the button is pressed, add 1 to the time variable. If it is not pressed, set the time variable to 0. The time variable stores how long the acceleration button has been pressed. Then, to find the instantaneous acceleration, you need some function that depends on time. Maybe Acceleration = Time/FACTOR. A more realistic one would be like this:
Posted: Mon Mar 26, 2007 1:10 am Post subject: RE:Acceleration
a suggestion, you mentioned you didn't want decimal number? This would make the acceleration outrageous. What most people do is just increment by say .05 per cycle of the loop and then round for the drawing.
Sponsor Sponsor
Cervantes
Posted: Mon Mar 26, 2007 4:22 pm Post subject: RE:Acceleration
klopyrev, I'm not sure why you think that the acceleration should increase the longer you press the button. That would correspond to having "jerk". Just as acceleration is the rate of change of velocity, jerk is the rate of change of acceleration. Spaceships have jerk. I don't know if most objects have any significant jerk or not. Regardless, keep that in mind.
Also, if you want to put a speed cap on your object, the way that was suggested works. However, it's not physically valid. It doesn't make much sense to just say the object cannot travel faster than 100 m/s. Instead, it makes sense to say that the object has a certain max acceleration value (that is directly proportional to the max force that it can produce), and there is also a frictional force. The frictional force is a combination of forces. Some are constant, (such as air resistance) is proportional to the square of the speed.
So here you can maybe make it up. One common way to do it is simply multiply the velocity by a positive constant factor less than one each time through the loop.
code:
object_velocity *= 0.99
Then you will have a max speed, and you can fiddle to get it right. Or you can do some math to determine what the constant should be.
The advantage of this is that it is more realistic. Instead of accelerating at a constant pace and then suddenly not being able to accelerate any more, you accelerate at an ever decreasing pace. More realistic.
Skynet
Posted: Mon Mar 26, 2007 6:40 pm Post subject: Re: RE:Acceleration
Cervantes @ Mon Mar 26, 2007 4:22 pm wrote:
klopyrev, I'm not sure why you think that the acceleration should increase the longer you press the button. That would correspond to having "jerk". Just as acceleration is the rate of change of velocity, jerk is the rate of change of acceleration. Spaceships have jerk. I don't know if most objects have any significant jerk or not. Regardless, keep that in mind.
Also, if you want to put a speed cap on your object, the way that was suggested works. However, it's not physically valid. It doesn't make much sense to just say the object cannot travel faster than 100 m/s. Instead, it makes sense to say that the object has a certain max acceleration value (that is directly proportional to the max force that it can produce), and there is also a frictional force. The frictional force is a combination of forces. Some are constant, (such as air resistance) is proportional to the square of the speed.
These two things tie together. Your acceleration is equal to the net force on the object divided by its mass. Since chemical rockets have a rapidly decreasing mass, they have a very visible "jerk" term. However, other factors (like Cervantes said - air resistance, friction) can apply an opposing force on the object, lowering the net force and thus decreasing acceleration. If such factors are included, it's possible to have a "maximum speed" imposed by the environment - a point at which the opposing forces on the object negate the maximum applied force. A common case is terminal velocity.
What would make the simulation nice and extensible would be to use this concept instead of just capping the max spped. Store the current velocity, the force your engine applies and the mass of your car. Using that and assuming an opposing force equal to the square of your velocity, you get the following:
F_net = F_engine - F_air*
*Assume F_air = K * v^2, where K is some number you "tune" and v = velocity. (K is proportional to drag coefficient, for any car people)
Then, your acceleration at any point will be:
a = F_net / m or a = (F_engine - K * v^2) / m
If you do this, you can add other things in later, since you're more accurately modeling what's happening in physics. Not that much extra work, either.
ericfourfour
Posted: Mon Mar 26, 2007 11:37 pm Post subject: RE:Acceleration
Holy crap! I'm learning that in physics . Now I'm excited.
Fashoomp
Posted: Tue Mar 27, 2007 8:07 pm Post subject: Re: Acceleration
Wow, thats a lot of response... But that still does not answer brakes. What if i want it to stop much faster than it starts?
Clayton
Posted: Tue Mar 27, 2007 8:18 pm Post subject: RE:Acceleration
Then have some sort of constant that is the acceleration due to braking (or in other words, a decceleration constant) to apply to your speed if you find that you are "braking".
Cervantes
Posted: Tue Mar 27, 2007 9:51 pm Post subject: RE:Acceleration
So you have a forward force applied by the engine, and you have a few frictional forces that we are modelling by multiplying the velocity of the car by 0.99 each time.
If we apply the brakes, that is nothing more than a force in the backwards direction. Treat it exactly the same as a negative force in the forwards direction. So just subtract some fixed value, the braking acceleration, from the velocity with each step through the loop. However, make sure the brakes don't apply if you are stationary!
Drakain Zeil
Posted: Thu Mar 29, 2007 5:59 pm Post subject: RE:Acceleration
well, you could do somethnig along the lines of:
if elapsed_time<X
speed = elapesed_time/number
then increment your car's position by speed, depending on direction