2014-12-26 39 views
1

目的是限制一个box2d的物理体的移动在一定的轴 - 向水平或垂直移动,这里是主体定义:限制沿轴线在andEngine 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轴的主体,加速度计,以便它使只有垂直:

在主游戏循环,已经设置其线速度为0的x分量:

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.如何限制其沿一个方向的移动?

回答

1

我从动态变化的Box2D的物体的种类,以运动解决了这个问题,从here了解身体类型,发现在我的情况下,运动的身体是一个动态的身体天色渐受一切力量更好物理世界。

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

,并设置其沿y轴的速度这样的垂直运动:

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