我最近从 Gamemaker: Studio 转向 Java 和 Slick2D。我不知道如何描述我的问题,但我会尽力而为。
在Gamemaker中,有一个名为motion_add的功能,它可以使用给定的参数(速度和方向)向对象添加运动。
假设我们有一个对象以 20 像素/帧的速度向下移动。如果您将该对象的角度反转 180 度(因此它现在指向上方)并运行 Motion_add 代码,它会一直减速到 0,然后再次加速。这对于 float 空间运动非常有效(这就是我正在尝试做的)。
我的问题是,如何重新创建它?这是我当前的移动代码:
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 旋转 Sprite 。如果按下 W(使物体加速),它会“引导”物体。如果对象的旋转与 Sprite 的旋转不同,则对象的旋转将设置为 Sprite 的旋转,这就是我的问题所在。
这样做将导致对象立即向新方向射击,且速度不会减慢。如何让它慢下来?
编辑(某种程度上) 我确信这一定有一个名字,如果你以 100 英里/小时的速度驾驶一辆汽车,在行驶时以某种方式使其转向 180 度,然后踩 throttle ,它就会减速然后再次加速。这就是我想要实现的目标。
最佳答案
您必须重新创建此行为。这或许可以称为惰性。您可以向对象添加方向 vector ,以便获得对象的移动方向以及动画指向的有效方向。
然后,当您在运动中改变方向时,您必须减速才能有效地朝新方向移动。
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
}
关于Java/Slick2D 运动问题(添加运动而不是设置它?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383586/