refactored Vector2
authorPatrik Gornicz <Gornicz.P@gmail.com>
Wed, 21 Jan 2009 23:14:59 +0000 (18:14 -0500)
committerPatrik Gornicz <Gornicz.P@gmail.com>
Wed, 21 Jan 2009 23:14:59 +0000 (18:14 -0500)
src/Effects/Screen.cpp
src/Entities/Polygon.cpp
src/Vector2.cpp
src/Vector2.h
src/collisionManager.cpp
src/graphics/graphics.cpp
src/mathw.cpp

index 9c0f283..770a456 100644 (file)
@@ -46,17 +46,17 @@ Vector2 Screen::positionDelta(const PhysicsEntity* e, float time_step) const
         radius = b->getRadius();
 
 
-    if(pos.y > 600-radius)
-        acc.y += 600-radius - pos.y;
+    if(pos.m_fY > 600-radius)
+        acc.m_fY += 600-radius - pos.m_fY;
 
-    if(pos.y < 0+radius)
-        acc.y += 0+radius - pos.y;
+    if(pos.m_fY < 0+radius)
+        acc.m_fY += 0+radius - pos.m_fY;
 
-    if(pos.x > 800-radius)
-        acc.x += 800-radius - pos.x;
+    if(pos.m_fX > 800-radius)
+        acc.m_fX += 800-radius - pos.m_fX;
 
-    if(pos.x < 0+radius)
-        acc.x += 0+radius - pos.x;
+    if(pos.m_fX < 0+radius)
+        acc.m_fX += 0+radius - pos.m_fX;
 
     return acc;
 }
@@ -75,17 +75,17 @@ Vector2 Screen::velocityDelta(const PhysicsEntity* e, float time_step) const
         radius = b->getRadius();
 
 
-    if(pos.y > 600-radius && velo.y > 0)
-        acc.y += velo.y * -(1 + CoR);
+    if(pos.m_fY > 600-radius && velo.m_fY > 0)
+        acc.m_fY += velo.m_fY * -(1 + CoR);
 
-    if(pos.y < 0+radius && velo.y < 0)
-        acc.y += velo.y * -(1 + CoR);
+    if(pos.m_fY < 0+radius && velo.m_fY < 0)
+        acc.m_fY += velo.m_fY * -(1 + CoR);
 
-    if(pos.x > 800-radius && velo.x > 0)
-        acc.x += velo.x * -(1 + CoR);
+    if(pos.m_fX > 800-radius && velo.m_fX > 0)
+        acc.m_fX += velo.m_fX * -(1 + CoR);
 
-    if(pos.x < 0+radius && velo.x < 0)
-        acc.x += velo.x * -(1 + CoR);
+    if(pos.m_fX < 0+radius && velo.m_fX < 0)
+        acc.m_fX += velo.m_fX * -(1 + CoR);
 
     return acc;
 }
index ecaf2c4..06955c3 100644 (file)
@@ -55,11 +55,11 @@ void Polygon::createBindingBox()
 
     for(unsigned int i=1; i<points.size(); i++)
     {
-             if(points[i].x < minP.x) minP.x = points[i].x;
-        else if(points[i].x > maxP.x) maxP.x = points[i].x;
+             if(points[i].m_fX < minP.m_fX) minP.m_fX = points[i].m_fX;
+        else if(points[i].m_fX > maxP.m_fX) maxP.m_fX = points[i].m_fX;
 
-             if(points[i].y < minP.y) minP.y = points[i].y;
-        else if(points[i].y > maxP.y) maxP.y = points[i].y;
+             if(points[i].m_fY < minP.m_fY) minP.m_fY = points[i].m_fY;
+        else if(points[i].m_fY > maxP.m_fY) maxP.m_fY = points[i].m_fY;
     }
 }
 
index d13ea95..2a8c8af 100644 (file)
  */
 
 #include "Vector2.h"
+#include "debug.h"
+
 #include "mathw.h"
 
 /// ***** Constructors/Destructors *****
 
 Vector2::Vector2()
-  : x(0), y(0)
+  : m_fX(0), m_fY(0)
 {
 
 }
-Vector2::Vector2(float x, float y)
-  : x(x), y(y)
-{
-
-}
-Vector2::Vector2(const Vector2& vec)
-  : x(vec.x), y(vec.y)
-{
-
-}
-Vector2::~Vector2()
+Vector2::Vector2(float fX, float fY)
+  : m_fX(fX), m_fY(fY)
 {
 
 }
@@ -44,21 +37,22 @@ Vector2::~Vector2()
 
 void Vector2::zero()
 {
-    x = 0;
-    y = 0;
+    m_fX = 0;
+    m_fY = 0;
 }
 void Vector2::unit()
 {
-    float len = length();
+    float fLen = length();
 
-    x /= len;
-    y /= len;
+    m_fX /= fLen;
+    m_fY /= fLen;
 }
 
 float Vector2::angle() const
 {
     //TODO
-    //return atan2A(y,x);
+    DASSERT(false);
+    //return atan2A(m_fY,m_fX);
     return 0;
 }
 float Vector2::length() const
@@ -67,53 +61,45 @@ float Vector2::length() const
 }
 float Vector2::sqrLength() const
 {
-    return x*x + y*y;
+    return this->dot(*this);
 }
 
-float Vector2::dot(const Vector2& v) const
+float Vector2::dot(const Vector2& vec) const
 {
-    return x*v.x + y*v.y;
+    return m_fX * vec.m_fX + m_fY * vec.m_fY;
 }
 
 
 string Vector2::toString() const
 {
      // long just to be safe
-    char* strX = new char[50];
-    char* strY = new char[50];
-
-    sprintf(strX, "%f", x);
-    sprintf(strY, "%f", y);
-
-    string val = (string)"Vector2  x: " + strX + ", y: " + strY;
+    char rgchars[100];
 
-     // deletes the allocated memory, not just what is used by sprintf
-    delete []strX;
-    delete []strY;
+    sprintf(rgchars, "Vector2  X: %f, Y: %f", m_fX, m_fY);
 
-    return val;
+    return rgchars;
 }
 void Vector2::print() const
 {
-    printf("%s\n",toString().c_str());
+    printf("%s\n", toString().c_str());
 }
 
 
 Vector2 Vector2::add(const Vector2& vec) const
 {
-    return Vector2(x+vec.x, y+vec.y);
+    return Vector2(m_fX + vec.m_fX, m_fY + vec.m_fY);
 }
 Vector2 Vector2::subtract(const Vector2& vec) const
 {
-    return Vector2(x-vec.x, y-vec.y);
+    return Vector2(m_fX - vec.m_fX, m_fY - vec.m_fY);
 }
-Vector2 Vector2::multiply(float c) const
+Vector2 Vector2::multiply(float f) const
 {
-    return Vector2(x*c, y*c);
+    return Vector2(m_fX * f, m_fY * f);
 }
-Vector2 Vector2::divide(float c) const
+Vector2 Vector2::divide(float f) const
 {
-    return Vector2(x/c, y/c);
+    return Vector2(m_fX / f, m_fY / f);
 }
 
 /// ***** Public Methods *****
@@ -126,37 +112,37 @@ Vector2 operator-(const Vector2& vec1, const Vector2& vec2)
 {
     return vec1.subtract(vec2);
 }
-Vector2 operator*(float c, const Vector2& vec)
+Vector2 operator*(float f, const Vector2& vec)
 {
-    return vec.multiply(c);
+    return vec.multiply(f);
 }
-Vector2 operator*(const Vector2& vec, float c)
+Vector2 operator*(const Vector2& vec, float f)
 {
-    return vec.multiply(c);
+    return vec.multiply(f);
 }
-Vector2 operator/(const Vector2& vec, float c)
+Vector2 operator/(const Vector2& vec, float f)
 {
-    return vec.divide(c);
+    return vec.divide(f);
 }
 
 
 void operator+=(Vector2& vec1, const Vector2& vec2)
 {
-    vec1.x += vec2.x;
-    vec1.y += vec2.y;
+    vec1.m_fX += vec2.m_fX;
+    vec1.m_fY += vec2.m_fY;
 }
 void operator-=(Vector2& vec1, const Vector2& vec2)
 {
-    vec1.x -= vec2.x;
-    vec1.y -= vec2.y;
+    vec1.m_fX -= vec2.m_fX;
+    vec1.m_fY -= vec2.m_fY;
 }
-void operator*=(Vector2& vec, float c)
+void operator*=(Vector2& vec, float f)
 {
-    vec.x *= c;
-    vec.y *= c;
+    vec.m_fX *= f;
+    vec.m_fY *= f;
 }
-void operator/=(Vector2& vec, float c)
+void operator/=(Vector2& vec, float f)
 {
-    vec.x /= c;
-    vec.y /= c;
+    vec.m_fX /= f;
+    vec.m_fY /= f;
 }
index 13ffd88..57a5541 100644 (file)
@@ -26,13 +26,11 @@ using std::string;
 class Vector2
 {
   public:
-    float x;
-    float y;
+    float m_fX;
+    float m_fY;
 
     Vector2();
     Vector2(float, float);
-    Vector2(const Vector2&);
-    ~Vector2();
 
     void zero();
     void unit();
index 72dcd6b..81d5e05 100644 (file)
@@ -106,14 +106,14 @@ void placeEntity(PhysicsEntity* p)
 
         if( b != NULL )
         {
-            const float& xb     = b->positionRaw().x;
-            const float& yb     = b->positionRaw().y;
+            const float& xb     = b->positionRaw().m_fX;
+            const float& yb     = b->positionRaw().m_fY;
             const float& rad    = b->radius;
 
-            minP.x = xb - rad;
-            minP.y = yb - rad;
-            maxP.x = xb + rad;
-            maxP.y = yb + rad;
+            minP.m_fX = xb - rad;
+            minP.m_fY = yb - rad;
+            maxP.m_fX = xb + rad;
+            maxP.m_fY = yb + rad;
 
             goto start;
         }
@@ -135,15 +135,15 @@ void placeEntity(PhysicsEntity* p)
     return;
 
 start:
-    for( int x =  static_cast<int>( minP.x / (screenX / xDivisions) );
-             x <= static_cast<int>( maxP.x / (screenX / xDivisions) );
+    for( int x =  static_cast<int>( minP.m_fX / (screenX / xDivisions) );
+             x <= static_cast<int>( maxP.m_fX / (screenX / xDivisions) );
              x++ )
     {
         if(x < 0 || xDivisions <= x)
             break;
 
-        for( int y =  static_cast<int>( minP.y / (screenY / yDivisions) );
-                 y <= static_cast<int>( maxP.y / (screenY / yDivisions) );
+        for( int y =  static_cast<int>( minP.m_fY / (screenY / yDivisions) );
+                 y <= static_cast<int>( maxP.m_fY / (screenY / yDivisions) );
                  y++ )
         {
             if(y < 0 || yDivisions <= y)
@@ -289,10 +289,10 @@ bool getInfo(const Ball* b1, const Ball* b2, CollisionInfo* pcInfo)
     Vector2 v2 = b2->velocityRaw();
 
     // quick binding box check
-    if (p1.x - r1 > p2.x + r2
-     || p1.x + r1 < p2.x - r2
-     || p1.y - r1 > p2.y + r2
-     || p1.y + r1 < p2.y - r2)
+    if (p1.m_fX - r1 > p2.m_fX + r2
+     || p1.m_fX + r1 < p2.m_fX - r2
+     || p1.m_fY - r1 > p2.m_fY + r2
+     || p1.m_fY + r1 < p2.m_fY - r2)
         return false;
 
     // test if not touching
@@ -318,14 +318,14 @@ bool getInfo(const Polygon* pPoly, const Ball* pBall, CollisionInfo* pcInfo)
     Vector2 vecPos  = pBall->positionRaw();
     Vector2 vecVelo = pBall->velocityRaw();
 
-    float fMaxX = pPoly->maxP.x;
-    float fMinX = pPoly->minP.x;
-    float fMaxY = pPoly->maxP.y;
-    float fMinY = pPoly->minP.y;
+    float fMaxX = pPoly->maxP.m_fX;
+    float fMinX = pPoly->minP.m_fX;
+    float fMaxY = pPoly->maxP.m_fY;
+    float fMinY = pPoly->minP.m_fY;
 
     // quick binding box check
-    if (vecPos.x - fRad > fMaxX || vecPos.x + fRad < fMinX ||
-        vecPos.y - fRad > fMaxY || vecPos.y + fRad < fMinY)
+    if (vecPos.m_fX - fRad > fMaxX || vecPos.m_fX + fRad < fMinX ||
+        vecPos.m_fY - fRad > fMaxY || vecPos.m_fY + fRad < fMinY)
         return false;
 
 
@@ -339,10 +339,10 @@ bool getInfo(const Polygon* pPoly, const Ball* pBall, CollisionInfo* pcInfo)
         for (unsigned int i = 0; i < num; i++)
         {
             Vector2 vec = vectorToLine(vecPos,
-                                       pts[i].x,
-                                       pts[i].y,
-                                       pts[(i + 1) % num].x,
-                                       pts[(i + 1) % num].y);
+                                       pts[i].m_fX,
+                                       pts[i].m_fY,
+                                       pts[(i + 1) % num].m_fX,
+                                       pts[(i + 1) % num].m_fY);
 
             if (vec.sqrLength() <= fRad*fRad && 0 < vec.dot(vecVelo))
             {
index d89db8a..6939824 100644 (file)
@@ -57,7 +57,7 @@ void graphics::drawCircle(float radius, const Vector2& pos, const float* color)
 {
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
-    glTranslatef(pos.x, pos.y, -1);
+    glTranslatef(pos.m_fX, pos.m_fY, -1);
     glScalef(radius, radius, radius);
 
     if(color != NULL)
@@ -104,7 +104,7 @@ void glDrawPolygon( const std::vector<Vector2>& points )
         {
             const Vector2& vec = points.at(n);
 
-            glVertex3f(vec.x, vec.y, 0);
+            glVertex3f(vec.m_fX, vec.m_fY, 0);
         }
     glEnd();
 }
index eda4781..2b71dec 100644 (file)
@@ -39,31 +39,31 @@ Vector2 vectorToLine
     float lineSize = (float) sqrt((x1 - x2) * (x1 - x2)
                                 + (y1 - y2) * (y1 - y2));
     if (lineSize == 0)
-        return Vector2(x1 - vec.x, y1 - vec.y);
+        return Vector2(x1 - vec.m_fX, y1 - vec.m_fY);
 
-    float u = ((vec.x - x1) * (x2 - x1)
-             + (vec.y - y1) * (y2 - y1)) / (lineSize * lineSize);
+    float u = ((vec.m_fX - x1) * (x2 - x1)
+             + (vec.m_fY - y1) * (y2 - y1)) / (lineSize * lineSize);
 
     if (u < 0)
-        return Vector2(x1 - vec.x, y1 - vec.y);
+        return Vector2(x1 - vec.m_fX, y1 - vec.m_fY);
     else if (u > 1)
-        return Vector2(x2 - vec.x, y2 - vec.y);
+        return Vector2(x2 - vec.m_fX, y2 - vec.m_fY);
     else
     {
         float ix = x1 + u * (x2 - x1);
         float iy = y1 + u * (y2 - y1);
-        return Vector2(ix - vec.x, iy - vec.y);
+        return Vector2(ix - vec.m_fX, iy - vec.m_fY);
     }
 }
 
 Vector2 perp(const Vector2& vec)
 {
-  return Vector2(-vec.y, vec.x);
+  return Vector2(-vec.m_fY, vec.m_fX);
 }
 
 float dot(const Vector2& vec1, const Vector2& vec2)
 {
-  return vec1.x * vec2.x + vec1.y * vec2.y;
+  return vec1.m_fX * vec2.m_fX + vec1.m_fY * vec2.m_fY;
 }
 
 //TODO float Vector2::projectionCoeff(const Vector2* vec) const;