Javascript Box2d.js脉冲/力和初始位置
我在“玩”kripken的box2d时遇到了两个问题。我选择这个叉子是因为它似乎是最快和最常用的Javascript Box2d.js脉冲/力和初始位置,javascript,box2d,physics,physics-engine,emscripten,Javascript,Box2d,Physics,Physics Engine,Emscripten,我在“玩”kripken的box2d时遇到了两个问题。我选择这个叉子是因为它似乎是最快和最常用的 API定义了bodyDef上的位置,但您“必须”在bodyDef上给出它 力量、冲动。。。保持与身体的连接,产生意想不到的恒定速度 以前有人有过这种行为吗?有人有什么提示吗 这来自一个复杂的应用程序,但我简化了演示: <html> <head> <script src="http://kripken.github.io/box2d.js/box2d.js">
<html>
<head>
<script src="http://kripken.github.io/box2d.js/box2d.js"></script>
</head>
<body>
<script>
// gravity 0 for top view scene
var world = new Box2D.b2World( new Box2D.b2Vec2(0, 0), true);
var bodyDef = new Box2D.b2BodyDef();
bodyDef.set_type( Box2D.b2_dynamicBody );
bodyDef.set_position(40,40);
var body = world.CreateBody(bodyDef);
// ISSUE 1
// without these two lines real position is 0,0
body.GetPosition().set_x(40);
body.GetPosition().set_y(40);
var dynamicBox = new Box2D.b2PolygonShape();
dynamicBox.SetAsBox(5.0, 5.0);
var fixtureDef = new Box2D.b2FixtureDef();
fixtureDef.set_shape(dynamicBox);
fixtureDef.set_density(1);
fixtureDef.set_friction( 0.8);
fixtureDef.set_restitution( 0.5);
body.CreateFixture(fixtureDef);
//ISSUE 2
// Never ending movements
//body.ApplyLinearImpulse(new Box2D.b2Vec2(5,5),body.GetWorldCenter());
body.ApplyForce(new Box2D.b2Vec2(50,50),body.GetWorldCenter());
function update() {
world.Step(1/30, 10, 10);
world.ClearForces();
console.log(body.GetPosition().get_x()+","+body.GetPosition().get_x());
}
setInterval(update, 1000/60);
</script>
</body>
</html>
//俯视场景的重力为0
var-world=new-Box2D.b2World(new-Box2D.b2Vec2(0,0),true);
var bodyDef=new Box2D.b2BodyDef();
bodyDef.set_type(Box2D.b2_dynamicBody);
车身定义设置位置(40,40);
var body=world.CreateBody(bodyDef);
//问题1
//没有这两条线,实际位置是0,0
body.GetPosition().set_x(40);
body.GetPosition().set_y(40);
var dynamicBox=new-Box2D.b2PolygonShape();
dynamicBox.SetAsBox(5.0,5.0);
var fixtureDef=new-Box2D.b2FixtureDef();
fixtureDef.set_形状(dynamicBox);
固定装置定义设置密度(1);
固定装置设置摩擦(0.8);
固定件定义设置恢复(0.5);
body.CreateFixture(fixtureDef);
//问题2
//永无止境的运动
//body.applylinearpulse(新的Box2D.b2Vec2(5,5),body.GetWorldCenter());
body.ApplyForce(新的Box2D.b2Vec2(50,50),body.GetWorldCenter());
函数更新(){
世界。步骤(1/30、10、10);
世界;
log(body.GetPosition().get_x()+”,“+body.GetPosition().get_x());
}
设置间隔(更新,1000/60);
对于问题1,设置位置应使用b2Vec2参数。试试这个:
bodyDef.set_position( new b2Vec2( 40, 40 ) );
我遇到了更多问题,所以最后我切换到box2dweb。更老,但更多的测试和更稳定