The Table Engine
collide.cpp
Go to the documentation of this file.
1 #include "SDL2/SDL.h"
2 
3 #include <iostream>
4 
5 #include "collide.h"
6 #include "transform.h"
7 #include "gameobject.h"
8 #include "scenemanager.h"
9 #include "vec2.h"
10 
12 {
13  setName("collide");
14  mCollide = new SDL_Rect();
15 
16  mOffsetX = 0;
17  mOffsetY = 0;
18 }
19 
21 {
22  delete mCollide;
23 }
24 
25 SDL_Rect* Collide::getRect()
26 {
27  return mCollide;
28 }
29 
30 SDL_Rect* Collide::nextRect(float dx, float dy)
31 {
32  return new SDL_Rect{(int) (mCollide->x + dx), (int) (mCollide->y + dy), mCollide->w, mCollide->h};
33 }
34 
35 void Collide::preventCollision(Collide* anotherCollide, float& dx, float& dy)
36 {
37  if (dx == 0 && dy == 0) return;
38 
39  auto secondRect = anotherCollide->getRect();
40 
41  // if the two colliders are already intersecting, apply a backward force to separate them
42  SDL_Rect* intersectRect = new SDL_Rect();
43  bool intersects = SDL_IntersectRect(mCollide, secondRect, intersectRect);
44  if (intersects)
45  {
46  bool hitFromSide = intersectRect->w < intersectRect->h;
47  if (hitFromSide)
48  {
49  bool isRight = intersectRect->x > mCollide->x;
50  dx = (isRight ? 1 : -1) * -1;
51  dy = 0;
52  return;
53  }
54  else
55  {
56  bool isUp = intersectRect->y > mCollide->y;
57  dx = 0;
58  dy = (isUp ? 1 : -1) * -1;
59  return;
60  }
61  }
62 
63  // freely move collider in x direction if no collision
64  auto nextFrameRect = nextRect(dx, 0);
65  if (SDL_HasIntersection(nextFrameRect, secondRect))
66  {
67  dx = 0;
68  }
69 
70  // freely move collider in y direction if no collision
71  nextFrameRect = nextRect(0, dy);
72  if (SDL_HasIntersection(nextFrameRect, secondRect))
73  {
74  dy = 0;
75  }
76 }
77 
79 {
80  return Vec2(mCollide->x, mCollide->y);
81 }
82 
84 {
85  return Vec2(mCollide->w, mCollide->h);
86 }
87 
88 void Collide::setTransformOffset(int x, int y)
89 {
90  mOffsetX = x;
91  mOffsetY = y;
92 }
93 
94 void Collide::setScreenPosition(int x, int y)
95 {
96  mCollide->x = x;
97  mCollide->y = y;
98 }
99 
100 void Collide::setScreenSize(int w, int h)
101 {
102  mCollide->w = w;
103  mCollide->h = h;
104 }
105 
107 {
108  mCollide->x = pos.x;
109  mCollide->y = pos.y;
110 }
111 
113 {
114  mCollide->w = size.x;
115  mCollide->h = size.y;
116 }
117 
118 bool Collide::isColliding(SDL_Rect* rect)
119 {
120  return SDL_HasIntersection(mCollide, rect);
121 }
122 
124 {
125  return SDL_HasIntersection(mCollide, collide->getRect());
126 }
127 
129 {
130  auto transform = m_owner->getComponent<Transform>();
131  if (transform == NULL) return;
132 
133  // collider's screen position equals transform's screen position + offset
134  float x = transform->getScreenPosition().x;
135  float y = transform->getScreenPosition().y;
137 
138  // collider's screen size equals transform's screen size unless manually set
139  if (getScreenSize() == Vec2(0, 0))
140  {
141  setScreenSize(transform->getScreenSize());
142  }
143 }
144 
145 // solely for debugging/visual purposes
147 {
148  // if (m_owner->getTag() == "Player")
149  // {
150  // SDL_SetRenderDrawColor(SceneManager::getInstance().getRenderer(), 255, 0, 255, 255);
151  // SDL_RenderDrawRect(SceneManager::getInstance().getRenderer(), mCollide);
152  // }
153 
154  // if (m_owner->getTag() == "Bow")
155  // {
156  // SDL_SetRenderDrawColor(SceneManager::getInstance().getRenderer(), 0, 255, 0, 255);
157  // SDL_RenderDrawRect(SceneManager::getInstance().getRenderer(), mCollide);
158  // }
159 
160  // if (m_owner->getTag() == "Arrow")
161  // {
162  // SDL_SetRenderDrawColor(SceneManager::getInstance().getRenderer(), 0, 255, 255, 255);
163  // SDL_RenderDrawRect(SceneManager::getInstance().getRenderer(), mCollide);
164  // }
165 
166  // if (m_owner->getTag() == "Key")
167  // {
168  // SDL_SetRenderDrawColor(SceneManager::getInstance().getRenderer(), 255, 255, 0, 255);
169  // SDL_RenderDrawRect(SceneManager::getInstance().getRenderer(), mCollide);
170  // }
171 }
Component to handle collisions between game objects.
Definition: collide.h:24
SDL_Rect * mCollide
The collision rectangle relative to the screen.
Definition: collide.h:26
float mOffsetX
Definition: collide.h:28
void preventCollision(Collide *anotherCollide, float &dx, float &dy)
Modifies velocities to prevent collision with another Collide component.
Definition: collide.cpp:35
~Collide()
Destructor for the Collide component.
Definition: collide.cpp:20
void setScreenSize(int w, int h)
Sets the size of the collider on the screen.
Definition: collide.cpp:100
float mOffsetY
Offset of the collider's position from the game object's transform position.
Definition: collide.h:28
void render()
Renders the collision boundaries for debugging purposes.
Definition: collide.cpp:146
void setTransformOffset(int x, int y)
Sets the offset of the collider from the transform's position.
Definition: collide.cpp:88
SDL_Rect * nextRect(float dx, float dy)
Calculates the position of the collider for the next frame.
Definition: collide.cpp:30
void setScreenPosition(int x, int y)
Sets the screen position of the collider.
Definition: collide.cpp:94
Collide()
Constructor for the Collide component.
Definition: collide.cpp:11
Vec2 getScreenSize()
Retrieves the size of the collision rectangle on the screen.
Definition: collide.cpp:83
bool isColliding(SDL_Rect *rect)
Checks if this collider is colliding with a given SDL_Rect.
Definition: collide.cpp:118
Vec2 getScreenPosition()
Retrieves the screen position of the collision rectangle.
Definition: collide.cpp:78
void update()
Updates the collision state of the component.
Definition: collide.cpp:128
SDL_Rect * getRect()
Gets the current collision rectangle.
Definition: collide.cpp:25
GameObject * m_owner
Pointer to the GameObject that owns this component.
Definition: component.h:28
void setName(std::string name)
Sets the name of the component.
Definition: component.cpp:19
T * getComponent()
Gets a component of the specified type.
Definition: gameobject.h:123
Component for handling the transformation of game objects.
Definition: transform.h:23
Vec2 getScreenPosition()
Get the screen space position calculated from world space.
Definition: transform.cpp:39
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.
Definition: vec2.h:16
float y
Y coordinate of the vector.
Definition: vec2.h:18
float x
X coordinate of the vector.
Definition: vec2.h:17
Provides the Transform component for positioning and sizing game objects.
Definition of Vec2 structure for 2D vector operations.