Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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.

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

#1 Patriarch K   Members   -  Reputation: 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?

                           



Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 11532

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   -  Reputation: 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.



PARTNERS