2013-08-22 112 views
0

即時移動一個帶有盒體的球,並在每次碰撞/接觸時將線速度提高1.1倍。的速度增加,但我不是能夠限制速度身體的極限線速度andengine box2d

代碼:

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)); 

我如何實現這一目標?

回答

0

從我可以看到你沒有在代碼中限制速度。接觸聽者內部的是,當速度低於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)); 
}