【问题标题】:Restricting movement of box2d body along an axis in andEngine在 andEngine 中限制 box2d 主体沿轴的移动
【发布时间】: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


    【解决方案1】:

    我通过将 box2d body 的类型从 dynamic 更改为 kinematic 解决了这个问题,从 here 阅读了有关 body types 的信息,并发现在我的情况下 kinematic body 作为一个动态的 body 更好,它受到所有力的影响在物理世界中。

    ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ball, BodyType.KinematicBody, FIXTURE_DEF);
    

    并像这样设置它沿 y 轴的速度以进行垂直运动:

    penBody.setLinearVelocity(mPhysicsWorld.getGravity().x, 0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多