3
\$\begingroup\$

I know this is a very typical problem and that there area a lot of similar questions, but I have been looking for a while and I have not found anything that fits what I want.

I am developing a 2D game in which I need to perform collisions between a ball and simple polygons. The polygons are defined as an array of vertices.

I have implemented the collisions with the bounding boxes of the polygons (that was easy) and I need to refine that collision in the cases where the ball collides with the bounding box. The ball can move quite fast and the polygons are not too big so I need to perform continuous collisions.

I am looking for a method that allows me to detect if the ball collides with a polygon and, at the same time, calculate the new direction for the ball after bouncing in the polygon.

(I am using XNA, in case that helps)

\$\endgroup\$
6
  • 2
    \$\begingroup\$ doswa.com/2009/07/13/circle-segment-intersectioncollision.html \$\endgroup\$
    – Tetrad
    May 1, 2012 at 15:42
  • \$\begingroup\$ That is, the key thing you're probably missing is that "polygons" are just collections of line segments. \$\endgroup\$
    – Tetrad
    May 1, 2012 at 15:42
  • \$\begingroup\$ what do you mean by polygon that can be a rather broad term considering that a polygon can have anywhere from 3 to infinity sides? what is the concavity of the polygon. are all faces convex, or are some concave? do you want pixel perfect collisions? is the structure of the polygon know to the graphics system (are you using the directX portion, or the XNA portion) \$\endgroup\$
    – gardian06
    May 1, 2012 at 18:32
  • \$\begingroup\$ I am concerned about the ball moving too fast. The algorithm you are linking is very good but it covers the case when the ball is moving. \$\endgroup\$ May 2, 2012 at 9:08
  • \$\begingroup\$ I used a sphere tree for this. Very straightforward and works well without having to test the real geometry. \$\endgroup\$
    – 3Dave
    Sep 5, 2012 at 19:27

2 Answers 2

2
\$\begingroup\$

The sounds similar to something I was working with and there is no simple answer that could be confined to one post that covers everything. In my opinion you should look into something called separate axis theorem (SAT).

When I was trying to implement it the best walk through I could across was this: http://dl.dropbox.com/u/22121701/Polygon%20collision.rar

It starts with first principles and eventually works it's way up to a full blown rigid body simulator. I only followed it as far as having it accommodate fast movements. It's written for C++, so it would help if you knew C++ however I didn't know it and still don't know it when I implemented it in XNA.

If you can't make heads or tails of it then you may want to look for something a bit more specialised for your case (this is a VERY general approach and will work for most things). If you're REALLY struggling I would be happy to give you MY implementation of it, but there are few comments and it was done when I was learning C#/XNA so it may not be efficient.

\$\endgroup\$
1
\$\begingroup\$

Why not simply use the edges and use line to line collision? One line is the distance traversed by you and one is an edge of the poly. Extend each poly line by the width of your sphere in each direction to account for the size of the sphere. Also after a collision has occurred you have all the angle info you'll need to determine the angle of reflection.

Line/Line Collision

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .