LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: TechNick6425 on July 27, 2013, 19:41:50

Title: Using pitch and yaw with movement?
Post by: TechNick6425 on July 27, 2013, 19:41:50
So, I'm trying to develop a game engine, and I have code that triggers when a key is pressed. I want the movement keys to move players in a certain direction based on their yaw. How would I do the math for this?
Title: Re: Using pitch and yaw with movement?
Post by: quew8 on July 28, 2013, 09:39:39
Forward Direction:
        dx = sin(yaw)
        dz = cos(yaw)
Title: Re: Using pitch and yaw with movement?
Post by: TechNick6425 on July 28, 2013, 10:14:39
Thanks! I wouldn't have figured that out myself! But, what would they be for sidesteping and backwards?
Title: Re: Using pitch and yaw with movement?
Post by: Cornix on July 28, 2013, 11:05:36
To the side: +/- 90°
Backwards: +/- 180°

Assuming you have the angle (in degree) between your current position and the forward position you can just calculate the angle with the above arithmetics.
Title: Re: Using pitch and yaw with movement?
Post by: TechNick6425 on July 28, 2013, 11:10:03
Thanks!
Title: Re: Using pitch and yaw with movement?
Post by: TechNick6425 on August 25, 2013, 23:03:56
Well, I'm now working on an FPS, and I want to be able to throw grenades, but I need to find a solution for the Y velocity based on the pitch. How would I modify these to work?
Title: Re: Using pitch and yaw with movement?
Post by: quew8 on August 26, 2013, 20:27:09
With pitch as well, it's slightly more complicated.

    dx = cos(pitch) * sin(yaw)
    dy = sin(pitch)
    dz = cos(pitch) * cos(yaw)
Title: Re: Using pitch and yaw with movement?
Post by: Turbots on September 09, 2013, 14:59:14
This is what I have at the moment:


switch (direction) {
case BACKWARDS_OR_FORWARDS:
position.z += amount * Math.sin(Math.toRadians(ry + 90));
position.x += amount * Math.cos(Math.toRadians(ry + 90));
position.y += amount * Math.sin(Math.toRadians(rx));
break;
case SIDEWAYS:
position.z += amount * Math.sin(Math.toRadians(ry));
position.x += amount * Math.cos(Math.toRadians(ry));
break;
case UP_OR_DOWN:
// TODO: implement
default:
break;
}
Title: Re: Using pitch and yaw with movement?
Post by: quew8 on September 09, 2013, 16:16:21
Is that another question or a reply? Because the topic has already been answered (presumable since TechNick6425 hasn't replied in over a fortnight)
Title: Re: Using pitch and yaw with movement?
Post by: TechNick6425 on March 08, 2014, 18:28:05
Sorry for taking so long, but how would you reverse this formula? Say, pointing towards the coordinates of the mouse pointer.
Title: Re: Using pitch and yaw with movement?
Post by: quew8 on March 08, 2014, 19:19:33
You can take this formua

dy = sin(pitch)

and rearrange it to

pitch = arcsin(dy)

Then when you know pitch you can rearrange the other formulas to:

yaw = arcsin(dx / cos(pitch))

Giving you pitch and yaw. Hope this helps.