LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: jdogboy on April 06, 2015, 03:25:12

Title: How to do first person controller?
Post by: jdogboy on April 06, 2015, 03:25:12
Hi, I have been trying to create a first person controller and I have the mouse look perfectly well, but how do I walk in the direction relative to the players yaw? I have used this code here and it doesn't seem to work.

public void walkForward(float distance)
{
    position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
    position.z += distance * (float)Math.cos(Math.toRadians(yaw));
}

NOTE - distance is mouseSensitivity * deltaTime
Title: Re: How to do first person controller?
Post by: Andrew_3ds on April 06, 2015, 04:28:55
It should be this:

float speed = 0.175f */change to the speed you want in your game/* * delta;

public void walkForward() {
    position.x -= (float) (Math.sin(Math.toRadians(yaw)) * speed)
    position.z += (float) (Math.cos(Math.toRadians(yaw)) * speed)
}


Where speed is the units of movement you want per frame. Mouse sensitivity shouldn't control how fast the player moves.
Title: Re: How to do first person controller?
Post by: jdogboy on April 06, 2015, 23:03:46
Thank you so much!
Title: Re: How to do first person controller?
Post by: j8a2b0e7 on April 19, 2015, 14:31:06
Sorry for hijacking this thread, but what should I add to the y-Coordinate if I wanted to have a free-moving camera in every direction (like a spectator)?
Title: Re: How to do first person controller?
Post by: quew8 on April 26, 2015, 16:54:14
So to add in a pitch:


float speed = 0.175f */change to the speed you want in your game/* * delta;

public void walkForward() {
    position.x += (float) (Math.cos(pitch) * Math.sin(yaw) * speed);
    position.y += (float) (Math.sin(pitch) * speed);
    position.z += (float) (Math.cos(pitch) * Math.cos(yaw) * speed);
}


(And make sure your pitch and yaw is in radians and not degrees)