Actions

Difference between revisions of "SPG:Solid Objects"

From Sonic Retro

(Adding information.)
(Layout & Additional Info)
Line 46: Line 46:
  
 
Objects such as rings, enemies, and bumpers have a hitbox, which is a general area that will trigger some kind of reaction with Sonic when both their hitboxes overlap. Object hitboxes are centered on the object's x and y positions.  
 
Objects such as rings, enemies, and bumpers have a hitbox, which is a general area that will trigger some kind of reaction with Sonic when both their hitboxes overlap. Object hitboxes are centered on the object's x and y positions.  
 
There are various types of hitboxes that objects use: Ones for collectables like rings, ones for Badniks, ones which always hurt Sonic, and special ones that use object-specific responses. They all do different things, however, the method they use to come into contact with Sonic in the first place is all the same.
 
  
 
Sometimes objects which seem solid (like bosses or bumpers) actually only have a hitbox, and when they overlap, simply push Sonic in the other direction. As a general rule, any seemingly solid object that Sonic cannot stand on is most likely actually using a hitbox rather than real solidity.
 
Sometimes objects which seem solid (like bosses or bumpers) actually only have a hitbox, and when they overlap, simply push Sonic in the other direction. As a general rule, any seemingly solid object that Sonic cannot stand on is most likely actually using a hitbox rather than real solidity.
Line 57: Line 55:
 
[[Image:SPGHitBoxes.png]]
 
[[Image:SPGHitBoxes.png]]
  
Sonic's hitbox is much like any object's. It sits at Sonic's ''xpos'' and ''ypos''. It has a width radius of 8, and its height radius is always 3 pixels shorter than Sonic's '''Body Height Radius'''.
+
Sonic's hitbox is much like that of any other object. It sits at Sonic's ''xpos'' and ''ypos''. It has a width radius of 8, and its height radius is always 3 pixels shorter than Sonic's '''Body Height Radius''', making it 17 x 33 pixels in size while standing.
  
 
When crouching, Sonic's hitbox needs to shrink. Problem is, Sonic's position and height radius don't actually change at all while crouching. So to tackle this it manually updates the hitbox's size and position while Sonic crouches, where 12px is added to the hitbox's y position, and the hitbox's height radius is set to 10.
 
When crouching, Sonic's hitbox needs to shrink. Problem is, Sonic's position and height radius don't actually change at all while crouching. So to tackle this it manually updates the hitbox's size and position while Sonic crouches, where 12px is added to the hitbox's y position, and the hitbox's height radius is set to 10.
Line 73: Line 71:
  
 
The same is true for solid object boxes, so Sonic will push against objects 1px further away when facing leftwards than he will tiles.
 
The same is true for solid object boxes, so Sonic will push against objects 1px further away when facing leftwards than he will tiles.
 +
 +
===Hitbox Types===
 +
 +
There are various types of hitboxes that general objects use: Ones for collectables like rings, ones for Badniks, ones which always hurt Sonic, and special ones that use object-specific responses. They all do different things, however the method they use to come into contact with Sonic in the first place is all the same.
 +
 +
====Hurt Hitboxes====
 +
While these aren't solid, any contact with them will immediately give damage to Sonic. Examples are the spikes on the GHZ log bridge, the spikes under a MZ Spike Trap, and certain projectiles.
 +
 +
====Badnik Hitboxes====
 +
These are very simmilar to hurt hitboxes, with the obvious difference being that while rolling, you won't take damage and will instead destroy the enemy.
  
 
==Solid Objects==
 
==Solid Objects==
Line 82: Line 90:
 
===General Solid Object Collision===
 
===General Solid Object Collision===
  
Usually, objects which want to be solid define a width radius and height radius for their solidity area, in much the same way as objects.
+
Usually, objects which want to be solid define a width radius and height radius for their solidity area, in much the same way as hitboxes.
  
 
Though, Sonic's collision with these boxes does not involve his hitbox at all. Rather the game calculates a box the size of Sonic's sensor arrangement to be used.
 
Though, Sonic's collision with these boxes does not involve his hitbox at all. Rather the game calculates a box the size of Sonic's sensor arrangement to be used.
Line 91: Line 99:
 
To check the vertical overlap, it is much the same, though it will account for what '''Body Height Radius''' Sonic currently has, as it changes often.
 
To check the vertical overlap, it is much the same, though it will account for what '''Body Height Radius''' Sonic currently has, as it changes often.
  
If Sonic is found to be touching the object, the game will then decide whether he is to be popped out the top or bottom or the left or right of the object. The game will compare Sonic's position to the object's position to determine which side he is on. If Sonic is on the left for example and the game has decided he needs to be popped out to the object's left side, it will only do so if Sonic is moving right, towards the object. The same (but flipped) is true for colliding with the right.
+
====Finding the direction of collision====
 +
If Sonic is found to be touching the object, the game will then decide whether he is to be popped out the top or bottom or the left or right of the object. The game will compare Sonic's position to the object's position to determine which side he is on.  
 +
 
 +
====Popping Sonic Out====
 +
Once a collision has occurred and the game had decided the direction Sonic then needs to be "popped out" of the object so that he is no longer in it, and his speeds need to be changed.
  
If the game decides Sonic is to be popped out upwards, then Sonic will land on the object and the game will set a flag making him stick to the object's surface and stay grounded, even though he's not touching any Solid Tiles. This flag will remain true until Sonic walks off the object or jumps.
+
=====Popped Left and Right=====
 +
If Sonic is on the left for example and the game has decided he needs to be popped out to the object's left side, it will only do so if Sonic is moving right, towards the object. The same (but flipped) is true for colliding with the right. Both of these would halt Sonic's ''xsp'' and ''gsp''.
 +
 
 +
=====Popped Upwards=====
 +
If the game decides Sonic is to be popped out upwards, then Sonic will land on the object and the game will set a flag telling the game Sonic is on the object.
 +
 
 +
===Standing On Solid Objects===
 +
Unlike tiles, which are an organised simple grid of data which can be easily checked each frame, objects are more expensive to check for.
 +
 
 +
So when standing on top of an object, rather than check beneath Sonic each frame to ensure he's still touching it and to move him with it, the game sets a flag which will effectively glue Sonic to an object when he lands on it.
 +
 
 +
The flags job is making him stick to the object's surface and stay grounded, even though he's not touching any Solid Tiles (as far as his tile sensors are concerned, Sonic is in the air while standing on an object). This flag will only be unset when walking off the edge of an object or jumping/getting hurt.
 +
 
 +
====Bugs Using This Method====
 +
If for some reason the object you are standing on is deleted or otherwise unloaded, and the game fails to reset the object standing flag, you can then start walking through the air. This is because the flag is telling the game that Sonic is still grounded even though there's no longer any object to be grounded to. Because Sonic's grounded, he won't fall. Additionally, he also won't be able to walk off the object's sides as the object isn't even there to check for it.
  
 
==Object Specific Collision==
 
==Object Specific Collision==
  
 
While a general description of Solid Object collision may cover a pushable block or a solid rock, not all objects behave the same. Some objects have slopes, and some will change what kind of solidity they have to suit different situations.
 
While a general description of Solid Object collision may cover a pushable block or a solid rock, not all objects behave the same. Some objects have slopes, and some will change what kind of solidity they have to suit different situations.
 
===Pushable Blocks===
 
 
Pushable blocks (specifically the type found in Marble Zone) are mostly normal solid objects, except for the fact when you are pushing them they will move themselves and Sonic 1 pixel in the push direction.
 
However, they clearly do not move this fast... Well, for some reason the block will only move around every 3 frames for so, but it's not consistent and may not be 100% intentional.
 
  
 
===Item Monitor===
 
===Item Monitor===
Line 113: Line 134:
  
 
However, there is an exception. If Sonic is moving up (''ysp'' < 0) while curled, the Item Box will in fact still act solid. Additionally, if Sonic's ''ypos''-16 is smaller than the item box's y position, the item box will bounce up with a Y speed of -1.5 knocking the Item Box upwards, and Sonic's ''ysp'' will be reversed.
 
However, there is an exception. If Sonic is moving up (''ysp'' < 0) while curled, the Item Box will in fact still act solid. Additionally, if Sonic's ''ypos''-16 is smaller than the item box's y position, the item box will bounce up with a Y speed of -1.5 knocking the Item Box upwards, and Sonic's ''ysp'' will be reversed.
 +
 +
===Pushable Blocks===
 +
 +
Pushable blocks (specifically the type found in Marble Zone) are essentially normal solid objects, except for the fact when you are pushing them they will move themselves and Sonic 1 pixel in the push direction.
 +
However, they clearly do not move this fast... Well, for some reason the block will only move around every 3 frames for so, but it's not consistent and may not be 100% intentional.
  
 
[[Category:Sonic Physics Guide|Solid Objects]]
 
[[Category:Sonic Physics Guide|Solid Objects]]

Revision as of 07:13, 6 August 2020

Notes: Research applies to all four of the Mega Drive games, and Sonic CD. If there are any varying differences between the games, this will be covered below.

Variables

The following variables/constants will be referenced frequently in this section.

//Variables
xpos: The X-coordinate of Sonic's center.
ypos: The Y-coordinate of Sonic's center.
xsp: The speed at which Sonic is moving horizontally.
ysp: The speed at which Sonic is moving vertically.
gsp: The speed at which Sonic is moving on the ground.
slope: The current slope factor (slp) value being used.
ang: Sonic's angle on the ground.
	
//Constants
acc: 0.046875
dec: 0.5
frc: 0.046875 (same as acc)
top: 6
jmp: 6.5 (6 for Knuckles)
slp: 0.125
slprollup: 0.078125
slprolldown: 0.3125
fall: 2.5

Introduction

There are many objects in Sonic games and they interact with Sonic in many different ways and are a very different beast than Solid Tiles. Object-player collision doesn't work the same way as Solid Tiles. This guide will go over the collision of various objects found in the Sonic trilogy.

Setup

Hitboxes and Solid Boxes (well, any object boxes) are built using a radius for width and a radius for height. However, just like with Sonic's sensors, this also includes the objects origin.

SPGHitBoxRadius.png

So, frustratingly, a hitbox with a width radius of 8 (when defined as 8 in the code) ends up being 17 pixels wide in-game rather than 16. As a result, all hitboxes in the entire game will be an odd number in size.

Sonic's Sizes

At any given time Sonic has 3 size radiuses. Firstly, his Push Radius which always remains constant (for pushing walls). He also has his Body Width Radius and his Body Height Radius (for floor/ceiling collision) both of which will change depending on Sonic's state.

Note: Information on these and how they change can be found at Solid Tiles. For specific object hitbox sizes, see Game Objects.

Object Hitboxes

Objects such as rings, enemies, and bumpers have a hitbox, which is a general area that will trigger some kind of reaction with Sonic when both their hitboxes overlap. Object hitboxes are centered on the object's x and y positions.

Sometimes objects which seem solid (like bosses or bumpers) actually only have a hitbox, and when they overlap, simply push Sonic in the other direction. As a general rule, any seemingly solid object that Sonic cannot stand on is most likely actually using a hitbox rather than real solidity.

Sonic's Hitbox

In order to interact with other object's hitboxes, Sonic needs his own.

SPGHitBoxes.png

Sonic's hitbox is much like that of any other object. It sits at Sonic's xpos and ypos. It has a width radius of 8, and its height radius is always 3 pixels shorter than Sonic's Body Height Radius, making it 17 x 33 pixels in size while standing.

When crouching, Sonic's hitbox needs to shrink. Problem is, Sonic's position and height radius don't actually change at all while crouching. So to tackle this it manually updates the hitbox's size and position while Sonic crouches, where 12px is added to the hitbox's y position, and the hitbox's height radius is set to 10.

Quirks With Hitboxes

Because these hitboxes aren't even numbered in size, and because object origins don't lay perfectly centred between pixels (rather they are 1px left and down) most hitboxes will also appear 1px too big on the right side and the bottom side. This is simply how things work in game and for that reason won't be ignored. Sprites like rings are even numbered sizes (such as 16 x 16) so an anomaly like the following can (and does, always) occur.

SPGRingTest.gif

Rings can be collected from one direction sooner than the other, you can try it yourself via debug mode. As long as the sprite of the ring is 4px out from the tiles on each side, you'll experience this inconsistency. A Ring's hitbox is defined as a radius of 6, but this results in a box with a width of 13 rather than 12. Because all sprites like this are an even size, but their hitbox must be odd, the box cannot be perfectly set on the sprite and will be larger to the left and bottom.

This is the case with any object's hitboxes.

The same is true for solid object boxes, so Sonic will push against objects 1px further away when facing leftwards than he will tiles.

Hitbox Types

There are various types of hitboxes that general objects use: Ones for collectables like rings, ones for Badniks, ones which always hurt Sonic, and special ones that use object-specific responses. They all do different things, however the method they use to come into contact with Sonic in the first place is all the same.

Hurt Hitboxes

While these aren't solid, any contact with them will immediately give damage to Sonic. Examples are the spikes on the GHZ log bridge, the spikes under a MZ Spike Trap, and certain projectiles.

Badnik Hitboxes

These are very simmilar to hurt hitboxes, with the obvious difference being that while rolling, you won't take damage and will instead destroy the enemy.

Solid Objects

Note: This section is a work in progress. While the information here is accurate, it may not paint the entire picture at this time.

As stated above, object collision is totally separate from tile collision. Sonic does not collide with objects using his solid tile sensors, instead, special calculations are used to check if Sonic's general shape is inside an object's solid box, and push him out.

General Solid Object Collision

Usually, objects which want to be solid define a width radius and height radius for their solidity area, in much the same way as hitboxes.

Though, Sonic's collision with these boxes does not involve his hitbox at all. Rather the game calculates a box the size of Sonic's sensor arrangement to be used.

Checking for an overlap

To check for an overlap horizontally, the object combines its radius with Sonic's Push Radius, and if the absolute distance between Sonic's xpos and the object's x position is less than this combined radius then an overlap has occurred.

To check the vertical overlap, it is much the same, though it will account for what Body Height Radius Sonic currently has, as it changes often.

Finding the direction of collision

If Sonic is found to be touching the object, the game will then decide whether he is to be popped out the top or bottom or the left or right of the object. The game will compare Sonic's position to the object's position to determine which side he is on.

Popping Sonic Out

Once a collision has occurred and the game had decided the direction Sonic then needs to be "popped out" of the object so that he is no longer in it, and his speeds need to be changed.

Popped Left and Right

If Sonic is on the left for example and the game has decided he needs to be popped out to the object's left side, it will only do so if Sonic is moving right, towards the object. The same (but flipped) is true for colliding with the right. Both of these would halt Sonic's xsp and gsp.

Popped Upwards

If the game decides Sonic is to be popped out upwards, then Sonic will land on the object and the game will set a flag telling the game Sonic is on the object.

Standing On Solid Objects

Unlike tiles, which are an organised simple grid of data which can be easily checked each frame, objects are more expensive to check for.

So when standing on top of an object, rather than check beneath Sonic each frame to ensure he's still touching it and to move him with it, the game sets a flag which will effectively glue Sonic to an object when he lands on it.

The flags job is making him stick to the object's surface and stay grounded, even though he's not touching any Solid Tiles (as far as his tile sensors are concerned, Sonic is in the air while standing on an object). This flag will only be unset when walking off the edge of an object or jumping/getting hurt.

Bugs Using This Method

If for some reason the object you are standing on is deleted or otherwise unloaded, and the game fails to reset the object standing flag, you can then start walking through the air. This is because the flag is telling the game that Sonic is still grounded even though there's no longer any object to be grounded to. Because Sonic's grounded, he won't fall. Additionally, he also won't be able to walk off the object's sides as the object isn't even there to check for it.

Object Specific Collision

While a general description of Solid Object collision may cover a pushable block or a solid rock, not all objects behave the same. Some objects have slopes, and some will change what kind of solidity they have to suit different situations.

Item Monitor

Item Monitors, as you may have noticed, are not always solid. While you can stand on them you can also go right through them while jumping or rolling. The game is actually checking what Sonic is doing and changing how the Item Monitor will react.

While not curled up in a ball, the Item Monitor acts as solid. The Item Monitor's hitbox isn't accessible at this time.

While curled, however, the item box will no longer have any solidity at all, and its hitbox is active and accessible. The hitbox collision is what destroys the Item Box and bounces Sonic off (Details in Monitors Rebound).

However, there is an exception. If Sonic is moving up (ysp < 0) while curled, the Item Box will in fact still act solid. Additionally, if Sonic's ypos-16 is smaller than the item box's y position, the item box will bounce up with a Y speed of -1.5 knocking the Item Box upwards, and Sonic's ysp will be reversed.

Pushable Blocks

Pushable blocks (specifically the type found in Marble Zone) are essentially normal solid objects, except for the fact when you are pushing them they will move themselves and Sonic 1 pixel in the push direction. However, they clearly do not move this fast... Well, for some reason the block will only move around every 3 frames for so, but it's not consistent and may not be 100% intentional.