From 9d6dea5f1cfd43cfa136241b352830371acfdc88 Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Wed, 21 Jan 2009 18:14:59 -0500 Subject: [PATCH] refactored Vector2 --- src/Effects/Screen.cpp | 32 ++++++++-------- src/Entities/Polygon.cpp | 8 ++-- src/Vector2.cpp | 96 ++++++++++++++++++++--------------------------- src/Vector2.h | 6 +-- src/collisionManager.cpp | 48 ++++++++++++------------ src/graphics/graphics.cpp | 4 +- src/mathw.cpp | 16 ++++---- 7 files changed, 97 insertions(+), 113 deletions(-) diff --git a/src/Effects/Screen.cpp b/src/Effects/Screen.cpp index 9c0f283..770a456 100644 --- a/src/Effects/Screen.cpp +++ b/src/Effects/Screen.cpp @@ -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; } diff --git a/src/Entities/Polygon.cpp b/src/Entities/Polygon.cpp index ecaf2c4..06955c3 100644 --- a/src/Entities/Polygon.cpp +++ b/src/Entities/Polygon.cpp @@ -55,11 +55,11 @@ void Polygon::createBindingBox() for(unsigned int i=1; i 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; } } diff --git a/src/Vector2.cpp b/src/Vector2.cpp index d13ea95..2a8c8af 100644 --- a/src/Vector2.cpp +++ b/src/Vector2.cpp @@ -16,26 +16,19 @@ */ #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; } diff --git a/src/Vector2.h b/src/Vector2.h index 13ffd88..57a5541 100644 --- a/src/Vector2.h +++ b/src/Vector2.h @@ -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(); diff --git a/src/collisionManager.cpp b/src/collisionManager.cpp index 72dcd6b..81d5e05 100644 --- a/src/collisionManager.cpp +++ b/src/collisionManager.cpp @@ -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( minP.x / (screenX / xDivisions) ); - x <= static_cast( maxP.x / (screenX / xDivisions) ); + for( int x = static_cast( minP.m_fX / (screenX / xDivisions) ); + x <= static_cast( maxP.m_fX / (screenX / xDivisions) ); x++ ) { if(x < 0 || xDivisions <= x) break; - for( int y = static_cast( minP.y / (screenY / yDivisions) ); - y <= static_cast( maxP.y / (screenY / yDivisions) ); + for( int y = static_cast( minP.m_fY / (screenY / yDivisions) ); + y <= static_cast( 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)) { diff --git a/src/graphics/graphics.cpp b/src/graphics/graphics.cpp index d89db8a..6939824 100644 --- a/src/graphics/graphics.cpp +++ b/src/graphics/graphics.cpp @@ -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& points ) { const Vector2& vec = points.at(n); - glVertex3f(vec.x, vec.y, 0); + glVertex3f(vec.m_fX, vec.m_fY, 0); } glEnd(); } diff --git a/src/mathw.cpp b/src/mathw.cpp index eda4781..2b71dec 100644 --- a/src/mathw.cpp +++ b/src/mathw.cpp @@ -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; -- 2.10.2