Detecting if a static / not enabled body WILL collide when enabled.

Topics: Developer Forum, User Forum
Aug 27, 2011 at 6:48 PM

Hi everyone.

 

I have a problem that i just cant solve in a nice way.

 

I have a situation where there is a world with bodys in it all working away doing there thing.

 

I then have an object thats body is set to not enabled, is there a nice way of checking if the body is currently colliding with anything? i.e. to know if it would be a clean spawn should I make it enabled and set it to dynamic?

 

Thanks in advance.

Aug 28, 2011 at 8:13 AM

Two options come to mind:

  1. Instead of setting your object to not enabled, set IsSensor = true and Dynamic = false.  This means it won't be influenced by gravity, but you can still subscribe to collision and seperation events, without it influencing the simulation with collision response.
  2. Create a body with the exact same geometry and set THAT as a sensor, if there's some reason the body must be set to enabled = false;
Aug 28, 2011 at 9:52 AM

Thanks Quantumplation,

 

That sounds like a very nice way of doing it! I will see how things go with  method 1 which seems a leaner approach to me. Ill try and report back with how things go.

 

Aug 28, 2011 at 10:35 AM

As expected that did the job perfectly. Thanks again.

Aug 30, 2011 at 6:51 PM

For anyone else finding this post while trying to solve the same kind of problem here is how I did it in the end.

In this example I use the word Item to represent the class that contains a body etc, this could be your player / bullet / whatever.

In your Item class subscribe to the bodies on collision and on separation events.

            this.Body.OnCollision += Body_OnCollision;
            this.Body.OnSeparation +=  Body_OnSeperation;

then setup a member for the item to hold a count of collisions and separations.

private int _canBePlacedCounter = 0;

On the event handler methods, increase of decrease the member count. In the code below there are extra conditions as for myself, I only want to perform these operations when an item is being placed into the "world".

        private bool Body_OnCollision(Fixture fixturea, Fixture fixtureb, Contact contact)
        {
            if(this.IsBeingPlaced)
            {
                _canBePlacedCounter++;
            }

            return true;
        }

        private void Body_OnSeperation(Fixture fixturea, Fixture fixtureb)
        {
            if (this.IsBeingPlaced)
            {
                _canBePlacedCounter--;
            }
        }

We then can setup a simple public property (this really should be a method if we want to get into coding standards but this is not the time or place)

 

        public bool CanBeDropped
        {
            get
            {
                if (_canBePlacedCounter == 0) return true;
                else return false;
            }
        }

 

The reason for this implementation is, I originally had a bool which I set to true or false whenever I got one of the events. The problem is... If your item collides with item A, then collides with item B then leaves item A you get a reading of not colliding. So... using a counter like this we can count in and count out all of the collisions and separations. Belive me this works like an absolute charm.

 

Hope it is of use to someone out there.