37 if (dx == 0 && dy == 0)
return;
39 auto secondRect = anotherCollide->
getRect();
42 SDL_Rect* intersectRect =
new SDL_Rect();
43 bool intersects = SDL_IntersectRect(
mCollide, secondRect, intersectRect);
46 bool hitFromSide = intersectRect->w < intersectRect->h;
49 bool isRight = intersectRect->x >
mCollide->x;
50 dx = (isRight ? 1 : -1) * -1;
56 bool isUp = intersectRect->y >
mCollide->y;
58 dy = (isUp ? 1 : -1) * -1;
64 auto nextFrameRect =
nextRect(dx, 0);
65 if (SDL_HasIntersection(nextFrameRect, secondRect))
72 if (SDL_HasIntersection(nextFrameRect, secondRect))
120 return SDL_HasIntersection(
mCollide, rect);
131 if (transform == NULL)
return;
135 float y = transform->getScreenPosition().y;
Component to handle collisions between game objects.
SDL_Rect * mCollide
The collision rectangle relative to the screen.
void preventCollision(Collide *anotherCollide, float &dx, float &dy)
Modifies velocities to prevent collision with another Collide component.
~Collide()
Destructor for the Collide component.
void setScreenSize(int w, int h)
Sets the size of the collider on the screen.
float mOffsetY
Offset of the collider's position from the game object's transform position.
void render()
Renders the collision boundaries for debugging purposes.
void setTransformOffset(int x, int y)
Sets the offset of the collider from the transform's position.
SDL_Rect * nextRect(float dx, float dy)
Calculates the position of the collider for the next frame.
void setScreenPosition(int x, int y)
Sets the screen position of the collider.
Collide()
Constructor for the Collide component.
Vec2 getScreenSize()
Retrieves the size of the collision rectangle on the screen.
bool isColliding(SDL_Rect *rect)
Checks if this collider is colliding with a given SDL_Rect.
Vec2 getScreenPosition()
Retrieves the screen position of the collision rectangle.
void update()
Updates the collision state of the component.
SDL_Rect * getRect()
Gets the current collision rectangle.
GameObject * m_owner
Pointer to the GameObject that owns this component.
void setName(std::string name)
Sets the name of the component.
T * getComponent()
Gets a component of the specified type.
Defines the Collide class, a component for managing collision detection and response.
Defines the SceneManager class for managing scenes in the game.
A structure to represent 2D vectors.
float y
Y coordinate of the vector.
float x
X coordinate of the vector.
Definition of Vec2 structure for 2D vector operations.