Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Rotated rectangle vs circle collision

Started by P0jahn Apr 5, 2013 at 11:17 PM 6 replies 4k views
Original Post
P0jahn
P0jahn

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
         rot = Math.toRadians(rect.rotation),

        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?

miga
miga

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.

P0jahn
P0jahn

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.

miga
miga

Ah I missed those commanssmile.png

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?

P0jahn
P0jahn

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]

miga
miga

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

P0jahn
P0jahn

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

And I assume you are using windows with java7 installed.

miga
miga

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.