Fernando Aires Castello
Fernando Aires Castello

Reputation: 1243

How to detect a collision between any two points in two vectors of points (C++)?

I have a class like this:

class Point
{
public:
   int x;
   int y;

   bool operator==( Point& other )
   {
       return (x == other.x) && (y = other.y);
   }
};

Then I have a rudimentary Sprite class which has a vector of points, and many other members/methods which I'm not showing below:

class Sprite
{
   std::vector<Point> points;
};

Now how do you find any Point in the vector of a Sprite object that collides with any other point in the vector of another Sprite object, using the operator== in the Point class? I've tried something like the following but it doesn't work quite right. Size() returns the total number of points in the vector of points and Points() returns the vector of points:

bool Sprite::CollidesWith( Sprite* other )
{
  for ( int ixThis = 0; ixThis < Size(); ixThis++ ) {
    for ( int ixOther = 0; ixOther < other->Size(); ixOther++ ) {
      if ( points->at(ixThis) == other->Points()->at(ixOther) )
        return true;
    }
  }

  return false;
}

Any ideas?

Upvotes: 3

Views: 990

Answers (1)

Will A
Will A

Reputation: 24988

Your operator== is incorrect.

   bool operator==( Point& other )
   {
       return (x == other.x) && (y == other.y);
   }

Upvotes: 2

Related Questions