Continuation of a series of articles on physics in games about Sonic. This post covers the loss of rings and being under water.Links to other parts of the series:Part 1: hard tilesPart 2: RunningParts 3 and 4: Jumping and SpinningParts 7 and 8: springs and gizmos, super speedsPart 5: the loss of rings
Ring Restrictions')
When Sonic hits an adversary or dangerous obstacles, he loses all the rings he has collected, but 32 rings fly in different directions. So Sonic cannot reassemble more than 32 rings, even if he had 100 of them.
Scatter ringsRings, flying away from Sonic, move along a certain trajectory of two concentric circles, each of which contains no more than 16 rings. The rings in the first circle fly apart faster than the rings in the second. Here is a formula for creating rings that can be converted to any programming language:
{ let t = 0 let angle = 101.25 ; 0=, 90=, 180=, 270= let n = false let speed = 4 while t < { " " -sin(angle)*speed cos(angle)*speed if n = true { -1 22.5 } let n = not n ; if n = false, n true, t 1 if t = 16 { let speed = 2 ; , let angle = 101.25 ; } } }
So, the first 16 rings move at a speed of 4 pixels per cycle, and the second 16 - two times slower (2 pixels per cycle).
Gravity for ringsRings do not use the same gravitational constant as Sonic. The value of their gravitational force is 0.09375. Therefore, this value is added each cycle to the vertical speed of the rings.
Ring bouncingWhen the scattering rings touch the ground, their vertical speed is multiplied by a factor of -0.75. This leads to a change in the direction of their vertical movement, and also reduces speed. This calculation is performed after adding gravity. Ring bouncing does not affect their horizontal speed.
The ring rebound is calculated so roughly that it is almost annoying. But in a normal game, this is actually imperceptible, and perhaps it was necessary to avoid the brakes. It seems that they do not check every cycle, whether they are bumped into the ground, so that sometimes they sink into the surface rather heavily before deciding to bounce back. This behavior does not need to be emulated.
However, there are other restrictions for jumping rings, perhaps they are used to avoid processor load. The rings are completely unaffected by walls, and they interact with vertical surfaces. In addition, they check for collisions with the surface only when they are moving down (i.e. when their vertical velocity is positive). Therefore, they can even fly through the ceiling. Sometimes it can really be a problem for Sonic if he gets hit in a small corridor, because most of the rings will fly into the ceiling and cannot be assembled again.
If I used a more powerful system than Genesis / Mega Drive, I would program the rings to check for horizontal collisions. In a collision, they must multiply their horizontal speed by a factor of -0.25. They should also check for collisions with ceilings, determining whether a collision happened while moving up or down. If they were moving down, they should bounce as usual, but if they moved up, they should reset their vertical speed. It would be more gracious to the player, so most of the rings would not be lost when trying to re-gather.
The life of the ringsAll flying rings are destroyed after 256 cycles, if they were not reassembled. Also, they are destroyed when going beyond the horizontal borders of the screen. And, again, on a more powerful system, this behavior can be not emulated.
Re-collecting ringsAfter hitting, Sonic needs 64 cycles before he can reassemble the scattered rings.
InvulnerabilityAfter impact, Sonic becomes invulnerable for a short time. This invulnerability lasts until Sonic flies away from the blow, and then another 120 cycles after the resumption of movement. During these 120 cycles, it flickers, disappearing and appearing every 4 cycles.
Ring animationsThe rings hanging in place have 4 animation frames, and they are replaced every 8 cycles. The behavior of the scattering rings is more difficult, because they have a higher animation speed when created, but over time it decreases. At the beginning, the animation frames are replaced every 2 cycles, but at the end they are slowed down and changed every 16 cycles or so. While I do not know the exact values. The scattering rings are created with one starting frame of the animation - the ring can be seen entirely.
The depth of drawing ringsBoth flying and hanging rings have a greater depth than Sonic, that is, they are drawn behind it. But they have less depth, that is, they appear above it when they turn into sparks.

Part 6: being under water
When Sonic is under water, he moves more slowly, but in general the physics remains the same. This is mainly achieved by dividing the corresponding variables in half, but some, such as gravity and the initial jump speed, are not exactly equal to half.
VariablesAccelerationAcceleration has half the normal value: 0.0234375.
SlowdownSlowdown is half the usual value: 0.25.
Rotation slowdown remains unchanged.
Friction forceThe frictional force is half the usual value: 0.0234375.
The frictional force during rotation is 0.01171875.
Max speedSonic's maximum running speed is 3, half the normal value.
Acceleration in the airAcceleration in the air operates in the usual way, but has a value of 0.046875, two times less than usual.
GravityGravity is 0.0625. Thanks to this, Sonic falls much more slowly.
Jump speedThe initial jump speed is -3.5 (-3 for Nackles), the speed at the time of separation from the ground is -2.
BubblesWhen Sonic collides under water with a bubble, his velocities in X and Y both become equal to 0.
entrance and exitWhen Sonic falls into water, his horizontal speed is multiplied by 0.5, and the vertical speed is multiplied by 0.25 (this happens after adding gravity). Conversely, when Sonic jumps out of the water, his vertical speed doubles (after adding the value of “water” gravity) and he begins to move faster, because water no longer affects his movement (however, leaving the water does not affect the horizontal speed).
DrowningWhen Sonic sinks, his speeds on X and Y both become equal to 0, and “water” gravity remains normal.
Speed ​​shoesIf Speed ​​Shoes are worn on Sonic and he falls under water, water variables have a greater impact, thus zeroing all the effects of Speed ​​Shoes. No calculations, such as multiplication by 0.5, are performed.
Animation speedsDiving under the water does not affect the speed of the animation of Sonic. Animations whose speeds are proportional to variables that change, such as Sonic's speed, change automatically in the same proportion.
PS for attentive and curious readers. In this series of posts there is a small Easter egg, namely, two encrypted English words. Each part contains exactly one letter (that is, it is now possible to decrypt already 6 letters), there will be 14 of them, like the parts. sites. Your guesses can be sent to the PM, for the read-on and just readers in the profile there is my mail. The first one who guessed will have some nice bonus (for example, translation of an article that is interesting for him). (I hope such riddles are not against the rules of Habr)