problem when integrate box2d into my own 2d-engine.

Hello all, I use box2d for several month. It works very nice for simple physics games. Now I consider integrate the box2d into my own engine, My target version is box2d-2.1.2, and here are my problems:

Action1:
I'm thinking about unify the spatial manager in my engine, since box2d use the dynamic-tree, I plan to reuse it for all of my game-object(actor). Basically, I want to create those physics object with b2Body, and let box2d manage them automatically. For those non-physics object, I would like add them manually into the dynamic-tree belongs to b2BroadPhase, and update the position and size of the AABB myself. My question are:

Question 1: is there any document introduce how to replace the b2DynamicTree?
Question 2: is it possible to hybrid b2Body and non-b2Body object in b2DynamicTree? (For those game-object, it doesn't need simulate with physics, but can be query by AABB tree).

For question 2, I look the box2d code, it seems like the proxy carry the userData will always cast into a fixture. So it seems like it is not possible to directly use b2DynamicTree in hybrid system. But if I can replace the tree (mention in question 1), at least I can rewrite the GetUserData and give a dummy fixture if it is a non-physics object.

Action2:
I plan to reuse the collision system of box2d in my engine. For those ground, physics-objects it works great, but when I try to use the collision system for non-physics objects, it gets problem. My first solution is create object with a b2Body, and set the b2Body as a sensor. This works fine, but it looks like box2d didn't check collision for static-static object. That will be a problem if I create a projectile (let's say a fireball) as a static sensor b2Body, and I want the fireball trigger the contact callback when it hit the ground --- which is a static b2Body. But it didn't successfully do that since fireball and ground pair are static-static. So My Question is

Queston:
Can box2d collision system manage collision for those static/user-control objects ???

And I have some solution below, which I'm not sure is it feasible:
Solution 1:
Change the box2d source code, add the new b2_customBody enum value. Create non-physics game object with b2Body, set as sensor, set the type to b2_customBody. In the b2Body::ShouldCollide() function, change the code to make it return true when b2_customBody vs any others.
After this, does the new type b2Body work correct with collision system? (the correct means, it works right with filter, group and contact listener...)

I'm not sure if these are clear? Any one can give me some suggestions or is there a better solution ?
You should not need to change Box2D. You can just create your own instance of b2DynamicTree and create your own meaning of the proxies. You can run this in parallel to Box2D. The dynamic tree was created with this in mind.
Hello Erin Catto, thank you for your reply.

I'm not sure if I'm misunderstanding, as your suggestion, it seems like I have to create my own b2DynamicTree, and in the meantime the b2BroadPhase still use the original one? So if I'm not wrong, it will have two trees in the game? And I must maintain the aabb of all objects (object with and without b2Body) in the b2DynamicTree I created ? So will this cause AABB of the b2Body update twice ?

Also, I'm not sure how this can solve the problem in "Action 2" ?

Thanks all the same.
KarasAya wrote:
I'm not sure if I'm misunderstanding, as your suggestion, it seems like I have to create my own b2DynamicTree, and in the meantime the b2BroadPhase still use the original one?


Yes

KarasAya wrote:
So if I'm not wrong, it will have two trees in the game?


Yes

KarasAya wrote:
And I must maintain the aabb of all objects (object with and without b2Body) in the b2DynamicTree I created ?


This depends. Do you want these game objects to interact with Box2D bodies?

KarasAya wrote:
Also, I'm not sure how this can solve the problem in "Action 2"


Sure, you can write your own collision system on top of b2DynamicTree.

It sounds like you want to animate some shapes and generate collisions? There are many ways to do this. You should look at the use cases to determine the best solution. For example, you could handle projectiles inside Box2D by using a sequence of connected ray-casts. What other cases do you have?
Thanks again for your reply. So here is my answer.

Erin Catto wrote:
This depends. Do you want these game objects to interact with Box2D bodies?


Yes

Erin Catto wrote:
It sounds like you want to animate some shapes and generate collisions? There are many ways to do this. You should look at the use cases to determine the best solution. For example, you could handle projectiles inside Box2D by using a sequence of connected ray-casts. What other cases do you have?


Yes, I want the animated-object (it will be controlled by script and our animation system.) are not have physics, and I would like to reuse the box2d collision system so that the physics-object and non-physics-object can have same collision system, for example the filter, collision group and broad-phase and narrow-phase detection.

If I manage the animated-object on my own collision system, it seems the phy and non-phy object will be hard to communicate.
My case here is: the fireball is controlled by script, and will do a sine-curve like move. when it hit something it try to ignite the collided object by sending an "ignite" signal to the object, and meanwhile the collided object will get this "ignite" signal, and check if it is burnable. When the fireball hit the ground it kill itself.

So it is possible the fireball hit a physics object or a non-physics object. And both need receive signal. That's why I think it will be benefit if I unify the collision system.
Well, I'm not sure what you should do. You seem like you want some really advanced usage of Box2D. I recommend reading the manual and studying the code and figure our what makes the most sense for your project. Here are some features you may consider:

- Sensors for detecting collision without generating contact forces
- b2TimeOfImpact for shape casts
- b2World::RayCast and QueryAABB
- b2Contact::SetEnabled
- b2Filter
Ok. Thank you for your suggestion.
I currently solve the collision system by change the box2d source code. I add b2_customBody in b2BodyType:
Code:
enum b2BodyType
{
   b2_staticBody = 0,
   b2_kinematicBody,
   b2_dynamicBody,
    // add {
   b2_customBody,
    // } add end
};


Then in the b2Body::ShouldCollide, I add:
Code:
bool b2Body::ShouldCollide(const b2Body* other) const
{
    if ( m_type == b2_customBody || other->m_type == b2_customBody )
    {
        return true;
    }
    ...
}


Then in the engine, for those non-phys object needs handle collision, I create a b2Body with b2_customBody type, and set it as sensor:

Code:
    b2BodyDef bd;
    bd.allowSleep = false;
    bd.type = b2_customBody;

    b2FixtureDef fd;
    fd.shape = &shape;
    fd.isSensor = true;
    fd.density = 0;
    m_pBody = pPhysics->World()->CreateBody(&bd);
    m_pBody->CreateFixture(&fd);


So this works fine currently, it can make the animated-object detect collision with static,dynamic body, and even custom body, also it can use box2d's dynamic tree for querying. I'm not sure if this will affect box2d's original physics simulation? Since it is a sensor, it will not process narrow phase collision detection which is good, but it is neither static, dynamic nor kinematic, will it be OK during simulation?
You should also disable simulation of the custom body. You will probably lose continuous collision.

Original thread: https://box2d.org/forum/viewtopic.php?t=4897