簡體   English   中英

限制機器人和引擎box2d的線速度

[英]limit linear velocity of body android andengine box2d

im使用盒子主體移動球,並且在每次碰撞/接觸時im將線速度提高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));

我該如何實現?

據我所知,您根本沒有在代碼中限制速度。 接觸偵聽器內部的功能是,當速度低於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 ));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM