PhysicsManager.testAABB ignores static bodies!

Hi.
I’m trying to make a function which finds an empty area near given position to spawn object.
I noticed that testAABB ignores static colliders. Is there any workaround?
Do you have any idea @slackmoehrle ?
Thanks in advance!

I don’t in JavaScript. @Big_Bear maybe can help us.

1 Like

These images give better idea what’s the problem:

I start to query area around spaceship. I use a rect with same size of crystal sprite.

rect_result = this.Physics_Manager.testAABB(rect);

if area is not empty it tries next option. You can see in the top image, numbered orange boxes show occupied areas.
It works perfect with dynamic boxes but not with static boxes.
By the way, ray casting works with both static and dynamic boxes. testAABB and testPoint doesn’t work.

If we solve it everybody can use it to spawn objects in physics world.

try using a kinematic body type instead of static.

1 Like

I’ve tried all other options (kinematic, static, animated) but result is same.

I found something here:
engine\cocos2d\core\physics\platform\CCPhysicsAABBQueryCallback.js

PhysicsAABBQueryCallback.prototype.ReportFixture = function (fixture) {
var body = fixture.GetBody();
if (body.GetType() === b2.Body.b2_dynamicBody) {
    if (this._isPoint) {
        if (fixture.TestPoint(this._point)) {
            this._fixtures.push(fixture);
            // We are done, terminate the query.
            return false;
        }
    }
    else {
        this._fixtures.push(fixture);
    }
}

// True to continue the query, false to terminate the query.
return true;
};

i tried to remove

if (body.GetType() === b2.Body.b2_dynamicBody) {

section but does’t affect anything.
I suspect there is something filtering bodies which is not dynamic.

@Big_Bear do you have any ideas my friend?

as you mentioned, “testAABB” only affected on dynamic body.
for your requirement, I provide a way of thinking:
if you do not want to customize engine, you can use a rigid object instead of “testAABB”.
when you want to check that place is a empty area or not, you can place the rigid object there and check if there are any contact event.
if we received a contact event then it means that area is not a empty area, then we move the test rigid body away, and do not spawn object
if we do not received any contact events then is means that area is a empty area, then we can spawn object and move away the test rigid body

1 Like

Ok thanks for this valuable information. I was thinking same idea yesterday. I’ll put sensor objects to determine if places are empty or not then will spawn object.

In case I need, I just want to learn where should I edit in source code to make testAABB work with both static and dynamic objects?

I have managed to customize js engine and it works perfect on browser but I can’t get same result in simulator. I changed “PhysicsAABBQueryCallback.prototype.ReportFixture” as I mentioned above and it’s ok for web build but unable to find where should I change in cocos2d-x engine or what should I do? Any suggestions?

I have found the source code to edit:

C:\CocosCreator\resources\cocos2d-x\cocos\editor-support\creator\physics\CCPhysicsAABBQueryCallback.cpp

Thanks.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.