Java/Slick2D 运动问题(添加运动而不是设置运动?)
Java/Slick2D Movement woes (Adding motion instead of setting it?)
我最近从 Gamemaker: Studio 转到 Java 和 Slick2D。我不确定如何描述我的问题,但我会尽力而为。
在 Gamemaker 中,有一个名为 motion_add 的功能可以为具有给定参数(速度和方向)的对象添加运动。
假设我们有一个物体以 20 像素/帧的速度向下移动。如果你将该对象的角度反转 180 度(因此它现在指向上方)并且 运行 motion_add 代码,它会一直减速到 0,然后再次加速。这对于浮动 space 运动非常有效(这是我正在尝试做的)。
我的问题是,如何重新创建它?这是我当前的移动代码:
private void doPlayerMovement(Input input, int delta) {
// Calculate rotation speed
rotateSpeed = rotateSpeedBase - (speed / 5);
if (rotateSpeed < 0.1f) { rotateSpeed = 0.1f; }
// Get input
if (input.isKeyDown(Input.KEY_A)) {
// Rotate left
if (input.isKeyDown(Input.KEY_W)) { rotation -= rotateSpeed * delta; }
sprite.rotate(-rotateSpeed * delta);
}
if (input.isKeyDown(Input.KEY_D)) {
// Rotate right
if (input.isKeyDown(Input.KEY_W)) { rotation += rotateSpeed * delta; }
sprite.rotate(rotateSpeed * delta);
}
if (input.isKeyDown(Input.KEY_W)) {
// Accelerate
speed += acceleration * delta;
} if (input.isKeyDown(Input.KEY_S)) {
// Decelerate
speed -= acceleration * delta;
} else {
if (speed > 0) { speed -= friction; }
}
// Clamping
if (speed > maxSpeed) { speed = maxSpeed; } else if (speed < minSpeed) { speed = minSpeed; }
if (rotation > 360 || rotation < -360) { rotation = 0; }
// Make sure rotation catches up with image angle, if necessairy
if (rotation != sprite.getRotation() && input.isKeyDown(Input.KEY_W)) {
rotation = sprite.getRotation();
}
// Update position
position.x += speed * Math.sin(Math.toRadians(rotation)) * delta;
position.y -= speed * Math.cos(Math.toRadians(rotation)) * delta;
会发生什么:
A和D旋转精灵。如果按下 W(加速物体),它 'steers' 物体。如果对象的旋转与 sprite 的旋转不同,则对象的旋转将设置为 sprite 的旋转,那就是我的问题所在。
这样做会导致物体立即朝新的方向射击,速度不会减慢。我如何让它变慢?
编辑(某种程度上)
我敢肯定这肯定有一个名字,如果你以 100 英里每小时的速度驾驶汽车,让它以某种方式转 180 度在它移动时,然后踩油门,它'我会减速然后再次加速。这就是我想要实现的目标。
您必须重新创建此行为。它可能被称为惯性。您可以向对象添加一个方向矢量,这样您就有了它要去的方向和动画指向的有效方向。
然后,当你在运动中改变方向时,你必须减速才能有效地朝着新的方向移动。
Vector movementVector=...; // choose whatever class you want
Vector pointingVector=...;
if (movementVector.x == -pointingVector.x || movementVector.y == -pointingVector.y) {
// if pressing W then decelerate
// if reaching speed == 0 then change movementVector value and accelerate in the other way
}
我最近从 Gamemaker: Studio 转到 Java 和 Slick2D。我不确定如何描述我的问题,但我会尽力而为。
在 Gamemaker 中,有一个名为 motion_add 的功能可以为具有给定参数(速度和方向)的对象添加运动。
假设我们有一个物体以 20 像素/帧的速度向下移动。如果你将该对象的角度反转 180 度(因此它现在指向上方)并且 运行 motion_add 代码,它会一直减速到 0,然后再次加速。这对于浮动 space 运动非常有效(这是我正在尝试做的)。
我的问题是,如何重新创建它?这是我当前的移动代码:
private void doPlayerMovement(Input input, int delta) {
// Calculate rotation speed
rotateSpeed = rotateSpeedBase - (speed / 5);
if (rotateSpeed < 0.1f) { rotateSpeed = 0.1f; }
// Get input
if (input.isKeyDown(Input.KEY_A)) {
// Rotate left
if (input.isKeyDown(Input.KEY_W)) { rotation -= rotateSpeed * delta; }
sprite.rotate(-rotateSpeed * delta);
}
if (input.isKeyDown(Input.KEY_D)) {
// Rotate right
if (input.isKeyDown(Input.KEY_W)) { rotation += rotateSpeed * delta; }
sprite.rotate(rotateSpeed * delta);
}
if (input.isKeyDown(Input.KEY_W)) {
// Accelerate
speed += acceleration * delta;
} if (input.isKeyDown(Input.KEY_S)) {
// Decelerate
speed -= acceleration * delta;
} else {
if (speed > 0) { speed -= friction; }
}
// Clamping
if (speed > maxSpeed) { speed = maxSpeed; } else if (speed < minSpeed) { speed = minSpeed; }
if (rotation > 360 || rotation < -360) { rotation = 0; }
// Make sure rotation catches up with image angle, if necessairy
if (rotation != sprite.getRotation() && input.isKeyDown(Input.KEY_W)) {
rotation = sprite.getRotation();
}
// Update position
position.x += speed * Math.sin(Math.toRadians(rotation)) * delta;
position.y -= speed * Math.cos(Math.toRadians(rotation)) * delta;
会发生什么:
A和D旋转精灵。如果按下 W(加速物体),它 'steers' 物体。如果对象的旋转与 sprite 的旋转不同,则对象的旋转将设置为 sprite 的旋转,那就是我的问题所在。
这样做会导致物体立即朝新的方向射击,速度不会减慢。我如何让它变慢?
编辑(某种程度上) 我敢肯定这肯定有一个名字,如果你以 100 英里每小时的速度驾驶汽车,让它以某种方式转 180 度在它移动时,然后踩油门,它'我会减速然后再次加速。这就是我想要实现的目标。
您必须重新创建此行为。它可能被称为惯性。您可以向对象添加一个方向矢量,这样您就有了它要去的方向和动画指向的有效方向。
然后,当你在运动中改变方向时,你必须减速才能有效地朝着新的方向移动。
Vector movementVector=...; // choose whatever class you want
Vector pointingVector=...;
if (movementVector.x == -pointingVector.x || movementVector.y == -pointingVector.y) {
// if pressing W then decelerate
// if reaching speed == 0 then change movementVector value and accelerate in the other way
}