# Rotated rectangle vs circle collision

## Recommended Posts

P0jahn    307

I am trying to translate the code found here:

http://www.migapro.com/circle-and-rotated-rectangle-collision-detection/

The collision detection do not work as intended.

Here is my code:

public static boolean rotatedRectangleVsCircle(GameObject rect, GameObject circle)
{
double rectCx = rect.currX + rect.width / 2, //Rectangle center x
rectCy = rect.currY + rect.height / 2, //Rectangle center y
ccX = circle.currX + circle.width / 2, //Circle center x
ccY = circle.currY + circle.height/ 2, //Circle center y

cx = Math.cos(rot) * (ccX - rectCx) - Math.sin(rot) * (ccY - rectCy) + rectCx,
cy = Math.sin(rot) * (ccX - rectCx) + Math.cos(rot) * (ccY - rectCy) + rectCy,

x,y;

if (cx < rect.currX)
x = rect.currX;
else if (cx > rect.currX + rect.width)
x = rect.currX + rect.width;
else
x = cx;

if (cy < rect.currY)
y = rect.currY;
else if (cy > rect.currY + rect.height)
y = rect.currY + rect.height;
else
y = cy;

if (findDistance(cx, cy, x, y) < circle.width / 2)
return true;
else
return false;
}


Point currX, currY represent the top-left corner of the sprite.

The sprite image looks like this at the start:

[attachment=14670:rectangle.png]

Finally, the rotation uses the start value 0 and increases with 1 every frame, rotating around its center point.

Why is it not working?

Edited by P0jahn

##### Share on other sites
miga    109

Hey there.

I think it's just a typo, but you are missing the type declaration at

x, y;


Have you tried taking logs outputting the values? See if the outputs correspond to the graphic representation or if they look correct in the scenes behind.Try to implement piece by piece making sure they work at lower level.

How are you graphically rotating them?

What actually works and what doesn't?

This is all I can say right now because I'm gonna be out all day today.

##### Share on other sites
P0jahn    307

Hello

I think it's just a typo, but you are missing the type declaration at

Actually, it is not a typo, it compiles just fine.

How are you graphically rotating them?

I am using Slick2D and I use image.rotate(float degree). Not exactly sure how the actual rotation is done.

The collision detection is simply inconsistent. It does not detect properly.

I increase the rectangles rotation variable with 1 every frame, rotating it clockwise. This differ from yours, which rotates counter-clockwise.

I tried decreasing my rotation variable with 1 every frame instead, making it rotate ccw, no luck.

Also wanted to note that rect.currX and rect.currY points to the unrotated top-left corner of the rectangle.

Edited by P0jahn

##### Share on other sites
miga    109

Ah I missed those commans

I've never used Slick2D which should not be a problem here at all. You just match the way it rotates.

Does it work with 0 degrees? Did you try outputting the values? If so, how are they acting? If I'm debugging it, I would start from there to find out what is not acting properly.

I can't tell much from the given piece of code. If you can show me a code that I can actually run and test, I could help more.

You do understand the idea, right?

Edited by miga

##### Share on other sites
P0jahn    307

It seems to work fine when the rotation is 0.

Check the attached file, everything is logged in a text area. You can pause the game with escape.

[attachment=14740:Game Test.rar]

Edited by P0jahn

##### Share on other sites
miga    109

I can't open your game. It crashes as soon as it tries to create its window.

##### Share on other sites
P0jahn    307

Is the "res" folder in the same directory as game.exe?

And I assume you are using windows with java7 installed.

##### Share on other sites
miga    109

Right, you are correct. Windows 7 and java updated to 7.