Actions

SPG:Hitboxes

From Sonic Retro

Revision as of 07:34, 26 November 2022 by LapperDev (talk | contribs) (Created page "SPG:Hitboxes" to avoid clutter in the large solid objects page & easier navigation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Sonic Physics Guide
Collision
Physics
Gameplay
Presentation
Special

Notes:

  • The research applies to all four of the Sega Mega Drive games and Sonic CD.
  • There are 2 main ways objects interact with the Player. Hitboxes like rings and bumpers, and solidity like spikes or push blocks.
  • Hitboxes and trigger areas for specific objects will are shown in Game Objects.

Introduction

The Player and other objects such as rings, enemies, and bumpers have a hitbox, which is a general rectangular area that will trigger some kind of reaction with the Player when both their hitboxes overlap. Object hitboxes are centered on the object's X and Y Positions.

Hitbox Sizes

While objects can freely define their Width Radius and Height Radius as they choose, when it comes to their hitboxes, they have to choose from a predefined list of radius size pairs.

Here's an snippet example list of some of the available hitbox radius vaues, taken from Sonic 3 & Knuckles

[4],4]
[20,20]
[12,20]
[20,12]
[4,16]
[12,18]
[16,16]
[6,6]
[24,12]
[12,16]
[16,8]
[8,8]
[20,16]
[20,8]
[14,14]
[24,24]
[40,16]
[16,24]
[8,16]
...

In your own game, you do not need to set hitbox sizes up like this as it provides no extra accuracy while playing, however it's worth noting as a limiation the original games had.

Overlapping

Hitboxes have to touch, well - overlap, to react. Overlap is the keyword here, if any pixel of the Player's hitbox is overlapping a pixel of an object's hitbox this will trigger the reaction from the object. The hitboxes just being right next to each other does not count.

Hitbox Reaction

So when they touch, some kind of reaction will occur. The type of which is determined by the hitbox type (which is actually just a general "reaction type" variable the objects have):

Attackable Hitboxes

These hitboxes are used by both Badniks and Bosses, and are attackable when curled but damage the Player when they are not curled up.

Routine Counter Increment Hitboxes

This type of hitbox simply increases moves to the next routine of the object, used by objects like Rings and Item Monitors.

Hurt Hitboxes

Any contact with these will immediately give damage to the Player. Examples are the spikes on the GHZ log bridge, the spikes under a MZ Spike Trap (though most spikes don't use hitboxes at all and are solid objects), and certain projectiles.

Special Hitboxes

These hitboxes result in different reactions based specific conditions, used by objects like the body segments of Caterkillers.

The Player's Hitbox

In order to interact with other object's hitboxes, the Player needs their own.

SPGHitBoxes.png The hitbox for Sonic.

The Player's hitbox is much like that of any other object. It sits at the their X Position and Y Position. It has a width radius of 8, and its height radius is always 3 pixels shorter than the Player's Height Radius, making it 17 X 33 pixels in size while standing.

When crouching, obviously the Player's hitbox needs to shrink. Problem is, the Player's position and Height Radius don't actually change at all while crouching. So to tackle this the game manually updates the hitbox's size and position while the Player 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 (see Basics) 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.

Trigger Areas

Sometimes an object will forgo the hitbox system entirely and instead perform it's own custom checks on the Player's position, checking if it is within some kind of rectangle. An example of this is the Checkpoint object.

SPGCheckpointTrigger.png

This serves the same purpose as hitboxes, but is fundamentally different. For one, the check happens within the object's own code, while hitboxes are checked at the end of the Player's code. Secondly, a trigger can be a rectangle of any size, without using radius values. So they will be described as having a top left position, and a complete width and complete height. Just a normal rectangle. What you see is what you get with these.

Because these are totally separate and do not involve the Player's hitbox at all, they will be differentiated as "Triggers" or trigger areas.