# Seperating Axis 2D Projection Issue

## Recommended Posts

Hello everyone! I’m trying to learn SAT, and I almost have everything figured out. The biggest struggle for me what to understand projection. I’m noticing my collision between two rectangles works fine, but once I rotate, on certain angles it isn’t checking min and max properly as you can see a gap in the image, but it still shows collision.

I’ve pasted my code, it’s not much. I cannot seem to fiquire out why it’s not projecting properly. My dot formula and project should be correct, unless I’m something is wrong with the rects updating.

I’m using two SFML Rectangles, and when they move or rotate I have to call getTransform().transformPoint(rectB.getPoint(1)) (points are zero based, so this would be top right), and the points are correct when I debug. Also, I use width / 2, because the origin is in the center of the rect.

Any idea what I’m doing wrong here?

Based on the images showing red when they’re so far apart *red means collision = true*

I’m assuming my projection is wrong because the min and max for both rects would have a gap.

       // Get Faces
axesEdge[0] = sf::Vector2f(rectA.getTransform().transformPoint(rectA.getPoint(1)).x + rectA.getLocalBounds().width / 2 - rectA.getTransform().transformPoint(rectA.getPoint(0)).x + rectA.getLocalBounds().width / 2,
rectA.getTransform().transformPoint(rectA.getPoint(1)).y + rectA.getLocalBounds().height / 2 - rectA.getTransform().transformPoint(rectA.getPoint(0)).y + rectA.getLocalBounds().height / 2);

axesEdge[1] = sf::Vector2f(rectA.getTransform().transformPoint(rectA.getPoint(1)).x + rectA.getLocalBounds().width / 2 - rectA.getTransform().transformPoint(rectA.getPoint(2)).x + rectA.getLocalBounds().width / 2,
rectA.getTransform().transformPoint(rectA.getPoint(1)).y + rectA.getLocalBounds().height / 2 - rectA.getTransform().transformPoint(rectA.getPoint(2)).y + rectA.getLocalBounds().height / 2);
axesEdge[2] = sf::Vector2f(rectB.getTransform().transformPoint(rectB.getPoint(0)).x + rectB.getLocalBounds().width / 2 - rectB.getTransform().transformPoint(rectB.getPoint(3)).x + rectB.getLocalBounds().width / 2,
rectB.getTransform().transformPoint(rectB.getPoint(0)).y + rectB.getLocalBounds().height / 2 - rectB.getTransform().transformPoint(rectB.getPoint(3)).y + rectB.getLocalBounds().height / 2);
axesEdge[3] = sf::Vector2f(rectB.getTransform().transformPoint(rectB.getPoint(0)).x + rectB.getLocalBounds().width / 2 - rectB.getTransform().transformPoint(rectB.getPoint(1)).x + rectB.getLocalBounds().width / 2,
rectB.getTransform().transformPoint(rectB.getPoint(0)).y + rectB.getLocalBounds().height / 2 - rectB.getTransform().transformPoint(rectB.getPoint(1)).y + rectB.getLocalBounds().height / 2);

Edited by ProgrammingLua

##### Share on other sites

I took a quick look at your code, and you're doing everything right accept for one mistake.

Do not use (width / 2) or (height / 2) when setting up your axes edges, it's throwing off your calculation. You can still keep rotation on the center origin, but remove that and try your code to see if it works and let me know.

Edited by Rutin
Spelling error - expect -> accept

##### Share on other sites
6 minutes ago, Rutin said:

I took a quick look at your code, and you're doing everything right expect for one mistake.

Do not use (width / 2) or (height / 2) when setting up your axes edges, it's throwing off your calculation. You can still keep rotation on the center origin, but remove that and try your code to see if it works and let me know.

Wow!! Thank you! Everything works now. I spent a few days not able to fix it now its good! I can rotate 2 objects with detection!

##### Share on other sites

Nice work, I'm surprised someone found your mistake! A little tidbit of advice: make sure to start debug rendering some of your inner calculations next time you write this kind of code. You can render transforms, planes, normals, points, projected points, etc. to make sure you are understanding the linear algebra.

##### Share on other sites
2 minutes ago, Randy Gaul said:

Nice work, I'm surprised someone found your mistake! A little tidbit of advice: make sure to start debug rendering some of your inner calculations next time you write this kind of code. You can render transforms, planes, normals, points, projected points, etc. to make sure you are understanding the linear algebra.

Thank you! I’m new to this so it was very hard to visualize axes to project on. Do you recommend I print to console as things happen?

##### Share on other sites

Oh no do not bother printing to console. Debug render with lines and points on the screen, just like you are rendering your boxes!

##### Share on other sites

Ok I understand. Thank you!

##### Share on other sites
20 minutes ago, ProgrammingLua said:

Wow!! Thank you! Everything works now. I spent a few days not able to fix it now its good! I can rotate 2 objects with detection!

No problem. We've all been in this situation where one miscalculation throws everything off the rails. Great job on implementing rotating convex collision.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634051
• Total Posts
3015243
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!