#include "mathw.h"
-/// ***** Private Class Header *****
-
/// ***** Private Method Headers *****
-void clearEntities();
-void placeEntity(PhysicsEntity* p);
-void updateEntities();
+static void clearEntities();
+static void placeEntity(PhysicsEntity*);
+static void updateEntities();
-void applyCollision(PhysicsEntity* p1, PhysicsEntity* p2);
-void applyCollision(Ball* b1, Ball* b2);
-void applyCollision(Polygon* pPoly, Ball* pBall);
+static void applyCollision(PhysicsEntity*, PhysicsEntity*);
+static void applyCollision(Ball*, Ball*);
+static void applyCollision(Polygon*, Ball*);
-bool getInfo(const Ball* b1, const Ball* b2, CollisionInfo* cInfo);
-bool getInfo(const Polygon* pPoly, const Ball* pBall, CollisionInfo* cInfo);
+static bool getInfo(const Ball*, const Ball*, CollisionInfo*);
+static bool getInfo(const Polygon*, const Ball*, CollisionInfo*);
/// ***** Private Variables *****
-const int xDivisions = 20;
-const int yDivisions = 16;
-const int screenX = 800;
-const int screenY = 600;
+static const int sc_ixDivisions = 20;
+static const int sc_iyDivisions = 16;
+static const int sc_ixScreenSize = 800;
+static const int sc_iyScreenSize = 600;
-setPhys divisions[xDivisions][yDivisions];
+setPhys divisions[sc_ixDivisions][sc_iyDivisions];
/// ***** Initializers/Cleaners *****
void clearEntities()
{
for( int x = 0;
- x < xDivisions;
+ x < sc_ixDivisions;
x++ )
{
for( int y = 0;
- y < yDivisions;
+ y < sc_iyDivisions;
y++ )
{
divisions[x][y].clear();
}
}
-void placeEntity(PhysicsEntity* p)
+void placeEntity(PhysicsEntity* ppe)
{
- Vector2 minP;
- Vector2 maxP;
+ DASSERT(ppe != NULL);
+
+ Vector2 vecMin;
+ Vector2 vecMax;
{ // Ball case
- Ball* b = dynamic_cast<Ball*>(p);
+ Ball* pBall = dynamic_cast<Ball*>(ppe);
- if( b != NULL )
+ if( pBall != NULL )
{
- const float& xb = b->positionRaw().m_fX;
- const float& yb = b->positionRaw().m_fY;
- const float& rad = b->radius;
+ const float& xb = pBall->positionRaw().m_fX;
+ const float& yb = pBall->positionRaw().m_fY;
+ const float& rad = pBall->radius;
- minP.m_fX = xb - rad;
- minP.m_fY = yb - rad;
- maxP.m_fX = xb + rad;
- maxP.m_fY = yb + rad;
+ vecMin.m_fX = xb - rad;
+ vecMin.m_fY = yb - rad;
+ vecMax.m_fX = xb + rad;
+ vecMax.m_fY = yb + rad;
goto start;
}
}
{ // Polygon case
- Polygon* pPoly = dynamic_cast<Polygon*>(p);
+ Polygon* pPoly = dynamic_cast<Polygon*>(ppe);
if( pPoly != NULL )
{
- minP = pPoly->minP;
- maxP = pPoly->maxP;
+ vecMin = pPoly->minP;
+ vecMax = pPoly->maxP;
goto start;
}
return;
start:
- for( int x = static_cast<int>( minP.m_fX / (screenX / xDivisions) );
- x <= static_cast<int>( maxP.m_fX / (screenX / xDivisions) );
+ for( int x = static_cast<int>( vecMin.m_fX / (sc_ixScreenSize / sc_ixDivisions) );
+ x <= static_cast<int>( vecMax.m_fX / (sc_ixScreenSize / sc_ixDivisions) );
x++ )
{
- if(x < 0 || xDivisions <= x)
+ if(x < 0 || sc_ixDivisions <= x)
break;
- for( int y = static_cast<int>( minP.m_fY / (screenY / yDivisions) );
- y <= static_cast<int>( maxP.m_fY / (screenY / yDivisions) );
+ for( int y = static_cast<int>( vecMin.m_fY / (sc_iyScreenSize / sc_iyDivisions) );
+ y <= static_cast<int>( vecMax.m_fY / (sc_iyScreenSize / sc_iyDivisions) );
y++ )
{
- if(y < 0 || yDivisions <= y)
+ if(y < 0 || sc_iyDivisions <= y)
break;
- divisions[x][y].insert(p);
+ divisions[x][y].insert(ppe);
}
}
}
void updateEntities()
{
for( int x = 0;
- x < xDivisions;
+ x < sc_ixDivisions;
x++ )
{
for( int y = 0;
- y < yDivisions;
+ y < sc_iyDivisions;
y++ )
{
for( setPhys::iterator it1 = divisions[x][y].begin();
}
}
-void applyCollision(PhysicsEntity* p1, PhysicsEntity* p2)
+void applyCollision(PhysicsEntity* ppe1, PhysicsEntity* ppe2)
{
- {
- Ball* b1 = dynamic_cast<Ball*>(p1);
- Ball* b2 = dynamic_cast<Ball*>(p2);
+ DASSERT(ppe1 != NULL && ppe2 != NULL);
- if( b1 != NULL && b2 != NULL )
+ {// Ball vs Ball
+ Ball* pb1 = dynamic_cast<Ball*>(ppe1);
+ Ball* pb2 = dynamic_cast<Ball*>(ppe2);
+
+ if( pb1 != NULL && pb2 != NULL )
{
- applyCollision(b1, b2);
+ applyCollision(pb1, pb2);
return;
}
}
- {
- Polygon* pPoly = dynamic_cast<Polygon*>(p1);
- Ball* pBall = dynamic_cast<Ball*>(p2);
+ {// Polygon vs Ball
+ Polygon* pPoly = dynamic_cast<Polygon*>(ppe1);
+ Ball* pBall = dynamic_cast<Ball*>(ppe2);
if( pPoly != NULL && pBall != NULL )
{
}
}
- {
- Polygon* pPoly = dynamic_cast<Polygon*>(p2);
- Ball* pBall = dynamic_cast<Ball*>(p1);
+ {// Ball vs Polygon
+ Polygon* pPoly = dynamic_cast<Polygon*>(ppe2);
+ Ball* pBall = dynamic_cast<Ball*>(ppe1);
if( pPoly != NULL && pBall != NULL )
{
DPF(0, "ENTITY TYPE NOT SUPPORTED BY applyCollision()!!");
}
-void applyCollision(Ball* b1, Ball* b2)
+void applyCollision(Ball* pb1, Ball* pb2)
{
+ DASSERT(pb1 != NULL && pb2 != NULL);
+
CollisionInfo cInfo;
- if(!getInfo(b1, b2, &cInfo))
+ if(!getInfo(pb1, pb2, &cInfo))
return;
// a few values to simplify the equations
- const Vector2& normal = cInfo.m_vecNormal;
- const Vector2& point = cInfo.m_vecPoint;
+ const Vector2& vecNormal = cInfo.m_vecNormal;
+ const Vector2& vecPoint = cInfo.m_vecPoint;
- float m1 = b1->mass;
- float m2 = b2->mass;
+ float m1 = pb1->mass;
+ float m2 = pb2->mass;
- float e = (b1->CoR + b2->CoR) / 2;
+ float e = (pb1->CoR + pb2->CoR) / 2;
- Vector2 v1 = b1->velocityRaw();
- Vector2 v2 = b2->velocityRaw();
+ Vector2 v1 = pb1->velocityRaw();
+ Vector2 v2 = pb2->velocityRaw();
- float iTop = -(e + 1) * (v1 - v2).dot(normal);
+ float iTop = -(e + 1) * (v1 - v2).dot(vecNormal);
// otherwise the collision happened and we do the math the below assumes
// collisions have no friction
- float impulse = iTop / (normal.dot(normal) * (1 / m1 + 1 / m2));
+ float impulse = iTop / (vecNormal.dot(vecNormal) * (1 / m1 + 1 / m2));
- b1->applyImpulse(impulse / m1 * normal, point);
- b2->applyImpulse(-impulse / m2 * normal, point);
+ pb1->applyImpulse(impulse / m1 * vecNormal, vecPoint);
+ pb2->applyImpulse(-impulse / m2 * vecNormal, vecPoint);
}
void applyCollision(Polygon* pPoly, Ball* pBall)
{
+ DASSERT(pPoly != NULL && pBall != NULL);
+
CollisionInfo cInfo;
if(!getInfo(pPoly, pBall, &cInfo))
return;
- float fCoR = pBall->CoR;
- Vector2 vecNorm = cInfo.m_vecNormal;
+ // a few values to simplify the equations
+ const Vector2& vecNorm = cInfo.m_vecNormal;
+ float fCoR = pBall->CoR;
Vector2 vecVelo = pBall->velocityRaw();
// impulse divided by mass
pBall->applyImpulse(idm * vecNorm);
// HACK
- // CoR penetration fix, adds the jitters
+ // CoR penetration fix, adds the polygon-ball jitters
// from center to point
Vector2 vecCollP = vecNorm / vecNorm.length() * pBall->radius;
pBall->applyNudge(cInfo.m_vecPoint + vecCollP - pBall->positionRaw());
}
-bool getInfo(const Ball* b1, const Ball* b2, CollisionInfo* pcInfo)
+bool getInfo(const Ball* pb1, const Ball* pb2, CollisionInfo* pcInfo)
{
// a few values to simplify the equations
- float r1 = b1->radius;
- float r2 = b2->radius;
+ float r1 = pb1->radius;
+ float r2 = pb2->radius;
- Vector2 p1 = b1->positionRaw();
- Vector2 p2 = b2->positionRaw();
- Vector2 v1 = b1->velocityRaw();
- Vector2 v2 = b2->velocityRaw();
+ Vector2 p1 = pb1->positionRaw();
+ Vector2 p2 = pb2->positionRaw();
+ Vector2 v1 = pb1->velocityRaw();
+ Vector2 v2 = pb2->velocityRaw();
// quick binding box check
if (p1.m_fX - r1 > p2.m_fX + r2
if ((p1 - p2).sqrLength() >= (r1 + r2)*(r1 + r2))
return false;
- // test if they are moving apart in some way if they aren't it's likely
+ // test if they are moving apart in some way if they are it's likely
// that they collided last frame and are still overlapping
if ((v1 - v2).dot(p1 - p2) >= 0)