(SOLVED) Directly controlling dynamic bodies.

Consider the following code
Code:
void Actor::Move(float x, float y)
{
    base_body->SetType(b2_dynamicBody);
    this->x = x; this->y = y;
   
    base_body->SetTransform(b2Vec2(x*PHYS_SCALE,y*PHYS_SCALE), DEG2RAD(angle));
}


Now reading the documentation would lead me to believe that calling SetTransform would lock it in place with the Actor invoking the Move function. As a result, If i were to call Move(400,300); every time the game steps, It can be assumed that the body will stay at the scaled location of 0.4 and 0.3

However, my console output shows me results to the contrary. It shows the body moving according to the physics simulation while the graphic the actor displays stays in place, where the code that moves the graphic is as follows
Code:
void Actor::PostStep()
{
    b2Vec2 pos = base_body->GetPosition();
// m2dtransform moves a node in the graphics engine to the specified position in the OGL viewport
    m2dTransform(graphic->GetNodeHandle(), pos.x*PHYS_UNSCALE, pos.y*PHYS_UNSCALE, angle);
}


Very very puzzling... anyone care to shed some light on this anomoly? My goal is to be able to move actors around and still be able to generate collisions with all types of objects since kinematic bodies wont interact with other kinematic bodies. Setting the body to kinematic on it's movement will keep the body in place.

EDIT: I've managed to make a work-around for the time being. What I am doing is calling world->Step() only after all the game's entities have stepped and it seems to work all the same. That way, i can call Move (which sets the body's type to kinematic and sets its transform) and then the derived entity must call SetPhysical, which sets its type to dynamic. Then when the world is updated, collision events are generated. At that point, the entities will be physical and collision events will be generated even if the entity called Move or Rotate.

Erin, this is only a suggestion, but if there was a way to set the location and orientation of a dynamic body, then that would be of great to help to people who are wraping Box2D. Either that, or since kinematic bodies can also move, having them generate collision events between each other would be even better. Im going to keep tinkering and see if I can come up with a better solution.
Hate to bump my own post, but some input would be appreceated.
Do you want any physics simulation at all? If not then you have to do some trickery like setting gravity to zero and disabling every contact in the pre-solve callback.
The thing is that I am trying to use Box2D's callbacks to drive collision detection in my framework. So by providing each actor with a default sensor bounding box I will be able to do just that. The callback object does the following:

Code:
class StageContactListener : public b2ContactListener
{
    public:
        StageContactListener(){};
        ~StageContactListener(){};

        void BeginContact(b2Contact* contact)
        {
            Actor* act1 = (Actor*)contact->GetFixtureA()->GetBody()->GetUserData();
            Actor* act2 = (Actor*)contact->GetFixtureB()->GetBody()->GetUserData();
            act1->onCollide(act2->GetID(),act2->GetName());
            act2->onCollide(act1->GetID(), act1->GetName());
        }
        void EndContact(b2Contact* contact)
        {
            Actor* act1 = (Actor*)contact->GetFixtureA()->GetBody()->GetUserData();
            Actor* act2 = (Actor*)contact->GetFixtureB()->GetBody()->GetUserData();
            act1->onEndCollide(act2->GetID(),act2->GetName());
            act2->onEndCollide(act1->GetID(),act1->GetName());
        }
        void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
        { }
        void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
        { }
};

and the Actor's collide event functions are prototyped as follows:
Code:
virtual void onCollide(int act_id, const string& act_name){};
virtual void onEndCollide(int act_id, const string& act_name){};


However, the trouble I am having is keeping the box synced up with the actor while it is meant to be directly controlled, and then do the opposite when the actors SetPhysical() method is invoked. The quickest fix would be, honestly, if Kinematic bodies generated collisions with other kinematic bodies, but I know that isn't likely to be implemented.

And in the facepalm department, can you spot what's wrong with this code? This is one of the functions I use to determine bounding volumes based on the sprite's data. (I already have fixed it, I just want someone to laugh with me on this one.)
Code:
float Animation::GetBoundingCircleRadius()
{
    if(max_width >= max_height)
        return max_width;
    else
        return max_height;
}
Figured it out. Here's the code that did it for me.

Code:
void Actor::Move(float x, float y)
{
    base_body->SetType(b2_kinematicBody);
    this->x = x;this->y = y;
    base_body->SetTransform(b2Vec2(x*PHYS_SCALE,y*PHYS_SCALE), DEG2RAD(angle));
}
void Actor::Move(float x, float y, float speed)
{
    base_body->SetType(b2_kinematicBody);

    b2Vec2 curr_pos = base_body->GetPosition();

    b2Vec2 destination(x*PHYS_SCALE,y*PHYS_SCALE);
    b2Vec2 trav_vec((destination.x-curr_pos.x)*speed,(destination.y-curr_pos.y)*speed);

    base_body->SetLinearVelocity(trav_vec);
}


First one snaps the actor right to the point at the cost of no physical simulation and the second one moves the actor to the point at the given speed. I kicked myself when I figured it out. Just goes to show how easy it can be to overcomplicate something so stupid and trivial.

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