我想我的看似简单的问题有一个简单的答案,但我可能完全错了。无论如何,我是box2dWeb的新手,在我的Box2dWeb世界中,我创建了一个地板和一个简单的坠落物体。当我“调试绘制”到我的画布时,我看到盒子掉落了一切。我想要做的就是将我创建的下降对象的x位置输出到browser console.log中,它不能正常工作。 console.log只显示我的对象的起始位置,但即使画布中的对象正在下降,该数字也不会更新。经过几个小时的搜索引擎搜索,以及Seth Ladds教程之类的地方我都空了。我希望有人可以提供帮助。我提供了一些示例代码来帮助自己解释一下。希望能帮助到你。感谢所有回复的人。
var world;
function init() {
var b2Vec2 = Box2D.Common.Math.b2Vec2
, b2BodyDef = Box2D.Dynamics.b2BodyDef
, b2Body = Box2D.Dynamics.b2Body
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
, b2Fixture = Box2D.Dynamics.b2Fixture
, b2World = Box2D.Dynamics.b2World
, b2MassData = Box2D.Collision.Shapes.b2MassData
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
, b2DebugDraw = Box2D.Dynamics.b2DebugDraw
;
world = new b2World(
new b2Vec2(0, 10) //gravity
, true //allow sleep
);
var fixDef = new b2FixtureDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
var bodyDef = new b2BodyDef;
//create ground
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = 9;
bodyDef.position.y = 13;
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(10, 0.5);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//FIXTURE - define fixture
crateFixture = new b2FixtureDef;
//set object attributes
crateFixture.density = 0.9;
crateFixture.friction = 0.5;
crateFixture.restitution = 0.5;
//BODY - define body
crateDef = new b2BodyDef;
//setup type
crateDef.type = b2Body.b2_dynamicBody;
crateDef.position.x = 5;
crateDef.position.y = 5;
crateDef.angle = 65;
//SHAPE - define shape
crateFixture.shape = new b2PolygonShape;
//define shape
crateFixture.shape.SetAsBox(2, 2);
//add to our world
world.CreateBody(crateDef).CreateFixture(crateFixture);
//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
window.setInterval(update, 1000 / 60);
};
function update() {
world.Step(
1 / 60 //frame-rate
, 10 //velocity iterations
, 10 //position iterations
);
world.DrawDebugData();
world.ClearForces();
console.log('the crate is located at ' + crateDef.position.x); //position of crate doesnt update
};
答案 0 :(得分:5)
好的,所以不,谢谢你没有专门针对Box2DWeb的任何真实文档,我终于想到了这一点。我很快也想感谢Jer In Chicago的回复,谢谢兄弟。无论如何答案:
将对象添加到您的世界后,就像这样:
crateFixture = world.CreateBody(crateBodyDef).CreateFixture(crateFixtureDef);
您可以按如下方式获取对象的值(在我的情况下是crateBody):
console.log('crate X: ' + crateFixture.GetBody().GetPosition().x);
console.log('crate Y: ' + crateFixture.GetBody().GetPosition().y);
console.log('crate Angle: ' + crateFixture.GetBody().GetAngle());
希望这有助于其他人!
答案 1 :(得分:2)
好吧,找不到Box2dWeb的api / doc,所以我下载了源代码并确认了正文应该有一个位置...而不是试图使用crateBody var,让我们循环遍历所有的机构在世上,看看我们是否可以打印他们的坐标...祝你好运!
修改:试试这个
// Add after world step call
for (b = world.GetBodyList(); b; b = b.GetNext()) {
console.log('the object is located at (' + b.position.x + ',' + b.position.y + ')');
}
答案 2 :(得分:0)
var body = world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
console.log(body.GetWorldCenter().x);
或
var body = world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
console.log(body.GetPosition().x);
或
var body = world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
console.log(body.GetWorldPoint(new b2Vec2(0,0)).x);
其中(0,0)是本地坐标中身体中心的坐标。 GetWorldPoint
函数将其转换为世界坐标。
答案 3 :(得分:0)
我在使用Box2dWeb时遇到了类似的问题。感谢您发布此问题。使用@JerinChicago提示我找到了解决方案。
for (var b = world.GetBodyList() ; b; b = b.GetNext()) {
console.log('the object is located at (' + b.GetPosition().x + ',' + b.GetPosition().y + ')');
}
我希望这会有所帮助。