Tôi nghĩ rằng có một câu trả lời đơn giản cho câu hỏi dường như đơn giản của tôi nhưng tôi có thể hoàn toàn sai. Dù sao tôi mới vào box2dWeb và trong thế giới Box2dWeb của tôi, tôi tạo ra một tầng và một đối tượng rơi đơn giản. Khi tôi "gỡ lỗi vẽ" vào vải của tôi, tôi thấy hộp rơi xuống và mọi thứ. Tất cả tôi muốn làm là đầu ra x vị trí của đối tượng rơi tôi tạo ra vào console.log trình duyệt và nó không hoạt động khá đúng. Console.log chỉ hiển thị vị trí bắt đầu của đối tượng của tôi nhưng số không cập nhật ngay cả khi đối tượng của tôi trong canvas đang rơi. Sau nhiều giờ tìm kiếm với nhiều công cụ tìm kiếm, và những nơi như hướng dẫn Seth Ladds tôi đã trống rỗng. Tôi hy vọng một người nào đó ở đây có thể giúp đỡ. Tôi đã cung cấp một số mã mẫu để giúp giải thích cho bản thân mình tốt hơn một chút. Hy vọng nó giúp. Cảm ơn tất cả những người trả lời.Có cách nào trong Box2dWeb xuất vị trí.x của một đối tượng đến console.log() không?
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
};
Thực ra, 'CreateBody' sẽ trả về' b2Body', bạn có thể lấy vị trí bằng 'crateBody.GetPosition(). X' – sntran
Trong khi .CreateBody() trả về một b2body, .CreateBody(). CreateFixture() trả về một b2fixture, không có phương thức GetPosition() – OneThreeSeven