From: unknown Date: Fri, 28 Jan 2011 22:56:06 +0000 (-0600) Subject: take into account sprite position for collision testing X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=ff09711bd755bb4d066b4a4b8ae8232c305f98b9;p=IvanGame.git take into account sprite position for collision testing --- diff --git a/Collision.cpp b/Collision.cpp index 9014c5c..f57ccf0 100644 --- a/Collision.cpp +++ b/Collision.cpp @@ -183,29 +183,30 @@ void CollisionCircle::draw(const Point2D& pos){ Sulock(Game::game()->Screen()); } -bool Collision::checkCollisions(const vector& c){ +bool Collision::checkCollisions(const vector& c, const Point2D cPos, const Point2D pos){ for(int i=0; i < c.size(); i++){ - if(collision(c[i]))//if any are true return true then and there + if(collision(c[i], cPos, pos))//if any are true return true then and there return true; } return false;//if none were found then return false. } -bool CollisionRectangle::collision(const Collision *c){ +bool CollisionRectangle::collision(const Collision *c, const Point2D cPos, const Point2D pos){ if(dynamic_cast(c)){ - const CollisionRectangle* rec = dynamic_cast(c); + //const CollisionRectangle* rec = dynamic_cast(c); } - else if(dynamic_cast(c)){ + else if(const CollisionCircle* col = dynamic_cast(c)){ + ///col->collision(this, cPos, pos); \todo this flips a shit!!!!!! } return false; } -bool CollisionCircle::collision(const Collision *c){ +bool CollisionCircle::collision(const Collision *c, const Point2D cPos, const Point2D pos){ if (dynamic_cast(c)) { } else if(const CollisionCircle* col = dynamic_cast(c)){ - if((col->mPos - mPos).length() < col->radius + radius) + if(((col->mPos + cPos) - (mPos + pos)).length() < (col->radius + radius)) return true; else return false; diff --git a/Collision.h b/Collision.h index 9953d5c..3cfba1b 100644 --- a/Collision.h +++ b/Collision.h @@ -18,9 +18,23 @@ class Collision { public: Collision():mPos(0.0,0.0),color(0xFF00FF00){ } - Point2D mPos;/**< The position of the center of the collision data \todo need the parent object's position*/ - bool checkCollisions(const vector& c); /**< Check collision with objects */ - virtual bool collision(const Collision *c) = 0; /**< Check collision with objects */ + Point2D mPos;/**< The position of the center of the collision data */ + /** + Check collision with objects + \param c the collision data associated with the sprite to check collision with. + \param cPos the position of the sprite we want to check collision with. + \param pos the current sprite's position. + \return Returns true if any of the object's collision datas are colliding . + */ + bool checkCollisions(const vector& c, const Point2D cPos, const Point2D pos); + /** + Check collision with objects + \param c a single collision data for an object + \param cPos the position of the sprite we want to check collision with. + \param pos the current sprite's position. + \return Returns true if any of the object's collision datas are colliding . + */ + virtual bool collision(const Collision *c, const Point2D cPos, const Point2D pos) = 0; /// \todo See this http://www.metanetsoftware.com/technique/tutorialA.html virtual void draw(const Point2D& pos) = 0;/**< Draws the collision data to the screen */ protected: @@ -44,7 +58,14 @@ public: CollisionRectangle(Point2D pt, double w, double h):width(w),height(h){ mPos = pt;} double width;/**< Rectangle's width */ double height;/**< Rectangle's height */ - virtual bool collision(const Collision *c);/**< Check collision with objects */ + /** + Check collision with objects + \param c a single collision data for an object + \param cPos the position of the sprite we want to check collision with. + \param pos the current sprite's position. + \return Returns true if any of the object's collision datas are colliding . + */ + virtual bool collision(const Collision *c, const Point2D cPos, const Point2D pos); virtual void draw(const Point2D& pos);/**< Draws the collision data to the screen */ }; @@ -63,8 +84,22 @@ public: CollisionCircle(); CollisionCircle(Point2D pt, double r) : radius(r){ mPos = pt; } double radius; /**< The raidus of the circle */ - virtual bool collision(const Collision *c);/**< Check collision with objects */ - bool collision(const CollisionRectangle*);/**< This does collision between Rectangles and Circles */ + /** + Check collision with objects + \param c a single collision data for an object + \param cPos the position of the sprite we want to check collision with. + \param pos the current sprite's position. + \return Returns true if any of the object's collision datas are colliding . + */ + virtual bool collision(const Collision *c, const Point2D cPos, const Point2D pos); + /** + This does collision between Rectangles and Circles + \param c a single collision data for an object + \param cPos the position of the sprite we want to check collision with. + \param pos the current sprite's position. + \return Returns true if any of the object's collision datas are colliding . + */ + bool collision(const CollisionRectangle*, const Point2D cPos, const Point2D pos);/**< This does collision between Rectangles and Circles */ virtual void draw(const Point2D& pos);/**< Draws the collision data to the screen */ }; #endif diff --git a/Sprite.cpp b/Sprite.cpp index 806ecd3..4bf15c8 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -68,7 +68,7 @@ Sprite* Sprite::collisionWithSprite(string name){ if(s==NULL) return NULL; for(int i=0; i < frame.collisionData.size(); i++) - if(frame.collisionData[i]->checkCollisions(s->getCollisionData()))//if there is a collision + if(frame.collisionData[i]->checkCollisions(s->getCollisionData(), s->mPos, mPos))//if there is a collision return s; return NULL;//if there aren't collisions }