An overview of physics in Sonic games. Part 2: running
- Transfer

Continuation of the series of articles on physics in games about Sonic.
Note : The
review covers all four parts of the game on the Genesis / Mega Drive and Sonic CD .
Everything written below refers to the situation when Sonic is on a flat dry surface without any special bonuses. Curves, physics of behavior in water, bonuses Super Sonic and Speed Shoes will be considered in separate articles. Under the steps in the article refers to program cycles, rather than character steps.
Links to other parts of the series:
Part 1: solid tiles
Parts 3 and 4: jumping and spinning
Parts 5 and 6: losing rings and being under water
Parts 7 and 8: springs and gizmos, super speeds
Variables
The following variables and constants will often be used in this part:
//переменные
Xsp: скорость горизонтального перемещения Соника
Ysp: скорость вертикального перемещения Соника
//константы
acc: 0.046875
dec: 0.5
frc: 0.046875 (равна acc)
top: 6
Principles of movement
GIF on the topic (4.7MB)

Acceleration
When you press and hold the button
from the standby mode, the speed of Xsp begins to increase by acc . When you press and hold the button
from the standby mode, the Xsp speed begins to decrease by acc .Slowdown
If Sonic is already moving, and not standing when you press
or
, the computer checks to see if the button is pressed in the direction in which it is already moving. If so, acc is added to Xsp , as usual . If the button is pressed in the opposite direction, the deceleration constant ( dec ) is added instead . Thanks to this, Sonic can turn quickly. If there was no difference between acc and dec , Sonic's speed change would take too much time, annoying the player. A good engine should not make such childhood mistakes.
You might think that with Xsp = 0.1, dec will subtract after clicking
, and the value of Xsp will be -0.4. Strange, but this is not the case with games for Mega Drive. If the result of adding or subtracting dec leads to a change in the sign of Xsp , then Xsp is assigned the value dec . For example, in the above case, Xsp will become -0.5. The funny result of this will be this behavior: you can click
in one step, and then click
(or vice versa), and start running faster than if you just clicked
! The final speed is still lower than one pixel per step, so this is not so noticeable, but nevertheless, this is true. When creating your own engine, you may not try to copy such strange behavior.Friction force
If you do not press
or
, friction force ( frc ) comes into play . At each step, in which the game is not receiving signals from the horizontal displacement controller buttons of Xsp subtracted frc , multiplied by the sign Xsp , until the absolute value Xsp is less than frc ; in this case, Xsp simply equals zero.Sonic's maximum speed can only be accelerated to a certain limit. At some point, he reaches maximum speed and can no longer accelerate on his own. After addition acc to Xsp computer checks whether exceeds Xsp value top . If exceeds, then the value top is assigned to it .
This means that if Sonic somehow acquired a higher speed, which he cannot achieve himself (for example, he reached it by pushing himself from the spring), then when the button is pressed in the direction of his movement, the computer adds acc , notices that Xsp is greater than top , and equalsXsp to top . Thus, you can reduce the momentum by pressing the direction button. This is a flaw! You can eliminate it during the development of the engine by checking if Xsp is less than top before adding acc . Only if Xsp less top computer must allow acc and check to see if more than Xsp value top . The problem is resolved.
In fact, the Sonic CD uses a similar fix because Sonic can perform a Super Peel Out (called Dash)in the Japanese version of the game), which pushes it forward at a speed of 12 pixels per step. Under normal conditions, Sonic cannot accelerate this way, but if he reaches this speed with a jerk, he can continue to run with it while the player presses the button in the direction of his movement. When you release the button, the friction force does its job. When pressed again, the friction force ceases to act, and the Xsp remains constant, but it cannot rise to 12 without a new jerk. Sonic CD
programmers did not begin to use this fix for a situation when Sonic is in the air, so if he uses a jerk, and then falls off the ledge when the button is pressed in the direction of his movement, then Xspcan drop to 6, no matter how high the speed was at that time. This is also a big flaw.
Here is the pseudo code that accurately emulates motion and friction:
if (игрок нажимает кнопку "Влево") {
if (xsp > 0)
{
xsp -= dec;
}
else (if xsp > -top)
{
xsp = xsp-acc;
}
} else if (игрок нажимает кнопку "Вправо") {
if (xsp < 0)
{
xsp += dec;
}
else if (xsp < top)
{
xsp = xsp+acc;
}
} else xsp = xsp-minimum(absolute(xsp), frc)*sign(xsp);
Principles of Animation
Running Animation
Considering the Sonic CD , Sonic has three different running animations. It is portrayed standing only if Xsp is zero. If it has any other Xsp , it goes into a walk animation, in which the frame change depends on the speed of movement on the ground.
As soon as Xsp becomes equal to (or greater) 6, it goes into a running animation with “spinning” legs. When Xsp is equal to or greater than 10, it goes into the jerk animation with the movement of the legs in the form of a "figure eight". Jerk animation is only Sonic CD .

Walking

Running

Jerk
Sonic braking animation turns into a braking animation when turning only if the absolute value of Xsp is equal to or greater than 4.5. In Sonic 1 and Sonic CD, it remains in the braking animation mode until the Xsp reaches zero or changes sign. In the other three games, Sonic returns to walking animation mode after playing all frames of the braking animation.

Braking
Differences in the physics of different characters
All characters - Sonic , Tails and Knuckles (Sonic, Tails and Knuckles) - have the same values of acceleration, deceleration, maximum speed, running and braking. They are handled the same way, with the exception of their special moves and sprites (and the annoying fact that Knuckles jumps lower than the other two).