Difference between revisions of "SPG:Special Stages"
From Sonic Retro
(Formatting and rewording) |
m (Format) |
||
Line 34: | Line 34: | ||
====Variables==== | ====Variables==== | ||
The Player object is like any other, it uses X and Y Speed values, and a Ground Speed value. However, these values are applied differently as will be explain up ahead. | The Player object is like any other, it uses X and Y Speed values, and a Ground Speed value. However, these values are applied differently as will be explain up ahead. | ||
+ | |||
+ | =====Constants===== | ||
+ | Acceleration = 0.046875 | ||
+ | Deceleration = 0.25 | ||
+ | Max = 8 | ||
+ | Gravity = 0.1640625 | ||
====States==== | ====States==== | ||
At any time Sonic knows if he is touching a solid block at any side (up, down, left, right, any). This will be referred to as "Block Hitting" or the "Block Hitting flag" (true or false). | At any time Sonic knows if he is touching a solid block at any side (up, down, left, right, any). This will be referred to as "Block Hitting" or the "Block Hitting flag" (true or false). | ||
− | === | + | ====Player Physics==== |
'''Note''': | '''Note''': | ||
*''All of Sonic's motion that reacts to the stage angle uses the'' '''Snapped Stage Angle'''. | *''All of Sonic's motion that reacts to the stage angle uses the'' '''Snapped Stage Angle'''. | ||
Sonic is essentially always performing the same code, with only slight differences depending on the current situation. | Sonic is essentially always performing the same code, with only slight differences depending on the current situation. | ||
− | The following will describe the different functions/routines Sonic can for his physics. How and when these routines are executed will be detailed in [[SPG:Special_Stages#1: | + | The following will describe the different functions/routines Sonic can for his physics. How and when these routines are executed will be detailed in [[SPG:Special_Stages#1:_Player_Events|Player events]]. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | ====Left/Right Movement Routine==== | + | =====Left/Right Movement Routine===== |
The Movement routine checks for {{left}} or {{right}} input and updates Sonic's Ground Speed. Acceleration/deceleration works the same as normal, like when Sonic walks on normal ground. Friction (same as acceleration) is applied when no {{left}} or {{right}} input is detected. | The Movement routine checks for {{left}} or {{right}} input and updates Sonic's Ground Speed. Acceleration/deceleration works the same as normal, like when Sonic walks on normal ground. Friction (same as acceleration) is applied when no {{left}} or {{right}} input is detected. | ||
After updating Ground Speed, a '''potential_x_speed''' and '''potential_y_speed''' are created using Sonic's current Ground Speed and the '''Snapped Stage Angle'''. | After updating Ground Speed, a '''potential_x_speed''' and '''potential_y_speed''' are created using Sonic's current Ground Speed and the '''Snapped Stage Angle'''. | ||
− | =====Movement Angle===== | + | ======Movement Angle====== |
− | Though, the angle used here is not the real '''Stage Angle''' or even the '''Snapped Stage Angle'''. Here, this Stage Angle is wrapped to 90 | + | Though, the angle used here is not the real '''Stage Angle''' or even the '''Snapped Stage Angle'''. Here, this Stage Angle is wrapped to a 90 degree slice, the '''Wrapped Stage Angle'''. |
Imagine a line pointing out from Sonic at this angle. If the special stage was not rotated at all, this line would be pointing to the right. As the stage rotates, so does the line. But when the stage rotates past a 45 degree mark (and the line is pointing lower than 45 degrees to the bottom right), the line snaps back 90 degrees to point up to the top right. Then as the stage rotates, this line continues to rotate from there until it reaches the same point again. The opposite is true if it were to rotate past -45 degrees (and the line was pointing higher than -45 degrees to the top right, where it would snap forward 90 degrees). | Imagine a line pointing out from Sonic at this angle. If the special stage was not rotated at all, this line would be pointing to the right. As the stage rotates, so does the line. But when the stage rotates past a 45 degree mark (and the line is pointing lower than 45 degrees to the bottom right), the line snaps back 90 degrees to point up to the top right. Then as the stage rotates, this line continues to rotate from there until it reaches the same point again. The opposite is true if it were to rotate past -45 degrees (and the line was pointing higher than -45 degrees to the top right, where it would snap forward 90 degrees). | ||
Line 65: | Line 65: | ||
This is the angle used for Sonic's left/right movement, even when in the air. Because this occurs in the air too, it makes the special stage feel all the more disorienting, which is by design. | This is the angle used for Sonic's left/right movement, even when in the air. Because this occurs in the air too, it makes the special stage feel all the more disorienting, which is by design. | ||
− | =====Checks===== | + | ======Checks====== |
The game performs a tile check at Sonic's X Position + '''potential_x_speed''', and Sonic's Y Position + '''potential_y_speed'''. | The game performs a tile check at Sonic's X Position + '''potential_x_speed''', and Sonic's Y Position + '''potential_y_speed'''. | ||
Line 74: | Line 74: | ||
Neither Sonic's Block Hitting flag nor Sonic's actual speeds are changed/used in this routine. | Neither Sonic's Block Hitting flag nor Sonic's actual speeds are changed/used in this routine. | ||
− | ====Fall Routine==== | + | =====Fall Routine===== |
The fall routine is where Sonic's real X Speed and Y Speed come into play. | The fall routine is where Sonic's real X Speed and Y Speed come into play. | ||
Firstly, Sonic's Block Hitting flag is reset to false. | Firstly, Sonic's Block Hitting flag is reset to false. | ||
− | =====Falling Speeds===== | + | ======Falling Speeds====== |
Directional gravity speeds '''x_gravity''' and '''y_gravity''' are calculated based on the '''Snapped Stage Angle'''. | Directional gravity speeds '''x_gravity''' and '''y_gravity''' are calculated based on the '''Snapped Stage Angle'''. | ||
Line 87: | Line 87: | ||
potential_y_speed = Y Speed + y_gravity | potential_y_speed = Y Speed + y_gravity | ||
− | =====Checks===== | + | ======Checks====== |
The game then performs a tile check at Sonic's X Position + '''potential_x_speed''', and Sonic's Y Position. | The game then performs a tile check at Sonic's X Position + '''potential_x_speed''', and Sonic's Y Position. | ||
Line 101: | Line 101: | ||
Then, X Speed is set to '''potential_x_speed''', and Y Speed is set to '''potential_y_speed'''. | Then, X Speed is set to '''potential_x_speed''', and Y Speed is set to '''potential_y_speed'''. | ||
− | ====Jump Routine==== | + | =====Jump Routine===== |
The jump routine simply checks if a jump button is pressed, if it is, set's Sonics X Speed and Y Speed based on the '''Snapped Stage Angle''', and sets Sonic's Block Hitting flag to false. | The jump routine simply checks if a jump button is pressed, if it is, set's Sonics X Speed and Y Speed based on the '''Snapped Stage Angle''', and sets Sonic's Block Hitting flag to false. | ||
Line 147: | Line 147: | ||
Each frame, the special stage works in the same order: | Each frame, the special stage works in the same order: | ||
− | ====1: | + | ====1: Player Events==== |
Firstly, Sonic's code is executed. | Firstly, Sonic's code is executed. | ||
* If Sonic is Block Hitting, run Jump Routine. | * If Sonic is Block Hitting, run Jump Routine. |
Revision as of 10:23, 8 November 2022
Sonic Physics Guide |
---|
|
Collision |
|
Physics |
|
Gameplay |
Presentation |
Special |
Contents
Rotating Maze
The special stages in Sonic 1 have very unique physics, controls, and "feel". They do not share physics code with the normal gameplay at all.
Blocks
the Maze is constructed out of a grid of tiles. The size of each grid cell is 24 x 24.
Maze Rotation
While it looks complicated, the handling of Special Stage rotation is more of an illusion than actual rotation.
While they appear to, these special stages don't actually rotate. The blocks don't even actually move at all. Instead, Sonic's gravity, jump direction, and left/right input movement angle all change based on the stage's current angle. The actual Sonic object still moves relative to the static, unrotated, base layout. So, Sonic never has to collide with any real slopes at any time. This makes collision much easier, because Sonic is only ever truly colliding with flat sides of a block, and not sloped angles of rotated blocks.
Rotation Illusion
When motion and physics is processed it's a static grid of the stage tiles/blocks with Sonic flying around it at all sorts of angles.
While playing, the stage is simply displayed rotated. When the game displays any given block, it gets rotated with the rotation angle, around Sonic. Thus completing the illusion.
Maze Speeds and Angles
The special stage starts with a rotation speed of 0.25 in the original games. (In degrees, this would be -0.3515625)
Note:
- The Stage Angle is always kept positive, the angle needs to be "wrapped" otherwise it will give weird results when snapping the angle when negative.
To get the Snapped Stage Angle (used for physics and drawing the stage), just do
(Stage Angle div 4) * 4
A rough conversion for degrees:
floor(Stage Angle / 5.625) * 5.625
Maze Player Setup
Much of the Special stage Player object is the same as the normal Player object.
Variables
The Player object is like any other, it uses X and Y Speed values, and a Ground Speed value. However, these values are applied differently as will be explain up ahead.
Constants
Acceleration = 0.046875 Deceleration = 0.25 Max = 8 Gravity = 0.1640625
States
At any time Sonic knows if he is touching a solid block at any side (up, down, left, right, any). This will be referred to as "Block Hitting" or the "Block Hitting flag" (true or false).
Player Physics
Note:
- All of Sonic's motion that reacts to the stage angle uses the Snapped Stage Angle.
Sonic is essentially always performing the same code, with only slight differences depending on the current situation. The following will describe the different functions/routines Sonic can for his physics. How and when these routines are executed will be detailed in Player events.
Left/Right Movement Routine
The Movement routine checks for or input and updates Sonic's Ground Speed. Acceleration/deceleration works the same as normal, like when Sonic walks on normal ground. Friction (same as acceleration) is applied when no or input is detected.
After updating Ground Speed, a potential_x_speed and potential_y_speed are created using Sonic's current Ground Speed and the Snapped Stage Angle.
Movement Angle
Though, the angle used here is not the real Stage Angle or even the Snapped Stage Angle. Here, this Stage Angle is wrapped to a 90 degree slice, the Wrapped Stage Angle.
Imagine a line pointing out from Sonic at this angle. If the special stage was not rotated at all, this line would be pointing to the right. As the stage rotates, so does the line. But when the stage rotates past a 45 degree mark (and the line is pointing lower than 45 degrees to the bottom right), the line snaps back 90 degrees to point up to the top right. Then as the stage rotates, this line continues to rotate from there until it reaches the same point again. The opposite is true if it were to rotate past -45 degrees (and the line was pointing higher than -45 degrees to the top right, where it would snap forward 90 degrees).
So put simply, this direction is always pointing within a 90 degree slice towards the right of the screen, wrapped. We'll call this Wrapped Stage Angle.
This is the angle used for Sonic's left/right movement, even when in the air. Because this occurs in the air too, it makes the special stage feel all the more disorienting, which is by design.
Checks
The game performs a tile check at Sonic's X Position + potential_x_speed, and Sonic's Y Position + potential_y_speed.
Note:
- Tile checking is explained up ahead in Tile Checks.
If a solid tile is found, Ground Speed is set to 0, otherwise, the potential_x_speed and potential_y_speed are simply directly added to Sonic's position. Neither Sonic's Block Hitting flag nor Sonic's actual speeds are changed/used in this routine.
Fall Routine
The fall routine is where Sonic's real X Speed and Y Speed come into play.
Firstly, Sonic's Block Hitting flag is reset to false.
Falling Speeds
Directional gravity speeds x_gravity and y_gravity are calculated based on the Snapped Stage Angle.
Then, a potential_x_speed and potential_y_speed are calculated using that new gravity:
potential_x_speed = X Speed + x_gravity potential_y_speed = Y Speed + y_gravity
Checks
The game then performs a tile check at Sonic's X Position + potential_x_speed, and Sonic's Y Position.
If a solid tile is found, potential_x_speed is set to 0, and Sonic's Block Hitting flag is set to true.
Then the game performs a tile check at Sonic's X Position + potential_x_speed, and Sonic's Y Position + potential_y_speed.
Note:
- Here, potential_x_speed could potentially be 0 if something was found in the previous tile check.
If a solid tile is found, potential_y_speed is set to 0, and Sonic's Block Hitting flag is set to true.
Then, X Speed is set to potential_x_speed, and Y Speed is set to potential_y_speed.
Jump Routine
The jump routine simply checks if a jump button is pressed, if it is, set's Sonics X Speed and Y Speed based on the Snapped Stage Angle, and sets Sonic's Block Hitting flag to false.
Collision
You may think collision in a rotating special stage will be extremely complicated, but it's not at all as complicated as it first appears. As previously stated, the stage doesn't really rotate, which simplifies things incredibly.
Collision is purely based on what tiles are nearby Sonic's position.
Tile Checks
Whenever a tile check occurs, it accepts an x and y position as parameters and will check the 4 closest tiles to this location.
To locate the tiles to check based on this position, it will do the following
check_x = (X Position - 12) div 24; check_y = (Y Position - 12) div 24;
So this essentially rounds the position and gets the grid coordinates of the top left tile of what will be a 2 x 2 check area.
The game then goes through and checks the top left, top right, bottom left, and bottom right tile in that square, in that order.
Note:
- No matter the stage rotation, this always happens the same way. Stage rotation is not accounted for.
It will always check all 4 tiles, and the last tile checked that had something in it will become the Current Tile, and will be the one reacted to.
And of course, if any of these 4 tiles had solidity, a solid tile was found and that will be reacted to as stated in the physics routines previously.
Reaction To Blocks
Because of how collision is processed, Sonic can only be "focused" on one block at any given time (the Current Tile), and will only react to that block (also, obviously, he'll only react if currently Block Hitting). Though it's important to note Collision happens if he touches any tile though, regardless of what one ends up as the focus.
Some blocks that change the special stage state have a global 48 frame cooldown timer (per block type) so he can only interact with them once even if he is touching them for more than 1 frame. These include the Up/Down blocks and the R block.
Up/Down Blocks
Up blocks double the speed of the stage, Down blocks divide it by 2 (if it's > 0.25) This Block turns into the opposite type each time it activates.
R Blocks
R blocks multiply the stage rotation speed by -1.
Bumper
The bumper acts much like a normal bumper does (aside from the fact that the initial collision is completely different) Measuring the angle from the centre of the bumper tile, with a repulsion force of 7.
Order of events
Each frame, the special stage works in the same order:
1: Player Events
Firstly, Sonic's code is executed.
- If Sonic is Block Hitting, run Jump Routine.
- After this, regardless of his Block Hitting state, Sonic will then perform his Movement routine and lastly his Falling routine.
After the movement routines, the Current Tile will be reacted to (only when Block Hitting).
Then lastly, Sonic's X Speed and Y Speed are added to his position.
2: Other Events
After Sonic's events, the game will update the Stage Angle, by adding the rotation speed, and then calculating the Snapped Stage Angle.
Notes
- For 100% accuracy, it is recommended you don't use degrees and rather follow the original game's 256 angle structure - though you'll have to also use the trigonometry functions from the original games.
- As you can tell, Sonic's speeds and motion are simply halted when a block is detected at his next move. The only reason he nicely smoothly falls onto surfaces and doesn't stop short is thanks to the gravity softly nestling him in each frame.