Physics Asked by Henri on September 29, 2021
I’ve tried to remember everything I could thats related to physics I’ve learned in school and college, but I’m failing. I’m currently trying to develop the physics of my own game (I could instead use some premade tools, but this wouldnt be any fun). For any sake, this is going to be top-down viewed in 2D, kinda like the SNES Zelda, for example.
Lets suppose we received some input (move right, left, up or down). Initially, what does that mean in terms of velocity and acceleration, given the options below?
Later on, on every game tick (which is kinda unrelated to physics itself), we recalculate the acceleration and velocity to find the current player position, as follows:
acceleration = ...
velocity = velocity + acceleration * time
position = position + velocity * time
Where acceleration
, velocity
and position
are all vectors and time
a scalar.
On this step, I would like to gradually decelerate, if it’s accelerated, so that the player reaches velocity = 0 and goes back to inertia. For instance, I would like to consider the terrain friction, object mass and wind forces.
How do I calculate the above deceleration, given that the player has an acceleration, velocity, position and mass values? Do we need anything else for this?
Which of the options (1-6) depends on what type of control experience you are trying to create. Imagine the player is stationary: when they hit the "move forward" input, should they immediately start moving forward with a certain speed, then slow to a stop due to some kind of simulated friction, or should they be issued an initial acceleration (like a rocket has just been lit) that speeds them up from stationary to a max speed over time, before coming back down to zero due to friction eventually?
This is up to you, and you can easily experiment in your code.
Graphically, these would look like the following (assuming constant frictional force):
So those are the first two options, and the others are just variations on this theme.
In conclusion (for the first part of your question): it is up to you, depending on what effect you want to create.
As for the second part, this is the core of the simulation, and you have a little mistake. You are correct that you need to keep track of the players current position, velocity, and acceleration, but your formulas for how to update these are a little off.
The first equation you have left as you are unsure how you will calculate the acceleration on each frame. We will come back to this.
The second equation describing how the velocity updates is correct. Velocity is the integral of the acceleration, so if you know the acceleration over a time interval, then the velocity will increase by this amount.
$$v=int a,dt = a (t_1 - t_0) = a cdot td$$
Where $td$ is the "time" delta in your equations.
The mistake is in your third equation. You are correct that the new position is the old position plus the displacement over the time interval:
$$p = p + s,$$
but this displacement, $s$, is not simply $v cdot td$ (the velocity times the elapsed time delta), but actually $vcdot td + frac{1}{2}a(td)^2$, since the velocity increases during the time interval, so we must use the suvat equation for constant acceleration.
So now what is the acceleration for this interval? Well before we can answer this, you need to know if you want to simulate mass. If so, then the acceleration is calculated from the current total force on the object (which I will come to in a second), divided by the mass.
$$a = F / m$$
But since the total force is the sum of some other forces (the impulse from a control input, wind forces, ground friction etc.), this is really
$$a = (F_1 + F_2 + F_3 dots) / m$$
which distributes to a set of accelerations
$$a = F_1/m + F_2/m + F_3/m dots = a_1 + a_2 + a_3 dots.$$
So if you want to have players of different masses, then you must work in forces. On the other hand, if all your players will have the same mass, then you can just sum accelerations, and not worry about forces. I will discuss what forces you might consider now.
The simplest force is friction. This acts in the opposite direction to that of the motion, and you can have set to some constant. If you write the code correctly, this will mean that the object will always come to a stop.
Next is the impulse force (or acceleration) due to the control inputs (if you are taking option (1) approach to control inputs, otherwise you would just set the velocity, as in option (2)). This just requires you to incorporate a force for a certain time interval, as if the control input were a rocket being switched on.
Once you have this framework working well, there are endless possibilities for other forces you may add!
Hope this helps.
Answered by Joe Iddon on September 29, 2021
About your question on what to do on input: it depends on how accurate you want your game to be. Game makers often choose methods that are either visually appealing or nice to play over physically accurate methods. Double jumping is an example of a method that is fun to play but in no way accurate.
For jumping the easiest way (and most used I think) is to just set the upwards velocity to some value. This imitates a large force over a short time period (impulse). You could have the magnitude of the jump depend on how long you hold the button.
For gradual deceleration you use either linear or quadratic friction. $text{acceleration}propto-|text{velocity}|$ or $text{acceleration}propto-text{velocity}^2$. Or in code it is easier to multiply the velocity by some constant to get linear deceleration.
velocity = velocity*0.99
If you have implemented friction you can also use it for left/right controls. If you hold down left/right apply a strong force in that direction and the friction will make the player reach terminal velocity in a short timespan. In other words the speed will asymptotically reach some maximal value.
You would want to make these accelerations/decelerations rather large because otherwise the game will feel kinda slow or slippery even.
Answered by AccidentalTaylorExpansion on September 29, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP