-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionSystem.cpp
147 lines (124 loc) · 4.75 KB
/
CollisionSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* The CollisionSystem tests for collisions between entities with a Collider component
*
* Author: Skylar Payne
* Date: 8/5/2013
* File: CollisionSystem.h
**/
#include "CollisionSystem.h"
#include "PositionComponent.h"
#include "ColliderComponent.h"
#include "Entity.h"
/**
* @brief CollisionSystem::OnMessage Checks for collisions when an entity moves
* @param msg holds entity ID
*/
void CollisionSystem::OnMessage(EntityMovedMessage& msg)
{
if(this->GetEntity(msg.ID)->HasComponent("Collider"))
{
_MovedEntities.insert(msg.ID);
}
}
/**
* @brief CollisionSystem::CheckCollision checks for a collision between two entities
* @param e1 the first entity
* @param e2 the second entity
* @param norm the normal of collision, if they collided
* @return true if the two entities are colliding, false otherwise
*/
bool CollisionSystem::CheckCollision(Entity* e1, Entity* e2, sf::Vector2f& norm)
{
sf::Vector2f const& p1 = e1->GetComponent<PositionComponent>("Position")->GetPosition();
sf::Vector2f const& p2 = e2->GetComponent<PositionComponent>("Position")->GetPosition();
ColliderComponent* c1 = e1->GetComponent<ColliderComponent>("Collider");
ColliderComponent* c2 = e2->GetComponent<ColliderComponent>("Collider");
//Reset the normal vector to avoid any miscalculations later on
norm.x = 0.f;
norm.y = 0.f;
//Find the respective x and y distances... Used later to determine level of penetration
float distx, disty, absdistx, absdisty = 0.f;
distx = (p2.x + c2->_Offset.x + c2->_Dimensions.x / 2) - (p1.x + c1->_Offset.x + c1->_Dimensions.x / 2);
disty = (p2.y + c2->_Offset.y + c2->_Dimensions.y / 2) - (p1.y + c1->_Offset.y + c1->_Dimensions.y / 2);
//Make sure both distances are positive... Direction doesn't matter
(distx < 0) ? absdistx = distx * -1: absdistx = distx;
(disty < 0) ? absdisty = disty * -1 : absdisty = disty;
//Use these combined half dimensions along with distances to determine level of penetration
float halfWidths = (c1->_Dimensions.x + c2->_Dimensions.x) / 2;
float halfHeights = (c1->_Dimensions.y + c2->_Dimensions.y) / 2;
//If both distances are greater than or equal to the half dimensions, no collision, return false.
if(!((absdistx < halfWidths) && (absdisty < halfHeights)))
return false;
//Otherwise, there was a collision, let's find where it happened!
//overlap is determined by the distance in half dimensions and distances
float xMag = halfWidths - absdistx;
float yMag = halfHeights - absdisty;
//Least significant overlap determines surface of collision...
if(xMag < yMag)
norm.x = (distx > 0) ? xMag : -xMag;
else
norm.y = (disty > 0) ? yMag : -yMag;
return true;
}
/**
* @brief CollisionSystem::Update checks for collisions between entities that have moved and all other entities
* @param dt frame time
*/
void CollisionSystem::Update(unsigned int dt)
{
Entity* e1 = nullptr;
Entity* e2 = nullptr;
PositionComponent* p1 = nullptr;
sf::Vector2f norm;
std::set<unsigned int> movedEntities = _MovedEntities;
std::set<unsigned int>::iterator mit;
std::set<unsigned int>::iterator eit;
for(mit = movedEntities.begin(); mit != movedEntities.end(); mit++)
{
for(eit = _EntitiesToUpdate.begin(); eit != _EntitiesToUpdate.end(); eit++)
{
if(*mit == *eit)
{
continue;
}
e1 = this->GetEntity(*mit);
e2 = this->GetEntity(*eit);
p1 = e1->GetComponent<PositionComponent>("Position");
if(CheckCollision(e1, e2, norm))
{
MoveEntityMessage msg;
msg.ID = *mit;
msg.newPosition.x = p1->GetPosition().x - norm.x;
msg.newPosition.y = p1->GetPosition().y - norm.y;
Emit<MoveEntityMessage>(msg);
CollisionMessage cmsg;
cmsg.ID1 = *mit;
cmsg.ID2 = *eit;
cmsg.norm.x = norm.x;
cmsg.norm.y = norm.y;
Emit<CollisionMessage>(cmsg);
continue;
}
}
_MovedEntities.erase(*mit);
}
}
/**
* @brief CollisionSystem::ValidateEntity validates an entity to update
* @param ID the entity to validate
*/
bool CollisionSystem::ValidateEntity(unsigned int ID)
{
Entity* e = this->GetEntity(ID);
bool rtn = e->HasComponent("Collider") && e->HasComponent("Position");
if(rtn)
{
EntityMovedMessage msg;
msg.ID = ID;
sf::Vector2f const& pos = e->GetComponent<PositionComponent>("Position")->GetPosition();
msg.oldPosition = pos;
msg.newPosition = pos;
Emit<EntityMovedMessage>(msg);
}
return rtn;
}