如何让画布上下文跟随Box2dWeb中的主体?

时间:2015-01-12 19:23:58

标签: box2dweb

我使用Box2D.Dynamics.b2DebugDraw渲染我的Box2dWeb世界。如何让画布保持在移动体的中心?

var debugDraw = new Box2D.Dynamics.b2DebugDraw();
debugDraw.SetSprite(document.getElementById("canvasElement").getContext("2d"));
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(Box2D.Dynamics.b2DebugDraw.e_shapeBit | Box2D.Dynamics.b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);

这是在运行模拟的循环中:

window.setInterval( function(){
    _this.world.Step(
        1 / 60   //frame-rate
        ,  10       //velocity iterations
        ,  10       //position iterations
    );
    _this.world.DrawDebugData();
    _this.world.ClearForces();
}, 1000 / 60);

1 个答案:

答案 0 :(得分:0)

翻译运行循环中的canvas元素:

var renderingContext = document.getElementById("canvasElement").getContext("2d");
debugDraw.SetSprite(renderingContext);

// .... snip .....

window.setInterval( function(){
    // ...
    renderingContext.translate(someX, someY); // <--- interesting line
    _this.world.DrawDebugData();
    _this.world.ClearForces();
}, 1000 / 60);