首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让画布上下文跟随Box2dWeb中的主体?

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

Stack Overflow用户
提问于 2015-01-12 19:23:58
回答 1查看 76关注 0票数 0

我用Box2D.Dynamics.b2DebugDraw来渲染我的Box2dWeb世界。我怎样才能让画布保持在移动身体的中心位置?

代码语言:javascript
复制
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);

在运行模拟的循环中:

代码语言:javascript
复制
window.setInterval( function(){
    _this.world.Step(
        1 / 60   //frame-rate
        ,  10       //velocity iterations
        ,  10       //position iterations
    );
    _this.world.DrawDebugData();
    _this.world.ClearForces();
}, 1000 / 60);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-12 19:43:18

在run循环中转换canvas元素:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27909265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档