2

First a bit of background. I have yet to read a book on game development, though I do plan on picking one up sometime.

A long time ago I made a simple pong game, followed by a simple Arkanoid-type game. In both games I figured the collision detection by comparing the x, y, and z of the ball to the paddle. I did this calculation for each side of the ball and each side of the paddle. It was the only way I could think of to do it at the time.

It was something along the lines of:

if (thing.x >= otherthing.x) {
    if (thing.y >= otherthing.y) {
    }
}

And so on.

Is this the normal way of figuring collision detection? Did I over-complicate it, or is this the basic way that it's done?

  • "The basic way", heh. :) Considering just 2 dimensions, you might "basically" want to check for collisions between any combination of circles, lines, points, rotated rectangles, convex polygons, self-intersecting polygons, etc. Additionally, some games may need to know the exact points at which the collision happened, how strongly the objects should bounce off of each other based on other physical properties, how much they should start spinning due to friction... There's not really one certain "basic way", as every game differs. (Could you be more specific what you mean by "basic"?) – Anko Jul 16 '14 at 10:29

1 Answers1

2

No, your way of doing it is exactly how AABB collisions work, being the most basic rectangle to rectangle collision: http://www.opentk.com/node/869. You basically check if any of the sides of a given rectangle are too far from those of a second one.

enter image description here

As you can see here, as soon as a side is too far, the rectangles don't collide.

You would need to take into account the size of the rectangles, which you seem to have forgotten in your little example. Supposing 2 Rectangle structs a and b with fields x, y, w and h:

if (a.x + a.w < b.x ||
    a.y + a.h < b.y ||
    a.x > b.x + b.w ||
    a.y > b.y + b.h) {
    return false; // No collision
}
return true; // Collision
Alexandre Desbiens
  • 1,554
  • 1
  • 12
  • 18
  • Thanks for the information! I did make my example overly simplistic, I was just trying to get the basic idea across. Your answer is very thorough and I appreciate it. – Sword of GF Jul 16 '14 at 18:15