【发布时间】:2014-12-26 07:23:57
【问题描述】:
目的是限制 box2d 物理体在某个轴上的运动 - 水平或垂直移动,这是体定义:
Body ballBody;
Sprite ball;
ball = new AnimatedSprite(x_end, y_end-ballTextureRegion.getHeight(), this.ballTextureRegion, this.getVertexBufferObjectManager());
ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ball, BodyType.DynamicBody, FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, ballBody, true, true));
scene.registerUpdateHandler(this.mPhysicsWorld);
scene.attachChild(ball);
使用andEngines IAccelerationListener通过加速度计,球在各个方向上移动,尝试限制身体沿x轴移动,使其仅垂直移动:
在主游戏循环中,将其线速度的x分量设置为0:
scene.registerUpdateHandler(new IUpdateHandler() {
public void reset() {
}
// main game loop
public void onUpdate(float pSecondsElapsed) {
ballBody.setLinearVelocity(0, ballBody.getLinearVelocity().y); // set x velocity as 0
}
});
但是现在小球也可以水平移动了,它的水平速度现在变小了,但不是完全为0。如何限制它只沿一个方向移动?
【问题讨论】:
标签: box2d andengine accelerometer game-physics