使用侧面滚动平台类型的游戏并遇到与我的角色一样的奇怪问题。如果他移动到一个平台的侧面,我继续按住移动按钮,他只是坚持它并且直到我松开运动键才会下降。这是我的动作代码:
public void move(float percent){
float desiredVel = speed.x *percent;
float velChange = desiredVel - b2body.getLinearVelocity().x;
float impulse = b2body.getMass() * velChange;
b2body.applyLinearImpulse(new Vector2(impulse,0),b2body.getWorldCenter(),true);
}
这是基于触摸板控制器给出的百分比,但基本上百分比介于-1和1之间。这是我的角色的身体/夹具:
BodyDef bdef = new BodyDef();
bdef.position.set(704/ Constants.PPM, 1000/Constants.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
b2body.setFixedRotation(true);
//Hitbox
FixtureDef fixtureDef = new FixtureDef();
PolygonShape shape = new PolygonShape();
shape.setAsBox(20/Constants.PPM,45/Constants.PPM);
fixtureDef.shape = shape;
//Add in category and mask bits
hitbox = b2body.createFixture(fixtureDef);
hitbox.setUserData("hitbox");
这是平台的主体/夹具:
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set((rect.getX() + rect.getWidth() / 2) / Constants.PPM, (rect.getY() + rect.getHeight() / 2) / Constants.PPM);
body = world.createBody(bdef);
shape.set(new Vector2(-rect.getWidth()/2/Constants.PPM, rect.getHeight()/2/Constants.PPM),new Vector2(rect.getWidth()/2/Constants.PPM, rect.getHeight()/2/Constants.PPM));
fdef.shape = shape;
fdef.friction = 0.5f;
fdef.density = 1;
fdef.filter.categoryBits = Constants.GROUND_BIT;
body.createFixture(fdef).setUserData("platform");
答案 0 :(得分:1)
这是由于玩家推入平台时产生的摩擦力克服了重力。
你可以尝试一些事情: