I'm working on a game where you're trying to run away from projectiles but their speed increases overtime. I came up with this solution:
vel = vel*0.01
x_coordinate = vel
At first it works fine but after some time it gets really fast and breaks the game. Is there another way that I can increase the speed for a some time and cap it at a certain level?
CodePudding user response:
You should increase the velocity with a constant rate.
vel = 0.01
x_coordinate = vel
Or an even better choice if you want to cap the speed at a certain point is to slow the rate at which the speed is increasing.
acc = 0.01
vel = acc
x_coordinate = vel
acc *= 0.9
CodePudding user response:
What you need is acceleration:
Velocity is the change in position over time. And acceleration is the change in speed over time.
For a constant speed you can basically add your velocity to your position and you will have a constant speed movement:
Assuming position and velocity are vectors:
pos = vector(0, 0)
vel = vector(1, 1)
pos = vel
If you have some acceleration the problem is still simple. before you change the position you shall change the velocity by acceleration.
Assuming position and acceleration, speed are vectors:
pos = vector(0, 0)
vel = vector(1, 1)
acc = vector(1, -1)
vel = acc
pos = vel
Now you have accelerated motion.