Jump to content

  • Log In with Google      Sign In   
  • Create Account

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

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Rotated rectangle vs circle collision


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

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
         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:

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, 05 April 2013 - 05:22 PM.


Sponsor:

#2 miga   Members   -  Reputation: 109

Like
0Likes
Like

Posted 06 April 2013 - 10:23 AM

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.



#3 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

Posted 07 April 2013 - 03:41 AM

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, 07 April 2013 - 03:41 AM.


#4 miga   Members   -  Reputation: 109

Like
0Likes
Like

Posted 07 April 2013 - 09:23 PM

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, 07 April 2013 - 09:38 PM.


#5 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

Posted 09 April 2013 - 08:50 AM

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.

Attached File  Game Test.rar   2.76MB   21 downloads


Edited by P0jahn, 09 April 2013 - 08:55 AM.


#6 miga   Members   -  Reputation: 109

Like
0Likes
Like

Posted 09 April 2013 - 07:02 PM

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



#7 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

Posted 10 April 2013 - 02:24 AM

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

And I assume you are using windows with java7 installed.



#8 miga   Members   -  Reputation: 109

Like
0Likes
Like

Posted 11 April 2013 - 11:14 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS