Advertisement

collision

Started by January 07, 2017 11:51 PM
77 comments, last by jpetrie 7 years, 8 months ago

Good work.

Don't be discouraged by blunt critiques of your code or pseudocode. Absolutely everyone starts off writing bad code, and hardly anyone found this particular problem easy when they first encountered it. It's not my style, but some people don't see a point in sugarcoating it with newbies, particularly if you seem resistant to taking advice.

If you're finding that previous attempts to help you are still too complicated, then try something simpler. Don't worry about Breakout, or moving balls, or making bricks disappear. Instead, just see if you can write a function that can determine whether a circle and a rectangle are overlapping. A few tips:

  1. The circle can be represented by a radius (float) and a center point (2D vector, or separate floats for x and y).
  2. The rectangle can be represented by a position (x, y) and size (width, height). Alternatively, you could have four separate floats for the positions of the left, right, top, and bottom edges of the box. Either way, you're dealing with what's called an axis-aligned box, because the box is not tilted or rotated.
  3. If you can determine the distance between the circle's center point and the box (see: Arvo's algorithm), then you have the problem solved. You just need to see if this distance is less than the radius of the sphere.

This won't completely solve your collision problems, but it'll be a good first step. I would advise doing this exercise, even if you already have your game collision working. It sounds like you might be doing some unnecessary stuff (like having separate steps for separate brick layers) and there's good reason to believe that you'll start running into trouble once the ball starts bouncing from other directions (e.g., if the player gets the ball in the top area). Much better to start simple with an algorithm that you understand step-by-step.

thanks for the encouragement, I am working on getting the upper computer paddle to shoot the bricks as a computer opponent. how do I post a screenshot?

Advertisement

well I took CDProp's advice and made up some stubbed out code that does collision detection between a rectangle and a circle.

Does it work well? Can you see how you might be able to utilize that function in your game?

Try this:

  1. Loop over all of your bullets. For each bullet:
    1. Loop over all bricks. For each brick:
      1. Use that function you wrote to determine if the bullet is intersecting the brick.
      2. If so, make the brick disappear.

If you have N bullets on the screen and M bricks, then you'll end up calling your function N×M times. On modern computers, this is probably not a problem unless you literally have hundreds of bullets and bricks. However, if needed you can speed up this algorithm by (for example) only testing your bullet against bricks that are in the same column as the bullet (assuming the bullet moves straight up).

One common pitfall with this type of collision detection is that the bullet might be moving so fast that it passes right through the brick without intersecting it. For example, on frame 1 the bullet might be fully below the brick, and on frame 2 the bullet might be fully above the brick. The easiest way to fix that is probably to divide your frame up into 5 or so increments, and move the bullet one increment at a time while testing for an intersection at each point. This requires 5 intersection tests per bullet per brick per frame, but it's often worth it. You can also use a different number of increments, e.g. 2 or 3 or 7, depending on your needs. Another option is to use a sweeping intersection test, which will tell you exactly when the bullet hit the brick and doesn't require multiple increments. However, that is an advanced topic. That article doesn't have a sweeping test specifically for circles vs. boxes, and so you'll have to come up with one yourself or find one somewhere else, or maybe use sphere vs. plane against each side of the box.

well I got the collision detection working for the lower gun and am now working on collision detection for the computer upper gun,

here is a screen shot of my game

http://imgur.com/sT4VGzp

Advertisement

well I finally got all of my collision detection routines worked out. next I have to get the upper gun to shoot randomly and then implement game scoring and then maybe some sound effects.

well I have decided to use the "s" key to fire the upper gun and the space bar to fire the lower gun, now all I have to do is implement the scoring routine.

It sounds like the original issue you had has been solved; that's great. If you'd like to post regular updates on the status of your project I suggest you use GDNet's blog feature.

This topic is closed to new replies.

Advertisement