【发布时间】:2019-07-16 20:33:18
【问题描述】:
我目前正在开发一款 3d 游戏,我想使用 wasd 键实现一种第一人称控制。
我下面的 sn-p 包括速度、位置和旋转,其中旋转代表0 和 2*Math.PI 之间的值。
我的问题是如何更新我的代码以使用 w-key 根据当前轮换“直行”,无论在哪里都使用 d 键返回我正面临着,等等。
我相信您知道我的问题是什么 - 我需要某种方法来实现 加速!
任何帮助将不胜感激。
let speed = 0.01,
maxSpeed = 0.01,
friction = 0.91
let position = {
x: 0,
y: 0,
z: 0
},
velocity = {
x: 0,
y: 0,
z: 0
},
rotation = 0;
let update = () => {
if (keyPressed("w") && velocity.z > -maxSpeed) velocity.z -= speed
if (keyPressed("s") && velocity.z < maxSpeed) velocity.z += speed
if (keyPressed("a") && velocity.x > -maxSpeed) velocity.x -= speed
if (keyPressed("d") && velocity.x < maxSpeed) velocity.x += speed
velocity.z *= friction
velocity.x *= friction
position.z += velocity.z * Math.sin(rotation) // this is
position.x += velocity.x * Math.sin(rotation) // not working
}
【问题讨论】:
-
你如何更新
rotation?
标签: javascript 3d html5-canvas controls game-physics