Hello,
I made a rotation of the ship but how to move it in the right direction? Plunker - [Wrong movement] Simple ship movement in Three.js and JavaScript (free model - link, free texture - link)
const playerMovementSpeed = 5;
const playerRotationSpeed = 3;
function keyboardHandler(dt) {
if (keyboard.pressed("KeyW") || keyboard.pressed("ArrowUp")) {
player.position.z += playerMovementSpeed * dt;
}
if (keyboard.pressed("KeyS") || keyboard.pressed("ArrowDown")) {
player.position.z -= playerMovementSpeed * dt;
}
if (keyboard.pressed("KeyA") || keyboard.pressed("ArrowLeft")) {
player.rotation.z += playerRotationSpeed * dt;
}
if (keyboard.pressed("KeyD") || keyboard.pressed("ArrowRight")) {
player.rotation.z -= playerRotationSpeed * dt;
}
}
Solution is translateY
: Plunker - Simple ship movement in Three.js and JavaScript (plnkr.co)
const playerMovementSpeed = 5;
const playerRotationSpeed = 3;
function keyboardHandler(dt) {
if (keyboard.pressed("KeyW") || keyboard.pressed("ArrowUp")) {
// player.position.z += playerMovementSpeed * dt;
player.translateY(-playerMovementSpeed * dt);
}
if (keyboard.pressed("KeyS") || keyboard.pressed("ArrowDown")) {
// player.position.z -= playerMovementSpeed * dt;
player.translateY(playerMovementSpeed * dt);
}
if (keyboard.pressed("KeyA") || keyboard.pressed("ArrowLeft")) {
player.rotation.z += playerRotationSpeed * dt;
}
if (keyboard.pressed("KeyD") || keyboard.pressed("ArrowRight")) {
player.rotation.z -= playerRotationSpeed * dt;
}
}
It is difficult to make a collision detection for translateY
. I replaced this movement with OimoPhysics: https://plnkr.co/edit/ENV8AfwIztG5YS7g?preview