📜 ⬆️ ⬇️

Review of physics in Sonic games. Part 2: Running



Continuation of a series of articles on physics in games about Sonic.

Note :
')
The review covers all four parts of the game on Genesis / Mega Drive and Sonic CD .

Everything written below refers to the situation when Sonic is on a flat dry surface, not having special bonuses. Curves, physics of behavior in water, bonuses Super Sonic and Speed ​​Shoes will be discussed in separate articles. Under the steps in the article refers to software cycles, not the steps of the character.

Links to other parts of the series:
Part 1: hard tiles
Parts 3 and 4: Jumping and Spinning
Parts 5 and 6: the loss of rings and being under water
Parts 7 and 8: springs and gizmos, super speeds


Variables

In this section, the following variables and constants will often be used:

// Xsp:     Ysp:     // acc: 0.046875 dec: 0.5 frc: 0.046875 ( acc) top: 6 

Principles of movement
Gif on topic (4.7MB)


Acceleration

By pressing and holding the button from standing mode, the speed of the Xsp starts to increase each step by acc . By pressing and holding the button from standing mode, the speed of the Xsp begins each step to decrease by acc .

Slowdown

If Sonic is already moving, and not worth it when you press or The computer checks if the button is pressed in the direction in which it is already moving. If so, then acc is added to the Xsp , as usual. If the button is pressed in the opposite direction, the deceleration constant ( dec ) is added instead. Thanks to this, Sonic can quickly turn around. If the difference between acc and dec were missing, Sonic's speed change would take too much time, annoying the player. A good engine should not make such childish mistakes.

You might think that when Xsp = 0.1 after clicking dec will be subtracted , and the Xsp value will be -0.4. Strange, but in the case of games for the Mega Drive it is not. 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 equal to -0.5. The funny result is 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 you create your engine, you can not try to copy this strange behavior.

Friction force

If you do not click or , the friction force comes into play ( frc ). At each step in which the game does not receive signals from the controller's horizontal movement buttons, frc multiplied by the Xsp sign is subtracted from Xsp until the absolute value of Xsp is less than frc ; in this case, Xsp is simply equal to zero.

Max speed

Sonic can only accelerate to a certain limit. At some point, it reaches maximum speed and can no longer be accelerated on its own. After adding acc to Xsp, the computer checks to see if Xsp is greater than the top value. If it exceeds, then it is assigned the value top .

This means that if Sonic somehow gained a higher speed, which he could not reach himself (for example, he reached it by pushing away from the spring), then when you press a button in the direction of his movement, the computer adds acc , notices that Xsp exceeds top , and equates Xsp to top . Thus, you can reduce the momentum by pressing the direction button. This is a flaw! You can eliminate it when developing the engine by checking whether Xsp is less than top before adding acc . Only if Xsp is less than top the computer should add acc and check if Xsp is greater than the value of top . Problem solved.

In fact, a similar fix is ​​used in the Sonic CD because Sonic can perform a quick jerk ( Super Peel Out ) (which is called Dash in the Japanese version of the game), which pushes him 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 her 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 use this fix for a situation where Sonic is in the air, so if he uses a jerk and then falls off the ledge while the button is held in the direction of its movement, then when removed from the ground, Xsp can drop to 6, regardless of how high the speed was at that time. This is also a big flaw.

Here is the pseudocode that emulates motion and friction force precisely:

 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

If we consider Sonic CD , then Sonic has three different running animations. He is depicted as 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 along the ground.

As soon as Xsp becomes equal (or greater) 6, it goes into running animation with “spinning” legs. When Xsp is equal to or greater than 10, it goes into a figure-eight jerk animation. The jerk animation is only Sonic CD .


Walking


Run


Jerk

Braking animation

Sonic goes into 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 Xsp reaches zero or changes sign. In the remaining three games, Sonic returns to the walk animation mode after completing the playback of all the frames of the drag animation.


Braking

Differences in the physics of different characters

All characters - Sonic , Tails and Knackles (Sonic, Tails and Knuckles) - have the same values ​​of acceleration, deceleration, maximum speed, running and braking. They are treated the same, with the exception of their special moves and sprites (and the annoying fact that the Knuckles jumps lower than the other two).

Source: https://habr.com/ru/post/276849/


All Articles