I'm making basic fps controls. I managed to apply acceleration to a velocity vector and of course velocity to position. Everything works perfectly and the character accelerates and decelerates in 3D space. But I can't seem to figure out how to apply direction to the character. I'm trying to move the camera (character) towards the direction I'm looking at. I'm very sure the fwd, right, and up unit vectors are fine.
By the time I get to adding my velocity to the position:
// Acceleration already calculated and it's increasing and decreasing velocity every frame.
transform.position.x += this.velocity.x * Game.Time.step;
transform.position.y += this.velocity.y * Game.Time.step;
transform.position.z += this.velocity.z * Game.Time.step;
I can't do something like this this:
// No good.
transform.position.x += this.velocity.x * transform.forward.x * Game.Time.step;
transform.position.y += this.velocity.y * transform.forward.y * Game.Time.step;
transform.position.z += this.velocity.z * transform.forward.z * Game.Time.step;
because it won't work. I'm able to walk forward, but not to the right (nor left).
Btw, before adding acceleration to the mix, I would do something like this:
// Used to do this, but no acceleration. Only moves as soon as I press a key.
if (Game.Input.Key.upArrow.down) Game.camera.transform.moveZ(-speed);
if (Game.Input.Key.downArrow.down) Game.camera.transform.moveZ( speed);
if (Game.Input.Key.rightArrow.down) Game.camera.transform.moveX( speed);
if (Game.Input.Key.leftArrow.down) Game.camera.transform.moveX(-speed);
...and the moveZ function would look something like this:
// Move X works the same obviously, just replace forward with right.
moveZ(speed) {
this.position.x += speed * this.forward.x;
this.position.y += speed * this.forward.y;
this.position.z += speed * this.forward.z;
}
But of course this clearly doesn't accelerate the character, but my camera would move towards the direction I was looking at. I'm sure this is a very basic question with an answer I should already know
Oh by any chance you want to take a look at the acceleration function I wrote, you can see it at this pastebin. Granted, I'm sure there's way simpler ways of doing it.