Jump to content

  • Log In with Google      Sign In   
  • Create Account


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: 8152

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#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