Actions

Difference between revisions of "SPG:Main Game Loop"

From Sonic Retro

(Cleanup and consistency, added SPG table of contents)
m (More links)
 
(12 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
==Introduction==
 
==Introduction==
  
In order to gain a full picture of how Sonic games work, as well as knowing what physically happens the order of events is just as important.  
+
In order to gain a full picture of how classic Sonic games work, the order of events is just as important as knowing what physically happens during them.
  
 
==Characters==
 
==Characters==
Characters are the first objects to run their code.
+
Characters/Players are the first objects to run their code.
  
 
Their main events depend on their current state.
 
Their main events depend on their current state.
  
Note: ''While the main lists below are in order, the sub lists, though also numbered, are merely descriptive.''
+
{| class="prettytable" style="width: auto;"
<div class="large-12 columns">
+
!Normal
<div class="large-4 columns">
+
!Rolling
===While Normal===
+
!Airborne
 
+
|-
"Normal" means when Sonic is not airborne or rolling.
+
|"Normal" means any time the Player is not airborne or rolling.
 
 
 
# Check for special animations that prevent control (such as balancing).
 
# Check for special animations that prevent control (such as balancing).
# Check for starting a spindash.
+
# Check for starting a spindash while crouched.
# Adjust Ground Speed based on current Ground Angle ([[SPG:Slope_Physics#Slope_Factor|Slope Factor]]).
+
# Adjust '''''Ground Speed''''' based on current '''''Ground Angle''''' ([[SPG:Slope_Physics#Slowing_Down_Uphill_And_Speeding_Up_Downhill|Slope Factor]]).
 
# Check for starting a jump.
 
# Check for starting a jump.
# Update Ground Speed based on directional input and apply friction/deceleration.
+
# Update '''''Ground Speed''''' based on directional input and apply friction/deceleration.
# Check for starting ducking, balancing on ledges, etc.
+
# Check for starting crouching, balancing on ledges, etc.
# [[SPG:Solid_Tiles#Wall_Sensors_.28E_and_F.29|Wall sensor collision]] occurs.
+
# [[SPG:Slope_Collision#Push_Sensors_.28Grounded.29|Push Sensor collision]] occurs.
## Which sensors are used varies based on the the [[SPG:Solid_Tiles#While_Grounded|sensor activation]].
+
#* Which sensors are used varies based on the the [[SPG:Slope_Collision#Grounded_Sensor_Activation|sensor activation]].
## Note: ''This occurs before Sonic's position physically moves, meaning he might not actually be touching the wall yet, the game accounts for this by adding Sonic's X Speed and Y Speed to the sensor's position.
+
#* This occurs before the Player's position physically moves, meaning they might not actually be touching the wall yet, the game accounts for this by adding the Player's '''''X Speed''''' and '''''Y Speed''''' to the sensor's position.
# Check for starting a roll.
+
# Check for [[SPG:Rolling#Criteria|starting a roll]].
# Handle camera boundaries (keep Sonic inside the view and kill Sonic if he touches the kill plane).
+
# Handle camera boundaries (keep the Player inside the view and kill them if they touch the kill plane).
# Move Sonic
+
# Move the Player object
## Update X Position and Y Position based on X Speed and Y Speed.
+
#* Calculate [[SPG:Slope_Physics#Moving_Along_Slopes|''X Speed'' and ''Y Speed'' from ''Ground Speed'' and ''Ground Angle'']].
# [[SPG:Solid_Tiles#Floor_Sensors_.28A_and_B.29|Floor sensor collision]] occurs.
+
#* Updates '''''X Position''''' and '''''Y Position''''' based on '''''X Speed''''' and '''''Y Speed'''''.
## Update Sonic's Ground Angle & position.
+
# Grounded [[SPG:Slope_Collision#Ground_Sensors_.28Grounded.29|Ground Sensor collision]] occurs.
## Adhere to level of terrain or become airborne if none found/too low.
+
#* Updates the Player's '''''Ground Angle'''''.
# Check for falling when Ground Speed is too low on walls/ceilings.
+
#* Align the Player to surface of terrain or become airborne if none found.
</div>
+
# Check for [[SPG:Slope_Physics#Falling_and_Slipping_Down_Slopes|slipping/falling]] when '''''Ground Speed''''' is too low on walls/ceilings.
<div class="large-4 columns">
+
|"Rolling" means any time the Player is curled up into a ball on the ground.
===While Rolling===
+
# Adjust '''''Ground Speed''''' based on current '''''Ground Angle''''' ([[SPG:Slope_Physics#Slowing_Down_Uphill_And_Speeding_Up_Downhill|Rolling Slope Factors]]).
 
 
# Adjust Ground Speed based on current Ground Angle ([[SPG:Slope_Physics#Slope_Factor|Rolling Slope Factors]]).
 
 
# Check for starting a jump.
 
# Check for starting a jump.
# Update Ground Speed based on directional input and apply [[SPG:Running#Friction|friction]].
+
# Update '''''Ground Speed''''' based on directional input and apply [[SPG:Running#Friction|friction]].
# [[SPG:Solid_Tiles#Wall_Sensors_.28E_and_F.29|Wall sensor collision]] occurs.
+
# [[SPG:Slope_Collision#Push_Sensors_.28Grounded.29|Push Sensor collision]] occurs.
## Which sensors are used varies based on the the [[SPG:Solid_Tiles#While_Grounded|sensor activation]].
+
#* Which sensors are used varies based on the the [[SPG:Slope_Collision#Grounded_Sensor_Activation|sensor activation]].
## Note: ''This occurs before Sonic's position physically moves, meaning he might not actually be touching the wall yet, the game accounts for this by adding Sonic's X Speed and Y Speed to the sensor's position.  
+
#* This occurs before the Player's position physically moves, meaning he might not actually be touching the wall yet, the game accounts for this by adding the Player's '''''X Speed''''' and '''''Y Speed''''' to the sensor's position.  
# Handle camera boundaries (keep Sonic inside the view and kill Sonic if he touches the kill plane).
+
# Handle camera boundaries (keep the Player inside the view and kill them if they touch the kill plane).
# Move Sonic
+
# Move the Player object
## Update X Position and Y Position based on X Speed and Y Speed.
+
#* Calculate [[SPG:Slope_Physics#Moving_Along_Slopes|''X Speed'' and ''Y Speed'' from ''Ground Speed'' and ''Ground Angle'']].
# [[SPG:Solid_Tiles#Floor_Sensors_.28A_and_B.29|Floor sensor collision]] occurs.
+
#* Update '''''X Position''''' and '''''Y Position''''' based on '''''X Speed''''' and '''''Y Speed'''''.
## Update Sonic's Ground Angle & position.
+
# Grounded [[SPG:Slope_Collision#Ground_Sensors_.28Grounded.29|Ground Sensor collision]] occurs.
## Adhere to level of terrain or become airborne if none found/too low.
+
#* Updates the Player's '''''Ground Angle'''''.
# Check for falling when Ground Speed is too low on walls/ceilings.
+
#* Align the Player to surface of terrain or become airborne if none found.
</div>
+
# Check for [[SPG:Slope_Physics#Falling_and_Slipping_Down_Slopes|slipping/falling]] when '''''Ground Speed''''' is too low on walls/ceilings.
<div class="large-4 columns">
 
===While Airborne===
 
 
 
"Airborne" means when Sonic is falling or jumping or otherwise not grounded.
 
  
 +
|"Airborne" means when the Player is falling or jumping or otherwise not grounded.
 
# Check for jump button release ([[SPG:Jumping#Jump_Velocity|variable jump velocity]]).
 
# Check for jump button release ([[SPG:Jumping#Jump_Velocity|variable jump velocity]]).
 
# Check for turning Super.
 
# Check for turning Super.
# Update X Speed based on directional input.
+
# Update '''''X Speed''''' based on directional input.
 
# Apply [[SPG:Air_State#Air_Drag|air drag]].
 
# Apply [[SPG:Air_State#Air_Drag|air drag]].
# Move Sonic
+
# Move the Player object
## Update X Position and Y Position based on X Speed and Y Speed.
+
#* Updates '''''X Position''''' and '''''Y Position''''' based on '''''X Speed''''' and '''''Y Speed'''''.
 
# Apply [[SPG:Air_State#Gravity|gravity]].
 
# Apply [[SPG:Air_State#Gravity|gravity]].
## Update Y Speed by adding ''grv'' to it.
+
#* Update Y Speed by adding gravity to it.
## Note: ''this happens after Sonic's position was updated. This is an important detail for ensuring Sonic's jump height is correct.''
+
#* This happens after the Player's position was updated. This is an important detail for ensuring the Player's jump height is correct.
 
# Check underwater for reduced gravity.
 
# Check underwater for reduced gravity.
# Rotate angle [[SPG:Air_State#Air_Rotation|back to 0]].
+
# Rotate '''''Ground Angle''''' [[SPG:Air_State#Air_Rotation|back to 0]].
# All collision checks occurs here.
+
# All air collision checks occur here.
## The sensors used depend on the [[SPG:Solid_Tiles#While_Airborne|sensor activation]].
+
#* The sensors used depend on the [[SPG:Slope_Collision#While_Airborne|sensor activation]].
## Wall collision occurs first.
+
#* Active [[SPG:Slope_Collision#Push_Sensors_.28Airborne.29|Airborne Push Sensors]] check first, then the active [[SPG:Slope_Collision#Ground_Sensors_.28Airborne.29|Airborne Ground Sensors]]/[[SPG:Slope_Collision#Ceiling_Sensors|Ceiling Sensors]] second.
</div>
+
|}
</div>
 
 
 
After moving the Player will check for nearby object hitboxes to trigger an overlap with. It it important to note this happens from Sonic's side, so before any objects run their code or act solid.
 
  
After this, the special objects are executed.
+
===Hitbox===
 +
After moving, the Player will check for nearby object hitboxes for overlap with theirs. It it important to note this happens from the Player's code, so before any objects run their code or act solid.
  
 
==Special objects==
 
==Special objects==
Line 86: Line 78:
  
 
==General Objects==
 
==General Objects==
 +
Object movement for things like enemies, bosses, gimmicks and any other objects found in a zone and their collisions with the terrain occur here.
 +
 +
The things listed below are in no particular order. Every object is coded separately and execute their code and actions in various orders within their routines.
 +
 +
===Actions And Movement===
 +
General game objects run their various routines with actions in whatever order they are designed with. Their position could be the first thing to update, or it could be the last. They could update their '''''X Speed''''' and '''''Y Speed''''' before or after the position updates. It depends on the object.
 +
 +
Note:
 +
* See [[SPG:Game Objects|Game Objects]] and [[SPG:Game Enemies|Game Enemies]] for descriptions of the actions that general objects will execute here.
 +
 +
===Solid Collision With Player===
 +
Player object collision with general objects happens here too. As stated in [[SPG:Solid_Objects#Solid_Objects|Solid Objects]], solid objects run their own code to push the Player out rather than the Player object itself detecting them. This allows the game to run less code per frame, because the player isn't checking every object before object's are looped to run their own code.  Player collision with an object could happen anywhere in an object's routine, also.
  
Object movement and collision occurs here.
+
===Trigger Areas===
Player collision with objects happens here too. As stated in [[SPG:Game_Objects|Game Objects]], solid objects push the Player out rather than the Player object itself detecting them.
+
If the object uses a [[SPG:Hitboxes#Trigger_Areas|trigger area]], overlap with that will be checked somewhere within the object's code, using the Player's current position.
Every object is different and execute their code in various orders.
 
  
 +
===Order Of General Objects===
 +
General objects themselves can be processed in any kind of order, objects are loaded into available memory at any time as they are needed. So an order or priority of general objects isn't really accounted for.
  
 
[[Category:Sonic Physics Guide|Main Game Loop]]
 
[[Category:Sonic Physics Guide|Main Game Loop]]

Latest revision as of 03:35, 30 July 2023

Sonic Physics Guide
Collision
Physics
Gameplay
Presentation
Special

Notes:

Introduction

In order to gain a full picture of how classic Sonic games work, the order of events is just as important as knowing what physically happens during them.

Characters

Characters/Players are the first objects to run their code.

Their main events depend on their current state.

Normal Rolling Airborne
"Normal" means any time the Player is not airborne or rolling.
  1. Check for special animations that prevent control (such as balancing).
  2. Check for starting a spindash while crouched.
  3. Adjust Ground Speed based on current Ground Angle (Slope Factor).
  4. Check for starting a jump.
  5. Update Ground Speed based on directional input and apply friction/deceleration.
  6. Check for starting crouching, balancing on ledges, etc.
  7. Push Sensor collision occurs.
    • Which sensors are used varies based on the the sensor activation.
    • This occurs before the Player's position physically moves, meaning they might not actually be touching the wall yet, the game accounts for this by adding the Player's X Speed and Y Speed to the sensor's position.
  8. Check for starting a roll.
  9. Handle camera boundaries (keep the Player inside the view and kill them if they touch the kill plane).
  10. Move the Player object
  11. Grounded Ground Sensor collision occurs.
    • Updates the Player's Ground Angle.
    • Align the Player to surface of terrain or become airborne if none found.
  12. Check for slipping/falling when Ground Speed is too low on walls/ceilings.
"Rolling" means any time the Player is curled up into a ball on the ground.
  1. Adjust Ground Speed based on current Ground Angle (Rolling Slope Factors).
  2. Check for starting a jump.
  3. Update Ground Speed based on directional input and apply friction.
  4. Push Sensor collision occurs.
    • Which sensors are used varies based on the the sensor activation.
    • This occurs before the Player's position physically moves, meaning he might not actually be touching the wall yet, the game accounts for this by adding the Player's X Speed and Y Speed to the sensor's position.
  5. Handle camera boundaries (keep the Player inside the view and kill them if they touch the kill plane).
  6. Move the Player object
  7. Grounded Ground Sensor collision occurs.
    • Updates the Player's Ground Angle.
    • Align the Player to surface of terrain or become airborne if none found.
  8. Check for slipping/falling when Ground Speed is too low on walls/ceilings.
"Airborne" means when the Player is falling or jumping or otherwise not grounded.
  1. Check for jump button release (variable jump velocity).
  2. Check for turning Super.
  3. Update X Speed based on directional input.
  4. Apply air drag.
  5. Move the Player object
    • Updates X Position and Y Position based on X Speed and Y Speed.
  6. Apply gravity.
    • Update Y Speed by adding gravity to it.
    • This happens after the Player's position was updated. This is an important detail for ensuring the Player's jump height is correct.
  7. Check underwater for reduced gravity.
  8. Rotate Ground Angle back to 0.
  9. All air collision checks occur here.

Hitbox

After moving, the Player will check for nearby object hitboxes for overlap with theirs. It it important to note this happens from the Player's code, so before any objects run their code or act solid.

Special objects

Next, objects executed are those which setup certain special events, like title cards.

After this, general objects are executed.

General Objects

Object movement for things like enemies, bosses, gimmicks and any other objects found in a zone and their collisions with the terrain occur here.

The things listed below are in no particular order. Every object is coded separately and execute their code and actions in various orders within their routines.

Actions And Movement

General game objects run their various routines with actions in whatever order they are designed with. Their position could be the first thing to update, or it could be the last. They could update their X Speed and Y Speed before or after the position updates. It depends on the object.

Note:

Solid Collision With Player

Player object collision with general objects happens here too. As stated in Solid Objects, solid objects run their own code to push the Player out rather than the Player object itself detecting them. This allows the game to run less code per frame, because the player isn't checking every object before object's are looped to run their own code. Player collision with an object could happen anywhere in an object's routine, also.

Trigger Areas

If the object uses a trigger area, overlap with that will be checked somewhere within the object's code, using the Player's current position.

Order Of General Objects

General objects themselves can be processed in any kind of order, objects are loaded into available memory at any time as they are needed. So an order or priority of general objects isn't really accounted for.