View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Same object collision

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Patriarch K  Members

Posted 18 January 2013 - 05:21 PM

Imagine that I have an object Box. I create the box with the property dy++ so that it is falling down all the time until it hits the ground. But if I want to create another object and put it above the first so that a collision is made and the box lands on the other box and so on. What is the best way to do this?

I've been trying something like

for(Box b1 : boxes){

for(Box b2 : boxes){

if(b1.intersects(b2) && b1.direction == down){

collision = true

blablablablabla

}

}

Yeah, not exactly like that, but that kind of method. But that does not work very well for me. Do you have an idea how to make it work properly?

### #2Bacterius  Members

Posted 18 January 2013 - 07:09 PM

You only need to handle collisions between any two objects once, so you have a symmetry problem here. If you first handle "A collides with B", then you must not handle "B collides with A" later on. And, by definition, an object can never collide with itself, so you must exclude this case.

I don't know how you would go about doing this with foreach syntax, but the basic idea is that if you have N objects, your loop looks like this:

for A from 0 to N - 1 do
for B from A + 1 to N - 1 do
if (Object A collides with Object B) then // handle stuff

This will ensure you never handle the same collision twice, and also excludes same-object collisions.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #3Patriarch K  Members

Posted 19 January 2013 - 10:33 AM

Thanks a lot. That worked perfectly fine

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.