Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In a game im developing in 2D, im have one or more circles (balls) that can collide with several rectangles (bricks).

Im trying to figure out a collision detection strategy, and have come up with the following plan.

  1. For each loop of the game ticker get the x,y point of the start and destination of the ball.

  2. Form a rectangle (blue color in fig1.) at the start and dest point that has the balls rotation as well as setting the rectangle width as the balls radius times two.

  3. check if the rectangle intersects with anyting in the play area using Separation of Axis

  4. if the balls path, defined trough the rectangle intersects with anything in the play area, move the ball to the collision point, and after setting a new start and dest point, rerun from point 2 for as long as there are collisions or the ball has not travelled 100% of its planned distance.

fig1

With this strategy i can have collision detection with high ball speeds as well as high precision.

Im a quite new to game development so theres a high probability that im overdoing this, and that there probably exists alternatives to this. Any help/insight on this is greatly appreciated.

share|improve this question
    
You might want to search for "collision detection circle and line" instead of rectangle. –  the_lotus Jul 13 '12 at 19:30

2 Answers 2

I have some ideas, although I'm not sure if any of these is better than your solution. Just something to think about.

1 . To filter out the cases where collision doesn't happen:

Notice, that the area your brick object covers while moving between two Updates, can be represented as a Polygon (let's call it P1). The area that you ball object covers, can be represented with a polygon (rotated rectangle) and two circles (let's call these P2, C1 and C2).

In every update, you create these polygons and circles (hopefully the acceleration of your objects isn't supposed to change between updates). Then check if P1 and (P2 || C1 || C2) intersect. If they don't, then you have no collision.

2 . To get the precision you want:

Interpolate to get the object positions between two Updates. If the ball moves 50 pixels in an Update, but the brick is only 20 pixels thick (or also moves too fast), then you must compare at least 3 states between T1 and T2 (T1 is the previous update, T2 is the current update). You can calculate the positions of the objects in these virtual updates (T1i, T1ii, T1iii etc.) the same way you do for T1 and T2. Then do collision detections with the corresponding object positions (Brick.GetPosition(T1i) vs Ball.GetPosition(T1i) etc.). The good thing about this method is that you can achieve exactly the precision you want, without doing too many or too difficult calculations.

share|improve this answer

I suggest you to really build the things from simple to complex, which means: start from a simple collision detection approach/routines, and improve it if you see that is not working properly, or not accurately enough. In your case, what you can just do is to simply check, at each timestep, if any ball is intersecting any of your rectangles, and if it is, just perform separation of the circle from the rectangle (move it away so that they don't collide anymore) and adjust the velocity/acceleration/properties (for example, you can mirror the velocity in the collision direction to have a bounce-like effect). From your side, you have the fact that circle and rectangles have very cheap intersection tests (if you have a circle, it intersects a rectangle only if it the center of the circle lies inside the rectangle, or if the center is closer than radius to any of the 4 points of the rectangle. In my opinion, if you assign circles and rectangle to different collision groups (so that collisions among rectangles for example are not checked) and don't use sqrt in comparing distances, you will probably make it. If you don't, consider using Bounding Volume Hierarchies to help the collision detection process, or modify the architecture of the game in order to run the collision detection many times between single updates. What you have in mind is a sort of Continuous Collision Detection that is often overkill.

share|improve this answer
    
I used to have a simple intersection test. However, when the ball gained speed, it tended to go straight through the rectangles without triggering a collision. So this is my first attempt to get a more reliable collision detection system, which can handle high speeds. –  netbrain Jul 13 '12 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.