avoiding physics objects overlapping each other

Hi.

i have a scene in my app where i have to create multiple physics objects.

The problem is, that at certain chance, 2 objects are possible to be created,

‘overlapping’ other. Is there a workaround to avoid this?

One option would be to write a checking function. When an object gets placed, create its body with a collision listener. Then, see if it registers a hit (with another object that it shouldn’t overlap). Then, if it does, have the app choose a different location and the run the checking function again, until it “passes”. 

Best regards,

Brent

by collision listener, do you mean I have to :

  1. revert the object from physical to ‘sensors’ objects

  2. put collision listeners to every object created

which one is the most suitable? If I am not wrong reading the documentation, changing physics object to sensor doesn’t

activate the physics, but still receive collision events?

Yes, both things. Initially create them as sensors, and also make them invisible. Then when the check is complete and the object is clear of any other objects, make it a non-sensor and restore it to visible. And, if you don’t need them to collision check any longer (with the other objects), remove its collision listener.

Brent

one question brent, when I attach collision listener to the sensor objects, how do i differentiate when the objects ‘collide’, or ‘overlapping’ each other?

There is no way to distinguish when a sensor “collides” versus “overlaps”, because it’s the same thing. If an object moves toward a sensor from afar, and touches it, you’ll receive a collision event. And also, if you simply place an object over a sensor (overlap), you will also receive a collision event.

Brent

One option would be to write a checking function. When an object gets placed, create its body with a collision listener. Then, see if it registers a hit (with another object that it shouldn’t overlap). Then, if it does, have the app choose a different location and the run the checking function again, until it “passes”. 

Best regards,

Brent

by collision listener, do you mean I have to :

  1. revert the object from physical to ‘sensors’ objects

  2. put collision listeners to every object created

which one is the most suitable? If I am not wrong reading the documentation, changing physics object to sensor doesn’t

activate the physics, but still receive collision events?

Yes, both things. Initially create them as sensors, and also make them invisible. Then when the check is complete and the object is clear of any other objects, make it a non-sensor and restore it to visible. And, if you don’t need them to collision check any longer (with the other objects), remove its collision listener.

Brent

one question brent, when I attach collision listener to the sensor objects, how do i differentiate when the objects ‘collide’, or ‘overlapping’ each other?

There is no way to distinguish when a sensor “collides” versus “overlaps”, because it’s the same thing. If an object moves toward a sensor from afar, and touches it, you’ll receive a collision event. And also, if you simply place an object over a sensor (overlap), you will also receive a collision event.

Brent