libgdx 向矩形所面对的方向施加力
libgdx apply force in direction rectangle is facing
我刚开始使用 box2d,我试图让火箭(现在是矩形)飞起来,游戏开始时它只是垂直站立,我想从矩形的底部施加力,因为那是引擎所在的位置,也是矩形所面对的方向。
我试过这样做
body.applyLinearImpulse(getUserData().getBoosterLinearImpulse(), new Vector2(body.getWorldCenter().x, body.getWorldCenter().y - Constants.ROCKET_HEIGHT), true);
这是一个在按下屏幕右下角时执行的方法。它仅在矩形静止时有效。
我显然不知道我在做什么。
我还有另一个问题:applyLineairForce 和 applyForce 之间有什么区别,我如何最好地学习使用 box2d,因为我觉得它并不容易(这不是问题)?
如果我没理解错的话,您想知道 applyLinearImpulse 和 applyForce 之间的区别。冲量是一次性施加的力,通常用于游戏中的跳跃,而力是每一帧都施加的东西,比如提高汽车的速度。
为了给你的火箭施加力,你可以使用这样的东西:
// gets x force based on angle
float x = (float)Math.sin(body.getAngle() - Math.PI); // minus PI as objects start off facing right
// gets y force based on angle
float y = (float)Math.cos(body.getAngle());
//apply force to center (applies force to middle so no rotation )
//body.applyForceToCenter( new Vector2(
// body.getMass()* (x * 12),
// body.getMass()*(y*12)), true);
//NOTE: bodies must be set to .fixedRotation = false in order to rotate;
//apply force to a point on body (will create rotational force )
body.applyForce( new Vector2(
body.getMass()* (x * 12),//x force to apply
body.getMass()* (y * 12)), //y force to apply
// apply force to body at 0.5f(halfway for 1f wide object) x and -5 y
body.getWorldPoint(new Vector2(0.5f,-5)),true);
我刚开始使用 box2d,我试图让火箭(现在是矩形)飞起来,游戏开始时它只是垂直站立,我想从矩形的底部施加力,因为那是引擎所在的位置,也是矩形所面对的方向。 我试过这样做
body.applyLinearImpulse(getUserData().getBoosterLinearImpulse(), new Vector2(body.getWorldCenter().x, body.getWorldCenter().y - Constants.ROCKET_HEIGHT), true);
这是一个在按下屏幕右下角时执行的方法。它仅在矩形静止时有效。 我显然不知道我在做什么。 我还有另一个问题:applyLineairForce 和 applyForce 之间有什么区别,我如何最好地学习使用 box2d,因为我觉得它并不容易(这不是问题)?
如果我没理解错的话,您想知道 applyLinearImpulse 和 applyForce 之间的区别。冲量是一次性施加的力,通常用于游戏中的跳跃,而力是每一帧都施加的东西,比如提高汽车的速度。
为了给你的火箭施加力,你可以使用这样的东西:
// gets x force based on angle
float x = (float)Math.sin(body.getAngle() - Math.PI); // minus PI as objects start off facing right
// gets y force based on angle
float y = (float)Math.cos(body.getAngle());
//apply force to center (applies force to middle so no rotation )
//body.applyForceToCenter( new Vector2(
// body.getMass()* (x * 12),
// body.getMass()*(y*12)), true);
//NOTE: bodies must be set to .fixedRotation = false in order to rotate;
//apply force to a point on body (will create rotational force )
body.applyForce( new Vector2(
body.getMass()* (x * 12),//x force to apply
body.getMass()* (y * 12)), //y force to apply
// apply force to body at 0.5f(halfway for 1f wide object) x and -5 y
body.getWorldPoint(new Vector2(0.5f,-5)),true);