From Sonic Retro
|Sonic Physics Guide|
When the Player destroys a Badnik, and they are in the air, the game checks to see if they should rebound or not.
If the Player's Y Position is greater than that of the Badnik, or their Y Speed is negative, they will not rebound. Instead,
sign(Y Speed) will be subtracted from Y Speed (this is not limited to 0. If Y Speed were 0.25 (64 subpixels), it would not be set to 0, but to -0.75 (-192 subpixels)). This slight reduction of vertical speed helps give the impression that the Badnik had some weight to it.
On the other hand, if the Player's Y Position is less than that of the Badnik and their Y Speed is positive, they will rebound. Their Y Speed will simply be multiplied by -1, reversing it fully.
When the Player breaks an Item Monitor while in the air, they will rebound off of the Item Monitor in the same way as they do Badniks - only without bothering to compare their Y Position to that of the Monitor. The Player will rebound (have Y Speed reversed) no matter what.
But can't you control the height of the bounce by holding or not holding the jump button? Well yes, but that is actually part of the main jumping code.
If the Player has jumped (and only if they has jumped) into the Badnik/Monitor, the variable jump height code is still in effect. As mentioned in the Variable Jump Height - during a jump, at any point if the Player isn't holding jump and their Y Speed is less than -4, it will be set to -4. For this reason, if you are not holding jump when you collide with the object, and if the reboundedY Speed is less than -4, it will be set to -4 because the jump code does that already. Effectively, allowing you to control the rebound height.
This means that if you weren't jumping, and instead land on a Badnik/Monitor by rolling, you cannot control your bounce height at all, as the jump height code is not in effect.
Note: X Speed is totally unaffected by destroying a Badnik or Monitor, both in the air and when rolling on the ground.
When the Player hits a boss while in the air, their X Speed and Y Speed are both divided by 2 and reversed (so, multiplied by -0.5). Though, if on the ground, their Ground Speed is not affected at all. In your game, you may wish to fix that and have Ground Speed reverse and divide by 2.
If they are rebounded by a boss upward by this, then the standard jump circumstances still apply: releasing the Jump button while Y Speed is less than -4 will set Y Speed to -4, air drag is calculated, and so on.
- Gravity is added to Y Speed before the engine checks for and/or performs the rebounding routine.