The Table Engine
Collide Class Reference

Component to handle collisions between game objects. More...

#include <collide.h>

+ Inheritance diagram for Collide:
+ Collaboration diagram for Collide:

Public Member Functions

 Collide ()
 Constructor for the Collide component. More...
 
 ~Collide ()
 Destructor for the Collide component. More...
 
SDL_Rect * getRect ()
 Gets the current collision rectangle. More...
 
Vec2 getScreenPosition ()
 Retrieves the screen position of the collision rectangle. More...
 
Vec2 getScreenSize ()
 Retrieves the size of the collision rectangle on the screen. More...
 
void preventCollision (Collide *anotherCollide, float &dx, float &dy)
 Modifies velocities to prevent collision with another Collide component. More...
 
void setTransformOffset (int x, int y)
 Sets the offset of the collider from the transform's position. More...
 
void setScreenPosition (int x, int y)
 Sets the screen position of the collider. More...
 
void setScreenPosition (Vec2 pos)
 Sets the screen position of the collider using a Vec2 object. More...
 
void setScreenSize (int w, int h)
 Sets the size of the collider on the screen. More...
 
void setScreenSize (Vec2 size)
 Sets the size of the collider on the screen using a Vec2 object. More...
 
bool isColliding (SDL_Rect *rect)
 Checks if this collider is colliding with a given SDL_Rect. More...
 
bool isColliding (Collide *collide)
 Checks if this collider is colliding with another Collide component. More...
 
void update ()
 Updates the collision state of the component. More...
 
void render ()
 Renders the collision boundaries for debugging purposes. More...
 
- Public Member Functions inherited from Component
 Component ()
 Default constructor for the Component. More...
 
 Component (std::string name)
 Constructs a Component with a specified name. More...
 
virtual ~Component ()
 Virtual destructor for safe polymorphic use. More...
 
std::string getName ()
 Retrieves the name of the component. More...
 
GameObjectgetOwner ()
 Retrieves the GameObject that owns this component. More...
 
void setName (std::string name)
 Sets the name of the component. More...
 
void setOwner (GameObject *owner)
 Sets the GameObject owner of this component. More...
 
virtual void input ()
 Virtual method for handling input, intended to be overridden by derived components. More...
 

Private Member Functions

SDL_Rect * nextRect (float dx, float dy)
 Calculates the position of the collider for the next frame. More...
 

Private Attributes

SDL_Rect * mCollide
 The collision rectangle relative to the screen. More...
 
float mOffsetX
 
float mOffsetY
 Offset of the collider's position from the game object's transform position. More...
 

Additional Inherited Members

- Protected Attributes inherited from Component
std::string m_name
 Name of the component, useful for identification or debugging. More...
 
GameObjectm_owner
 Pointer to the GameObject that owns this component. More...
 

Detailed Description

Component to handle collisions between game objects.

Collide uses SDL_Rect structures to define bounding boxes for collision detection and response. It integrates with the game object's position and allows dynamic adjustment of the collision box based on the game object's movements and interactions.

Definition at line 24 of file collide.h.

Constructor & Destructor Documentation

◆ Collide()

Collide::Collide ( )

Constructor for the Collide component.

Definition at line 11 of file collide.cpp.

◆ ~Collide()

Collide::~Collide ( )

Destructor for the Collide component.

Definition at line 20 of file collide.cpp.

Member Function Documentation

◆ getRect()

SDL_Rect * Collide::getRect ( )

Gets the current collision rectangle.

Returns
A pointer to an SDL_Rect representing the current collision rectangle.

Definition at line 25 of file collide.cpp.

◆ getScreenPosition()

Vec2 Collide::getScreenPosition ( )

Retrieves the screen position of the collision rectangle.

Returns
A Vec2 object representing the screen position.

Definition at line 78 of file collide.cpp.

◆ getScreenSize()

Vec2 Collide::getScreenSize ( )

Retrieves the size of the collision rectangle on the screen.

Returns
A Vec2 object representing the size of the collision rectangle.

Definition at line 83 of file collide.cpp.

◆ isColliding() [1/2]

bool Collide::isColliding ( Collide collide)

Checks if this collider is colliding with another Collide component.

Parameters
collidePointer to another Collide component to check against.
Returns
True if colliding, false otherwise.

Definition at line 123 of file collide.cpp.

◆ isColliding() [2/2]

bool Collide::isColliding ( SDL_Rect *  rect)

Checks if this collider is colliding with a given SDL_Rect.

Parameters
rectPointer to an SDL_Rect to check against.
Returns
True if colliding, false otherwise.

Definition at line 118 of file collide.cpp.

◆ nextRect()

SDL_Rect * Collide::nextRect ( float  dx,
float  dy 
)
private

Calculates the position of the collider for the next frame.

Parameters
dxHorizontal displacement.
dyVertical displacement.
Returns
A pointer to an SDL_Rect representing the next frame's collider position.

Definition at line 30 of file collide.cpp.

◆ preventCollision()

void Collide::preventCollision ( Collide anotherCollide,
float &  dx,
float &  dy 
)

Modifies velocities to prevent collision with another Collide component.

Parameters
anotherCollidePointer to another Collide component.
dxReference to the horizontal velocity that may be modified.
dyReference to the vertical velocity that may be modified.

Definition at line 35 of file collide.cpp.

◆ render()

void Collide::render ( )
virtual

Renders the collision boundaries for debugging purposes.

Reimplemented from Component.

Definition at line 146 of file collide.cpp.

◆ setScreenPosition() [1/2]

void Collide::setScreenPosition ( int  x,
int  y 
)

Sets the screen position of the collider.

Parameters
xHorizontal screen position.
yVertical screen position.

Definition at line 94 of file collide.cpp.

◆ setScreenPosition() [2/2]

void Collide::setScreenPosition ( Vec2  pos)

Sets the screen position of the collider using a Vec2 object.

Parameters
posVec2 object representing the new position.

Definition at line 106 of file collide.cpp.

◆ setScreenSize() [1/2]

void Collide::setScreenSize ( int  w,
int  h 
)

Sets the size of the collider on the screen.

Parameters
wWidth of the collider.
hHeight of the collider.

Definition at line 100 of file collide.cpp.

◆ setScreenSize() [2/2]

void Collide::setScreenSize ( Vec2  size)

Sets the size of the collider on the screen using a Vec2 object.

Parameters
sizeVec2 object representing the new size.

Definition at line 112 of file collide.cpp.

◆ setTransformOffset()

void Collide::setTransformOffset ( int  x,
int  y 
)

Sets the offset of the collider from the transform's position.

Parameters
xHorizontal offset.
yVertical offset.

Definition at line 88 of file collide.cpp.

◆ update()

void Collide::update ( )
virtual

Updates the collision state of the component.

Reimplemented from Component.

Definition at line 128 of file collide.cpp.

Member Data Documentation

◆ mCollide

SDL_Rect* Collide::mCollide
private

The collision rectangle relative to the screen.

Definition at line 26 of file collide.h.

◆ mOffsetX

float Collide::mOffsetX
private

Definition at line 28 of file collide.h.

◆ mOffsetY

float Collide::mOffsetY
private

Offset of the collider's position from the game object's transform position.

Definition at line 28 of file collide.h.


The documentation for this class was generated from the following files: