0

ボックスボディでボールを動かし、各衝突/接触で線形速度を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));

どうすればこれを達成できますか?

4

1 に答える 1

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 ));
}
于 2013-08-26T15:34:33.333 に答える