Actions

Difference between revisions of "SPG:Special Stages"

From Sonic Retro

m (Format)
m (Format & structure)
Line 3: Line 3:
 
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.
 
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===
+
===Maze Blocks===
the Maze is constructed out of a grid of tiles. The size of each grid cell is 24 x 24.  
+
the Maze is constructed out of a grid of tiles. The size of each grid tile is 24 x 24.
 +
Some tiles contain blocks (which includes special blocks such as Bumpers and Up/Down Blocks) while others are empty.
  
 
===Maze Rotation===
 
===Maze Rotation===
While it looks complicated, the handling of Special Stage rotation is more of an illusion than actual 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.
 
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=====
 
=====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.
+
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 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.
+
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.
  
===Maze Speeds and Angles===
+
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)
 
The special stage starts with a rotation speed of 0.25 in the original games. (In degrees, this would be -0.3515625)
  
Line 29: Line 32:
 
  floor(Stage Angle / 5.625) * 5.625
 
  floor(Stage Angle / 5.625) * 5.625
  
===Maze Player Setup===
+
===Maze Player Object===
 
Much of the Special stage Player object is the same as the normal Player object.  
 
Much of the Special stage Player object is the same as the normal Player object.  
  
====Variables====
+
====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.
 
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
 
  Acceleration = 0.046875
 
  Deceleration = 0.25
 
  Deceleration = 0.25
Line 42: Line 44:
  
 
====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 the "Block Hitting flag" (true or false).
  
 
====Player Physics====
 
====Player Physics====
Line 129: Line 131:
  
 
====Reaction To Blocks====
 
====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.
+
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 the Block Hitting flag is currently true). 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.
 
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.
Line 140: Line 142:
 
R blocks multiply the stage rotation speed by -1.  
 
R blocks multiply the stage rotation speed by -1.  
  
=====Bumper=====
+
=====Bumper Blocks=====
The bumper acts much like a normal [[SPG:Game_Objects#Bumpers|bumper]] does (aside from the fact that the initial collision is completely different)
+
Bumper blocks act exactly like a normal [[SPG:Game_Objects#Bumpers|Bumper]] does (aside from the fact that the initial collision condition is completely different)
Measuring the angle from the centre of the bumper tile, with a repulsion force of 7.
+
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===
 
===Order of events===
Line 149: Line 151:
 
====1: Player Events====
 
====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's Block Hitting flag is true, run Jump Routine.
* After this, regardless of his Block Hitting state, Sonic will then perform his Movement routine and lastly his Falling 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 Tile''' will be reacted to (only when Block Hitting).
+
After the movement routines, the '''Current Tile''' 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.
 
Then lastly, Sonic's X Speed and Y Speed are added to his position.

Revision as of 19:36, 8 November 2022

Sonic Physics Guide
Collision
Physics
Gameplay
Presentation
Special

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 blocks (which includes special blocks such as Bumpers and Up/Down Blocks) 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 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 the Block Hitting flag is currently true). 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 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 Tile 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.