Operation Null Mind 0.4.1
A 2D roguelike top down shooter game made with SDL2
 
Loading...
Searching...
No Matches
colliders.h File Reference

Defines the collider system for collision detection. More...

#include <SDL.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  Collider
 This struct represents a collider. More...
 
struct  ColliderCheckResult
 

Macros

#define MAX_COLLISIONS_PER_CHECK   20
 
#define MAX_COLLIDABLES   1024
 

Typedefs

typedef struct Collider Collider
 This struct represents a collider.
 
typedef struct ColliderCheckResult ColliderCheckResult
 

Enumerations

enum  CollisionLayer {
  COLLISION_LAYER_NONE = 0 , COLLISION_LAYER_PLAYER = 1 << 0 , COLLISION_LAYER_ENVIRONMENT = 1 << 1 , COLLISION_LAYER_ENEMY = 1 << 2 ,
  COLLISION_LAYER_TRIGGER = 1 << 3 , COLLISION_LAYER_ENEMY_PROJECTILE = 1 << 4 , COLLISION_LAYER_PLAYER_PROJECTILE = 1 << 5
}
 

Functions

void Collider_Start ()
 Initialize the collision manager.
 
void Collider_Register (Collider *collidable, void *owner)
 Register a collider with the collision manager.
 
bool Collider_Check (Collider *collidableObject, ColliderCheckResult *result)
 Check for collisions with a collider.
 
void Collider_Reset (Collider *collider)
 Deactivates a collider.
 

Variables

Collider * ColliderList [MAX_COLLIDABLES]
 
int ColliderCount
 

Detailed Description

Defines the collider system for collision detection.

This file contains the declarations for the collision manager. The collision manager is responsible for detecting collisions between entities in the game.

Author
Mango, Tony and Darren
Date
2025-03-09

Usage Examples

TLDR: Create Collider struct somewhere, register it, check for collisions, and handle the collisions.

Step 1: Create a collider struct for your entity. <br>

Note: the collider needs to be stored in a memory that's reusable. (i.e static/global variables)

Example:

// Static variables
static Collider playerCollider = {
// (If you're making the hitbox follow something, only the last two values (width and height) matter)
.hitbox = {0, 0, 20, 20},
.collidesWith = COLLISION_LAYER_ENVIRONMENT
};
// Global variables (View PlayerData struct and player variable)
.hitbox = {0, 0, 20, 20},
.collidesWith = COLLISION_LAYER_ENVIRONMENT
};
@ COLLISION_LAYER_ENEMY
Definition colliders.h:83
@ COLLISION_LAYER_ENVIRONMENT
Definition colliders.h:82
@ COLLISION_LAYER_PLAYER
Definition colliders.h:81
PlayerData player
[Data] The player's initial data and configuration
Definition player_data.c:22
This struct represents a collider.
Definition colliders.h:96
SDL_Rect hitbox
The hitbox of the collider.
Definition colliders.h:97
PlayerState state
Current player state.
Definition player.h:144
Collider collider
Player collision data.
Definition player.h:104

This collider will check for collision with environment and enemies.

Step 2: Register the collider

Register the collider with the collision manager:

Example:

Collider_Register(&playerCollider, &player);
void Collider_Register(Collider *collider, void *owner)
[Start] Registers a collider to the Colliders array.
Definition colliders.c:36

This will add the collider to the list of colliders that the collision manager checks.

Step 3: Check for collisions

Check for collisions with the collider:

Example:

Collider_Check(&playerCollider, &result);
bool Collider_Check(Collider *collider, ColliderCheckResult *checkResult)
[PostUpdate] Checks if a collider is intersecting with any of its collider layers.
Definition colliders.c:67
Definition colliders.h:113

This will check for collisions with the collider and store the results in the result struct. After that, you can do something with the detected collisions:

for (int i = 0; i < result.count; i++) {
Collider* collisionObject = result.objects[i];
// Do something with the collision object
switch(collisionObject->layer) {
// Handle collision with environment, for example stopping movement
break;
// Handle collision with enemy, for example taking damage
break;
}
}
Collider * objects[MAX_COLLISIONS_PER_CHECK]
An array of detected colliders.
Definition colliders.h:114
int count
Number of collisions detected.
Definition colliders.h:115
CollisionLayer layer
What layer this object belongs to.
Definition colliders.h:98

Macro Definition Documentation

◆ MAX_COLLIDABLES

#define MAX_COLLIDABLES   1024

◆ MAX_COLLISIONS_PER_CHECK

#define MAX_COLLISIONS_PER_CHECK   20

Typedef Documentation

◆ Collider

typedef struct Collider Collider

This struct represents a collider.

A collider is a hitbox that can be used to detect collisions between objects. It is used to detect collisions between entities in the game.

Todo:
Add a system to follow/attach colliders to entities.

◆ ColliderCheckResult

Enumeration Type Documentation

◆ CollisionLayer

Enumerator
COLLISION_LAYER_NONE 
COLLISION_LAYER_PLAYER 
COLLISION_LAYER_ENVIRONMENT 
COLLISION_LAYER_ENEMY 
COLLISION_LAYER_TRIGGER 
COLLISION_LAYER_ENEMY_PROJECTILE 
COLLISION_LAYER_PLAYER_PROJECTILE 

Function Documentation

◆ Collider_Check()

bool Collider_Check ( Collider *  collider,
ColliderCheckResult *  checkResult 
)

Check for collisions with a collider.

Parameters
collidableObjectThe collider to check
resultWhere to store the collision results
Returns
true if any collisions were detected

Check for collisions with a collider.

This function checks for collision between a collider and everything else in the ColliderList array. Any colliders whose layer is not in the input collider's collidesWith section will be ignored.

Parameters
colliderThe input collider
checkResultThe checkResult of the collider, which includes 2 members: checkResult.objects: an array of detected colliders checkResult.count: number of collisions detected
Returns
true if collision detected, false otherwise

◆ Collider_Register()

void Collider_Register ( Collider *  collider,
void *  owner 
)

Register a collider with the collision manager.

Parameters
collidableThe collider to register
ownerThe entity that owns this collider

Register a collider with the collision manager.

This is so that every colliders can be checked by each other.

Parameters
colliderA pointer to the collider struct. For example: &player.state.collider
ownerA pointer to the owner of the collider. For example: &player

◆ Collider_Reset()

void Collider_Reset ( Collider *  collider)

Deactivates a collider.

Parameters
colliderThe collider to deactivate

Deactivates a collider.

Parameters
colliderThe collider to reset

◆ Collider_Start()

void Collider_Start ( )

Initialize the collision manager.

Initialize the collision manager.

Variable Documentation

◆ ColliderCount

int ColliderCount
extern

◆ ColliderList

Collider* ColliderList[MAX_COLLIDABLES]
extern