• Advertisement
Sign in to follow this  

Rotated rectangle vs circle collision

This topic is 1748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Edited by P0jahn

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites

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 this post


Link to post
Share on other sites

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?

Edited by miga

Share this post


Link to post
Share on other sites

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 this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement