我制作平台游戏角色控件,类似这样 -> BOX2D platformer 我正在使用 ContactListener 接口(interface)。 我的问题是链接中代码中的这些行(摩擦)->
if(!grounded) {
playerPhysicsFixture.setFriction(0f);
playerSensorFixture.setFriction(0f);
} else {
if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D) && stillTime > 0.2) {
playerPhysicsFixture.setFriction(100f);
playerSensorFixture.setFriction(100f);
}
else {
playerPhysicsFixture.setFriction(0.2f);
playerSensorFixture.setFriction(0.2f);
}
}
当脚已经接触地面时,我不知道如何改变摩擦力。当我更改角色的 onUpdate() 方法中的摩擦力时,已启动的接触摩擦力不会改变。有人可以帮我解决这个问题吗?
最佳答案
来自 b2Fixture:
// Set the coefficient of friction. This will _not_ change the friction of
// existing contacts.
void SetFriction(float32 friction);
所以你是完全正确的。在 Box2D 的最新版本中,他们添加了 Contact.setFriction()
来弥补这种行为。我不确定在哪个版本的 libGDX 中添加了这个版本的 Box2D,但至少在 libGDX 的 nightlies 中这些新方法是可用的。
因此,请在 ContactListener
中设置 Contact
上的摩擦力。
关于box2d - 当 body 开始与地面接触时,如何改变 body 的摩擦力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362396/