• Create Account

We're offering banner ads on our site from just \$5!

### #ActualP0jahn

Posted 05 April 2013 - 05:22 PM

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?

### #1P0jahn

Posted 05 April 2013 - 05:17 PM

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 increases with 1 every frame, rotating around its center point.

Why is it not working?

PARTNERS