# Help with collision

I was wondering if anyone knows where I can find a tutorial on collision
detection in a 2D space. I realise it probably isn’t too hard, but I want to
see if their is a better way to detect collisions than what I am using.

Mike S. Codename: Freak901010_________________________________________________________________
MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx

I guess it depends on what you’re doing now, and what you need.
What ARE you doing now?

For most of my games, I find that “bounding box” collision detection is
good enough. Most people don’t notice the inaccuracies… especially since
they’re usually in favor of the player. (If the bullet destroys the rock
even though they didn’t technically have pixels “touching,” noone will
ever complain :^) )

Bounding box is pretty much:

if (thing1_x + thing1_width >= thing2_x &&
thing1_x <= thing2_x + thing2_width &&

``````  thing1_y + thing1_height >= thing2_y &&
thing1_y <= thing2_y + thing2_height)
``````

{
/* they collided! */
}

That is:

if ( the right side of thing1 is past the left side of thing2, and
the left side of thing1 is not past the right side of thing2, and

``````   the bottom of thing1 is below the top of thing2, and
the top of thing2 is not past the bottom of thing2)
``````

then,
they collided!

If I want to give any kind of handicap to one object or the other, I just
’widen’ their bounding boxes by adding a few pixels here or there.

Note: This only works if the ‘things’ on the screen aren’t moving towards each
other at too high a ‘speed’ (eg, pixels-per-frame-in-which-you-do–this-test)

In that case, you’ll want to do sub-movement and tests, or some kind of
liney algorithm, or some other thing that rarely ever applies in the
retro-style “Atari 2600 clone” games I always write.

Good luck!

-bill!
bill at newbreedsoftware.com
http://www.newbreedsoftware.com/bill/On Tue, May 07, 2002 at 05:01:45PM -0600, mike shoup wrote:

I was wondering if anyone knows where I can find a tutorial on collision
detection in a 2D space. I realise it probably isn’t too hard, but I want to

see if their is a better way to detect collisions than what I am using.

On Tue, 07 May 2002 17:01:45 -0600, mike shoup scribbled:

I was wondering if anyone knows where I can find a tutorial on
collision detection in a 2D space. I realise it probably isn’t too
hard, but I want to see if their is a better way to detect
collisions than what I am using.

You don’t give enough information to help much.

There are many different techniques for 2D collision detection,
depending on the number of objects in your world, how many of them
are moving, whether some set of them are neatly rectangular and/or
axis-aligned, how fast they move, whether you have to worry about
"tunnelling", if they’re just collections of pixels or if they have
some polygonal shape, whether they’re convex or concave, whether
you’re operating in pixel (integer) space or a floating point
coordinate system, whether or not you need earliest time of
collision, etc…

This discussion is also not SDL-specific, so is also probably better
held on a different list, such as the one of the gamedev lists hosted
by sourceforge.

``````Kent--
``````

Kent Quirk, CTO, CogniToy
@Kent_Quirk
http://www.cognitoy.com

Do a search on the SDL mailing list, there was a long discussion of this
subject just a few months ago. The following URL should do the trick for
you:

You can do a search like this your self by doing something like
"site:libsdl.org collision detection" in the search box on google.com.
Don’t include the "s in the search, I put them there because of the
syntax of English text.

``````	Bob Pendleton
``````

mike shoup wrote:> I was wondering if anyone knows where I can find a tutorial on collision

detection in a 2D space. I realise it probably isn’t too hard, but I
want to see if their is a better way to detect collisions than what I am
using.

Mike S. Codename: Freak901010

MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-----------------------------------------+

• Bob Pendleton, an experienced C/C++/Java +
• UNIX/Linux programmer, researcher, and +
• system architect, is seeking full time, +
• consulting, or contract employment. +
• Resume: http://www.jump.net/~bobp +
• Email: @Bob_Pendleton +
±-----------------------------------------+

In “Escape of the Unicorn” we are using “perfect pixel collision detection”,
every frame (we are using animated sprites) has its mask, we are checking if
two rectangles collides, then we are checking two masks. And it works, look
inside source (http://eounicorn.sf.net).On Tue, May 07, 2002 at 05:01:45PM -0600, mike shoup wrote:

I was wondering if anyone knows where I can find a tutorial on collision
detection in a 2D space. I realise it probably isn’t too hard, but I want
to see if their is a better way to detect collisions than what I am using.

char collision(float x0, float y0, int w0, int h0, float x2, float y2, int
w1, int h1)
{
float x1 = x0 + w0;
float y1 = y0 + h0;

``````float x3 = x2 + w1;
float y3 = y2 + h1;

return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
``````

}

returns 0 or 1

where
x0, y0, w0 and h0 are the x coordinate, y coordinate, width and
height of the first rectangular area
x2, y2, w1 and h1 are the x coordinate, y coordinate, width and
height of the second rectangular area

Got this from a tutorial somewhere… I didn’t write it myself. Hope it
helps.

yeah, this is probably the way you want to do it, as it’s a nice mix of
performance (only checking rectangles) and percision (colliding
rectangles are then checked w/ masks). i’d say this is probably the way
you want to do it … unless of course your game contains all sprites
that are rectangular in shape, in which case you can drop the masks.

• chrisOn Wed, 2002-05-08 at 00:45, Jacek Pop?awski wrote:

On Tue, May 07, 2002 at 05:01:45PM -0600, mike shoup wrote:

I was wondering if anyone knows where I can find a tutorial on collision
detection in a 2D space. I realise it probably isn’t too hard, but I want
to see if their is a better way to detect collisions than what I am using.

In “Escape of the Unicorn” we are using “perfect pixel collision detection”,
every frame (we are using animated sprites) has its mask, we are checking if
two rectangles collides, then we are checking two masks. And it works, look
inside source (http://eounicorn.sf.net).

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Chris Thielen
@Christopher_Thielen