Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Collision detection.

Name: Anonymous 2007-12-16 9:12

Hi, I'm relatively new to programming games. In order to teach myself a bit about it, I'm attempting to create a very simple 2-dimensional game.

When planning my game I realized that I would need some sort of collision detection. Though I haven't started coding yet, I've attempted to illustrate my problem on this image: http://img248.imageshack.us/img248/408/picug2.png

Assume that box 0 is being pushed in the direction of box 1. Box 0 would collide with box 1; box 1 would accelerate and collide with box 2 etc.

I had planned my game as running an infinite loop that would update the position of these boxes before drawing them. Therefore, my approach to this would be to check whether any of these boxes collides with any of the other boxes.

I've made a function to test whether two boxes collide or not, but it seems I'll be running this function several times each update. Let's assume I had 100 boxes in my game, I'd have to check whether the first box collided with the remaining 99 boxes. Then I'd check if the next box collided with the remaining 98 and so forth.

It seems to me like I'd end with a complexity like: O((n*(n+1))/2) for something I'd have to run each update. Though my game is rather small, this seems like a heavy way of doing it.

In my example, the boxes would never collide with any box that isn't next to them, but I’m attempting to make a more general solution, that would also work if these boxes have switched place for some reason.

If you've read through my wall of text I thank you. My question is how do I avoid this complexity and still check the collision of all boxes? If there is no simple answer, perhaps someone could point me in the direction of some article dealing with this problem.

I will reply to my own post, telling myself to read SICP in order to get that reply out of the way.

Name: Anonymous 2007-12-16 9:12

READ SICP

Name: Anonymous 2007-12-16 9:35

>>2
DONT HELP HIM!!!

Name: Anonymous 2007-12-16 10:26

Name: Anonymous 2007-12-16 10:34

OP here
Well wikipedia was kinda obvious, but the other link actually seems like it migth be just what I was looking for. Thank you very much.

Name: Anonymous 2007-12-16 10:52

>>5
fuck off

Name: Anonymous 2007-12-16 10:58

>>6
DONT HELP HIM!!!

Name: Anonymous 2007-12-16 11:20

O((n*(n+1))/2)
That's O(n^2)

You'd need some way to locate objects that are near another which can be done by dividing the field into smaller areas and look at the objects only in the current and neighboring areas or using some sort of tree to keep track of objects. Then you use simple a collision detection like bounding box or bounding sphere to find out whether the ones you picked out are close enough to do a real collision detection on.
Basically you keep weeding out the unlikely ones using simpler algorithms and then perform more costly operations on a very small subset of the objects.

Name: Anonymous 2007-12-16 11:24

>O((n*(n+1))/2)
lern2asymptoticnotation

Name: Anonymous 2007-12-16 14:17

>>8 is correct. Assuming that the boxes can only move along the x axis, id simply store a pointer to the adjacent boxes, if any, and then only check collisions with them (and, since i know which direction the box is moving, I only need to check one of the two possibilities, per box, each frame).
If the boxes can move any direction at all, a more general solution is required. For 2D, quad tree's work quite well: split the playing field into four, split each sub division by four, repeat until small enough. Then for each division (the smallest ones), store a list of boxes. For each box, you now only need to check collisions with other boxes in that division, and, if at the edge of a division, the boxes in the adjacent division.

I'm not very good at explaining, hope you understood.

Name: Anonymous 2007-12-18 13:31

OP here, just wanted to thank you all for the replies before the thread dies. I've been considering your solutions and with the help of those, along with some reading, I believe I'm on the way to creating something that I'll be satisfied with.

You've proven that /prog/ actually has a creative purpose and is able to give other useful answers than `Read SICP'. Something that I, untill now, was convinced would not be possible.

Name: Anonymous 2007-12-18 13:56

>>1
>>8
>>10
>>11
same troll

Name: Anonymous 2007-12-18 14:00

Name: Anonymous 2007-12-18 14:24

>>8
That's O(n^2)
That's O(n2)

Also, http://en.wikipedia.org/wiki/Quadtree

Name: Anonymous 2007-12-18 17:00

>>1
Note that "Collision detection" is basically just figuring out who hit what and where.  Responding to changes reflecting energy transfer between solid objects tends to be called "physics engine", albeit a simple one in this example.

Name: Anonymous 2009-03-18 2:40

I wants lots and lots of some delectable pot!

Marijuana MUST be legalized.

Name: Anonymous 2011-02-04 14:01


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List