3
$\begingroup$

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?

  1. that means we need to reset the acceleration to some value
  2. that means we need to reset the velocity to some value
  3. that means we need to reset both the acceleration and velocity to some value
  4. that means we need to sum the current acceleration with some value
  5. that means we need to sum the current velocity with some value
  6. that means we need to sum both the acceleration and velocity with some value

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 timea 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?

$\endgroup$
2
  • $\begingroup$ Are you simulating something on the surface of the earth? Do you want the option of using a gravitational acceleration other than earth's? Will you be looking down on projectiles that are traveling in the "z" direction while you are observing the "x" and "y" directions? What time interval are you simulating, and what delta-time interval do you plan to use? $\endgroup$ Jul 28, 2020 at 14:55
  • $\begingroup$ The gravitational force is way to high to be simulated on games and this is why we usually use our customs (this is what I read the other day). There's no Z (no jumping or flying allowed just yet). Delta times are nearly at 60 frames per second, if this is what youre asking. $\endgroup$ Jul 28, 2020 at 15:05

2 Answers 2

0
$\begingroup$

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):

Option (2): set velocity

Option (1): enter image description here

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 $v\cdot 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.

$\endgroup$
2
  • $\begingroup$ Hi! I still can't relate the effect I want to accomplish with either acceleration or velocity. You did not mention the gradual deceleration, or am I completely lost here? You mentioned 'writing the code corretly" but that just seems to be the problem. Also, what about the friction part (which implies on the gradual deceleration)? $\endgroup$ Jul 28, 2020 at 15:50
  • $\begingroup$ I don't know what you mean by "relate the effect I want to accomplish" because you haven't described what effect you want to accomplish! I did explain the gradual deceleration - by adding a frictional force opposite to the direction of motion, the acceleration will be negative, so the player will always come to a step with a gradual deceleration. I will write the code for you if you really want to show it works. :) $\endgroup$
    – Joe Iddon
    Jul 29, 2020 at 15:56
0
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Hi! There's no jumping allowed just yet. It's no platformer, instead it's a top-down like Zelda. When you talk about deceleration, youre implicitly talking about velocity, right? What about the acceleration value we added on the previous step (controller input)? $\endgroup$ Jul 28, 2020 at 15:14
  • $\begingroup$ @Henri Oh right I read over the top-down part! With deceleration I just mean negative acceleration. You can add the controller acceleration to the friction. When you reach terminal velocity the friction will exactly cancel the input acceleration. $\endgroup$ Jul 28, 2020 at 15:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.