2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "collisionManager.h"
20 #include <bear/debug.h>
21 #include <bear/Vector2.h>
22 #include <bear/mathw.h>
25 #include "Entities/Ball.h"
26 #include "Entities/Polygon.h"
27 #include "Entities/PhysicsEntity.h"
29 #include "CollisionInfo.h"
31 /// ***** Private Method Headers *****
33 static void clearEntities();
34 static void placeEntity(PhysicsEntity*);
35 static void updateEntities();
37 static void applyCollision(PhysicsEntity*, PhysicsEntity*);
38 static void applyCollision(Ball*, Ball*);
39 static void applyCollision(Polygon*, Ball*);
41 static bool getInfo(const Ball*, const Ball*, CollisionInfo*);
42 static bool getInfo(const Polygon*, const Ball*, CollisionInfo*);
44 /// ***** Private Variables *****
46 static const int sc_ixDivisions = 20;
47 static const int sc_iyDivisions = 16;
48 static const int sc_ixScreenSize = 800;
49 static const int sc_iyScreenSize = 600;
51 setPhys divisions[sc_ixDivisions][sc_iyDivisions];
53 /// ***** Initializers/Cleaners *****
55 void collision::init()
59 void collision::clean()
64 /// ***** Public Methods *****
66 void collision::update(setPhys& sp)
70 for( setPhys::iterator it = sp.begin();
80 /// ***** Private Methods *****
92 divisions[x][y].clear();
97 void placeEntity(PhysicsEntity* ppe)
105 Ball* pBall = dynamic_cast<Ball*>(ppe);
109 const float& xb = pBall->positionRaw().m_fX;
110 const float& yb = pBall->positionRaw().m_fY;
111 const float& rad = pBall->m_radius;
113 vecMin.m_fX = xb - rad;
114 vecMin.m_fY = yb - rad;
115 vecMax.m_fX = xb + rad;
116 vecMax.m_fY = yb + rad;
123 Polygon* pPoly = dynamic_cast<Polygon*>(ppe);
127 vecMin = pPoly->m_minP;
128 vecMax = pPoly->m_maxP;
134 DPF(0, "ENTITY TYPE NOT SUPPORTED BY placeEntity()!!");
138 for( int x = static_cast<int>( vecMin.m_fX / (sc_ixScreenSize / sc_ixDivisions) );
139 x <= static_cast<int>( vecMax.m_fX / (sc_ixScreenSize / sc_ixDivisions) );
142 if(x < 0 || sc_ixDivisions <= x)
145 for( int y = static_cast<int>( vecMin.m_fY / (sc_iyScreenSize / sc_iyDivisions) );
146 y <= static_cast<int>( vecMax.m_fY / (sc_iyScreenSize / sc_iyDivisions) );
149 if(y < 0 || sc_iyDivisions <= y)
152 divisions[x][y].insert(ppe);
157 void updateEntities()
167 for( setPhys::iterator it1 = divisions[x][y].begin();
168 it1 != divisions[x][y].end();
171 for( setPhys::iterator it2 = divisions[x][y].begin();
172 it2 != divisions[x][y].end();
178 applyCollision(*it1, *it2);
185 void applyCollision(PhysicsEntity* ppe1, PhysicsEntity* ppe2)
187 DASSERT(ppe1 != NULL && ppe2 != NULL);
190 Ball* pb1 = dynamic_cast<Ball*>(ppe1);
191 Ball* pb2 = dynamic_cast<Ball*>(ppe2);
193 if( pb1 != NULL && pb2 != NULL )
195 applyCollision(pb1, pb2);
201 Polygon* pPoly = dynamic_cast<Polygon*>(ppe1);
202 Ball* pBall = dynamic_cast<Ball*>(ppe2);
204 if( pPoly != NULL && pBall != NULL )
206 applyCollision(pPoly, pBall);
212 Polygon* pPoly = dynamic_cast<Polygon*>(ppe2);
213 Ball* pBall = dynamic_cast<Ball*>(ppe1);
215 if( pPoly != NULL && pBall != NULL )
217 applyCollision(pPoly, pBall);
222 DPF(0, "ENTITY TYPE NOT SUPPORTED BY applyCollision()!!");
225 void applyCollision(Ball* pb1, Ball* pb2)
227 DASSERT(pb1 != NULL && pb2 != NULL);
231 if(!getInfo(pb1, pb2, &cInfo))
234 // a few values to simplify the equations
235 const Vector2& vecNormal = cInfo.m_vecNormal;
236 const Vector2& vecPoint = cInfo.m_vecPoint;
238 float m1 = pb1->m_mass;
239 float m2 = pb2->m_mass;
241 float e = (pb1->m_CoR + pb2->m_CoR) / 2;
243 Vector2 v1 = pb1->velocityRaw();
244 Vector2 v2 = pb2->velocityRaw();
247 float iTop = -(e + 1) * (v1 - v2).dot(vecNormal);
249 // otherwise the collision happened and we do the math the below assumes
250 // collisions have no friction
252 float impulse = iTop / (vecNormal.dot(vecNormal) * (1 / m1 + 1 / m2));
254 pb1->applyImpulse(impulse / m1 * vecNormal, vecPoint);
255 pb2->applyImpulse(-impulse / m2 * vecNormal, vecPoint);
258 void applyCollision(Polygon* pPoly, Ball* pBall)
260 DASSERT(pPoly != NULL && pBall != NULL);
264 if(!getInfo(pPoly, pBall, &cInfo))
267 // a few values to simplify the equations
268 const Vector2& vecNorm = cInfo.m_vecNormal;
270 float fCoR = pBall->m_CoR;
271 Vector2 vecVelo = pBall->velocityRaw();
273 // impulse divided by mass
274 float idm = (-(fCoR + 1) * vecVelo.dot(vecNorm))
275 / (vecNorm.dot(vecNorm));
277 pBall->applyImpulse(idm * vecNorm);
280 // CoR penetration fix, adds the polygon-ball jitters
282 // from center to point
283 Vector2 vecCollP = vecNorm / vecNorm.length() * pBall->m_radius;
284 pBall->applyNudge(cInfo.m_vecPoint + vecCollP - pBall->positionRaw());
287 bool getInfo(const Ball* pb1, const Ball* pb2, CollisionInfo* pcInfo)
289 // a few values to simplify the equations
290 float r1 = pb1->m_radius;
291 float r2 = pb2->m_radius;
293 Vector2 p1 = pb1->positionRaw();
294 Vector2 p2 = pb2->positionRaw();
295 Vector2 v1 = pb1->velocityRaw();
296 Vector2 v2 = pb2->velocityRaw();
298 // quick binding box check
299 if (p1.m_fX - r1 > p2.m_fX + r2
300 || p1.m_fX + r1 < p2.m_fX - r2
301 || p1.m_fY - r1 > p2.m_fY + r2
302 || p1.m_fY + r1 < p2.m_fY - r2)
305 // test if not touching
306 if ((p1 - p2).sqrLength() >= (r1 + r2)*(r1 + r2))
309 // test if they are moving apart in some way if they are it's likely
310 // that they collided last frame and are still overlapping
312 if ((v1 - v2).dot(p1 - p2) >= 0)
315 pcInfo->m_vecPoint = p1 - (p1 - p2) * r1 / (r1 + r2);
316 pcInfo->m_vecNormal = p1 - p2;
321 bool getInfo(const Polygon* pPoly, const Ball* pBall, CollisionInfo* pcInfo)
323 // a few values to simplify the equations
324 float fRad = pBall->m_radius;
325 Vector2 vecPos = pBall->positionRaw();
326 Vector2 vecVelo = pBall->velocityRaw();
328 float fMaxX = pPoly->m_maxP.m_fX;
329 float fMinX = pPoly->m_minP.m_fX;
330 float fMaxY = pPoly->m_maxP.m_fY;
331 float fMinY = pPoly->m_minP.m_fY;
333 // quick binding box check
334 if (vecPos.m_fX - fRad > fMaxX || vecPos.m_fX + fRad < fMinX ||
335 vecPos.m_fY - fRad > fMaxY || vecPos.m_fY + fRad < fMinY)
343 const vector<Vector2>& pts = pPoly->m_points;
344 unsigned int num = pts.size();
346 for (unsigned int i = 0; i < num; i++)
348 Vector2 vec = vectorToLine(vecPos,
351 pts[(i + 1) % num].m_fX,
352 pts[(i + 1) % num].m_fY);
354 if (vec.sqrLength() <= fRad*fRad && 0 < vec.dot(vecVelo))
366 pcInfo->m_vecPoint = vecTotVec / iTot + vecPos;
367 pcInfo->m_vecNormal = vecPos - pcInfo->m_vecPoint;