3

I'm working not a game, and since I couldn't find good collision detection method for detecting collision with rotated rectangles, I decided to try something like this: I have a rotated rectangle (which I suppose I can get its corners' coordinates (maybe some function of CGRect or trigonometry if necessary) I want to get all the coordinates along the line connecting two of the corners (even just the int values, or with a certain increment so it won't get too long) and get them into an array. then I can check for the containing rectangle of the ball which its bounding rectangle doesn't rotate) if it contains the any point of the array. if it does, they collided.

Is there an easy way of getting these coordinates into an array? assuming I know the start and end? I understand it will be not quite efficient, but right now I'm using the OpenGL color detection, and it is too kinda slow, and not working well, so I'm kinda in a need of a new method.

Thanks!

La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • It would probably be easier to find the intersection of a line segment and a circle. [Here's a link](http://doswa.com/2009/07/13/circle-segment-intersectioncollision.html). – user1118321 Feb 14 '12 at 05:19
  • thanks for ur comment, this is pretty much what i'm after, just don't know how to get this line and check for intersection in Objective-C – La bla bla Feb 14 '12 at 05:30

1 Answers1

1

You already have the lines via the edges of your rectangle. Just take the X,Y coords of your 4 corner and you have your 4 lines.

Generally with collision you can almost always find a way to fake it, I would only bother using a rotated rect for the collision if you really need it, a lot of the time you can take what looks like a rectangle and just give it circular collision.

If you are sticking with the rotated rect another approach you can try is to apply the inverse rotation to both objects, then check the collision. So if the rect is rotated 45 deg, rotate the rect back -45 degrees so it is axis aligned, then also rotate the center of the circle -45 degrees. Now you have a much simpler collision to work with.

You can get more here: Circle-Rectangle collision detection (intersection)

Community
  • 1
  • 1
TurqMage
  • 3,321
  • 2
  • 31
  • 52
  • Thanks for the answer. regarding ur first sentence. I have the 4 corners, but how does it give me the line? this is my initial problem. – La bla bla Feb 14 '12 at 18:46
  • You have 4 lines, you have to check each. - Top Left to Top Right, Top Right to Bottom Right, etc – TurqMage Feb 14 '12 at 18:51
  • no, i mean, how can i get the lines that are between these points? – La bla bla Feb 14 '12 at 18:57
  • The link from before goes over it, http://doswa.com/2009/07/13/circle-segment-intersectioncollision.html For the Top Left to Top Right line you want to make a vector by subtracting the Top Left from Top Right. That value will be the vector for seg_v – TurqMage Feb 14 '12 at 21:18
  • A vector in code is just represented by a point, an x and a y. I don't believe there is a build in vector class in Obj-C. If you need help with the vector math there is a simple class here: http://mac.softpedia.com/get/Developer-Tools/Vector2D.shtml – TurqMage Feb 15 '12 at 17:32