From Sonic Retro
|Sonic Physics Guide
|0.1875 (48 subpixels)
When the Player has rings and collides into a Badnik (while uncurled) or other hazard, such as spikes or a field of lava, they are flung backwards and lose their rings. During this special hurt state, their physics behave quite differently.
The Player becomes invulnerable for a short time.
Y Speed is set to hurt_y_force at the moment of impact, and X Speed is set to hurt_x_force times a, where a is the sign of the Player's X Position minus the hazard in question's X Position. If a would be 0, it defaults to 1 so that the Player can never be flung straight upward. The direction their sprite is facing, however, does not change.
Note: The above is true even for being hurt by large lava fields and long rows of spikes (both of which are triggered by object hitboxes). The direction the Player is flung is always based on which side of the hazard's X Position they're on.
Even if the harmful object is above the Player, their Y Speed is set to -4. If the object is above and is solid, like spikes, they'll detect the ceiling collision in the next step and Y Speed will be set to 0. This results in the Player appearing to just fall away from the hazard.
While in the hurt state, the Player does not react to controller input. Also, Gravity is changed from the normal value to hurt_gravity_force until they land. When they do land, their X Speed is set to 0, so that they stop completely.
Air drag is not applied while hurt.
Exiting Hurt State
The Player cannot leave the hurt state until they land on the ground or on a Solid Object.
Note: In your framework, you may wish to change this by adding a timer that returns them to a normal falling state when it runs out. This is more fair, because without this fix the Player can be knocked off of a high ledge and they will be unable to regain control for quite some distance.
When the Player leaves the hurt state, Gravity is returned to normal.
When the Player has no rings and touches a harmful object, they will enter their death state. X Speed is set to 0, and Y Speed to -7. Gravity is set to the normal value.
Collision with Walls
In the Sonic 2 Nick Arcade prototype, if the Player hits a wall at their top X Speed of 6, they will bounce away with the identical physics as if they were hit by a harmful object - altered gravity and all. The only differences are that the Player doesn't lose rings, and when they land they spend a few steps in the knocked flat animation.
Granted, this isn't a feature in the finished Sonic 2, or even the later builds of the game, but the effect has been included in the some of the 3D Sonic games.