From Sonic Retro
|Sonic Physics Guide|
Collision with Hazards
When the Player has rings and bumps into an enemy or other hazard, such as spikes or a field of lava, they are flung backwards. During this special hurt state, their physics behave quite differently.
Their Y speed is set to -4 at the moment of impact, and their X speed is set to 2 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 lava fields and rows of spikes. The direction the Player is flung is always based on which side of the centre of the hazard they're on.
Even if the harmful object is above the Player, their Y Speed is set to -4. If the object is solid, like spikes, they'll detect the 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 0.21875 (the normal value) to 0.1875 until they land. When they do land, their X Speed is set to 0, so that they stop dead. Also, they enter into an invulnerability state which lasts for 120 frames (0x78).
Air drag is not applied while hurt.
The Player cannot leave the hurt state until they hit the ground. In your framework, you may wish to change this, 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.
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.
When the Player is killed by a harmful object, their X Speed is set to 0, and their Y Speed to -7. Gravity remains normal. Of course, the Player takes no input at this point, either. While in their dying state, they detect no collisions with objects or water surfaces.