htmlcanvasbox2dbox2dweb

Box2dWeb missing ApplyLinearImpuse() and ApplyAngularImpulse() methods?


I have been trying to get to grips with HTML5 game writing coming from an XNA / Farseer background.

It seems that box2dweb is missing ApplyLinearImpuse() and ApplyAngularImpulse() methods.

I have even looked at the source here and it seems to be the case.

Does anyone know why these methods are not provided?


Solution

  • Just now I needed the same thing. Fortunately the implementation or ApplyAngularImpulse is very simple. You can add the following to your code to patch it in to box2d:

    Box2Dweb

    b2Body.prototype.ApplyAngularImpulse = function(impulse) {
        if (this.IsAwake() == false) {
            this.SetAwake(true);
        }
        this.m_angularVelocity += this.m_invI * impulse;
    };
    

    Box2Djs

    b2Body.prototype.ApplyAngularImpulse = function(impulse) {
        if (this.IsSleeping() == false)
        {
            this.m_angularVelocity += this.m_invI * impulse;
        }
    };
    

    In general, to get at features in the C++ version that haven't been ported to the Flash or JavaScript versions, you can have a look at the original source, and port them over yourself as above. Even if you're not familiar with C++, the code is fairly approachable.