【问题标题】:limit linear velocity of body android andengine box2d限制body android和engine box2d的线速度
【发布时间】:2013-08-22 08:54:22
【问题描述】:

我移动一个带有盒子体的球,并且在每次碰撞/接触时,我都会将线速度增加 1.1 倍。速度增加但我无法限制速度

代码:

public static final FixtureDef _BALL_FIXTURE_DEF=PhysicsFactory.createFixtureDef(0, 1.0f, 0.0f, false, _CATEGORYBIT_BALL, _MASKBITS_BALL, (short)0);
_ballCoreBody = PhysicsFactory.createCircleBody(_physicsWorld, _ballCore, BodyType.DynamicBody, _BALL_FIXTURE_DEF);
_ballCoreBody.setAngularDamping(0);
_ballCoreBody.setLinearDamping(0);
_ballCoreBody.setActive(true);
_ballCoreBody.setBullet(true);
_ballCoreBody.setGravityScale(0);
this._scene.attachChild(_ballCore);
this._physicsWorld.registerPhysicsConnector(new PhysicsConnector(_ballCore, _ballCoreBody));

在contactListener内部

if(x1.getBody().getLinearVelocity().x<15.0f && x1.getBody().getLinearVelocity().y<15.0f)
x1.getBody().setLinearVelocity(new Vector2(x1.getBody().getLinearVelocity().x*1.2f, x1.getBody().getLinearVelocity().y*1.2f));
else
x1.getBody().setLinearVelocity(new Vector2(x1.getBody().getLinearVelocity().x/1.1f, x1.getBody().getLinearVelocity().y/1.1f));

我如何做到这一点?

【问题讨论】:

    标签: android box2d andengine game-engine game-physics


    【解决方案1】:

    据我所知,您的代码中根本没有限制速度。接触监听器内部的内容是,当速度低于 15.0 时,它会增加 1.2 倍的速度,之后会增加 1.1 倍的速度,因此每次碰撞都会不断加速。这可能更合适,试一试(没有测试过代码,所以可能需要调整):

    float xVel = x1.getBody().getLinearVelocity().x;
    float yVel = x1.getBody().getLinearVelocity().y;
    
    //if you want to be sure the speed is capped in all directions evenly you need to find
    //the speed in the direction and then cap it.
    bool isBelowMaxVel = ( xVel * xVel + yVel, * yVel ) < 225.0f; //15 * 15 = 225 // this is to avoid using a square root
    
    if( isBelowMaxVel ) // only increase speed if max not reached
    {
        x1.getBody().setLinearVelocity(new Vector2( xVel * 1.1f, yVel * 1.1f ));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多