Jump to content

View more

Image of the Day

Isn't this a lovely apple tempart placeholder thing  #gamedev worth a #screenshotsaturday even I would say. https://t.co/fQH1d0ySIG
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.


Sign up now

Same object collision

2: Adsense

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.


  • You cannot reply to this topic
2 replies to this topic

#1 Patriarch K   Members   

212
Like
0Likes
Like

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?

                           



#2 Bacterius   Members   

13156
Like
1Likes
Like

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.”


#3 Patriarch K   Members   

212
Like
0Likes
Like

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.