1

Lately I've been working on implementing a good collision detection system for my game. Before implementing it into my game I decided to make a sort of simulation. I've followed many tutorials (mainly this one) but I can get it working. I followed it step by step, but I don't know if my error is in the overlap (checking if the two projections overlap) or in the whole code itself.

The code has got the Vector2D and ConvexPolygon implementation as well. Here's the code:

http://pastebin.com/whV31SDi

It's pasted in pastebin because it's quite long.

1
  • It is always a dilemma how much to expand acronyms in question titles—that have to be short too—but a Google match shows that for most computer scientists SAT risks evoking the boolean satisfiability problem. en.wikipedia.org/wiki/Boolean_satisfiability_problem Commented Sep 1, 2012 at 11:40

3 Answers 3

1

Well as you say the code is rather long, and I didn't go through it all. But two errors stuck out

1) Your operator- is backwards

    Vector2D& operator - (const Vector2D& other)
    {
            Vector2D resultant;
            resultant = Vector2D(other.x - this->x,other.y - this-> y);
            return resultant;
    }

should be

    Vector2D& operator - (const Vector2D& other)
    {
            Vector2D resultant;
            resultant = Vector2D(this->x - other.x, this->y - other.y);
            return resultant;
    }

2) There are lots of style issues with your code. The biggest one is returning all your values by references to local variables. That is just asking for trouble because the variable no longer exists after the function returns. Here's a better way to write your operator+ for instance

   Vector2D operator+ (const Vector2D& other) const
   {
            return Vector2D(other.x + this->x, other.y + this->y);
   }

Still not perfect, because symmetrical operators like operator+ should be global functions not class members, but better than what you have.

Sign up to request clarification or add additional context in comments.

1 Comment

@EdoardoTygerDominici Well I don't know much about collision detection but isn't it a problem that your second polygon isn't convex? The tutorial you mentioned says this algorithm works for convex shapes only. In fact some people would say the second polygon isn't a polygon at all, because it's edges cross.
0

I think it has to do with the fact you are using a bool-function to check the if there are overlaps.

In your for-loop you exit the function every time the shapes have not overlapped, and every frame the for loop starts over, and exits at the same place. change it to a void function.

I could be mistaken because I didn't really read all that code thorough, but I have had that problem myself.

Comments

0

This answer may be a bit late, however after looking at your code, it seems that you are only checking against the axes of only one of the convex polygons. SAT requires that you check against both shapes axes, which is most likely your issue. To fix this just repeat your collision check for the other shapes axes that weren't checked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.