Actions

SPG:Special Stages

From Sonic Retro

Revision as of 09:38, 28 November 2022 by LapperDev (talk | contribs) (Added notes section)
Sonic Physics Guide
Collision
Physics
Gameplay
Presentation
Special

Notes:

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.

Maze Blocks

the Maze is constructed out of a grid of tiles. The size of each grid tile is 24 x 24. Some tiles contain solid "Blocks" (which includes normal solid blocks and special blocks such as Bumpers and Up/Down Blocks), some contain non-solid tiles like Rings, while others are empty.

Maze Rotation

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 Player motion and physics are processed in the Maze, it's always happening on a static un-rotated grid of the stage tiles/blocks.

While playing, the Player's gravity direction is being manipulated, and the stage is simply displayed as if it was rotated. When the game displays any given block, it gets rotated around Sonic (which is always centre of the screen) by the current Stage rotation angle. Thus completing the illusion.

A more general way to imagine this is: Instead of the stage itself rotating, it's as if the camera and gravity direction rotates while moving across the stage.

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 Object

Much of the Special stage Player object is the same as the normal Player object.

Variables & Constants

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.

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 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 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.

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:

If a solid block 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 block 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 block 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. It will always check all 4 tiles.

Note:

  • No matter the stage rotation, this always happens the same way. Stage rotation is not accounted for.
  • If any of these 4 tiles contains a solid Block, a solid Block was found and a collision will occur as stated in the physics routines previously.

The last tile checked which had a Block in it (no matter the Stage orientation or layout of the Blocks) will become the Current Block, and will be the one focused on for any unique reaction (if it is a Bumper for example).

Reaction To Blocks

Because of how collision is processed, Sonic can only be "focused" on one block at any given time (the Current Block), and will only react to that block. Sonic will only react to the Current Block if the Block Hitting flag is currently true.

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 Blocks

Bumper blocks act exactly like a normal Bumper does (aside from the fact that the initial collision condition is completely different) The game measures the angle from the centre of the Bumper tile to Sonic's position, 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's Block Hitting flag is true, run Jump Routine.
  • After this, regardless of his Block Hitting flag state, Sonic will then perform his Movement routine and lastly his Falling routine.

After the movement routines, the Current Block will be reacted to (only when the Block Hitting flag is true).

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.