Using pitch and yaw with movement?

Started by TechNick6425, July 27, 2013, 19:41:50

Previous topic - Next topic

TechNick6425

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?

quew8

Forward Direction:
        dx = sin(yaw)
        dz = cos(yaw)

TechNick6425

Thanks! I wouldn't have figured that out myself! But, what would they be for sidesteping and backwards?

Cornix

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.


TechNick6425

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?

quew8

With pitch as well, it's slightly more complicated.

    dx = cos(pitch) * sin(yaw)
    dy = sin(pitch)
    dz = cos(pitch) * cos(yaw)

Turbots

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;
}

quew8

Is that another question or a reply? Because the topic has already been answered (presumable since TechNick6425 hasn't replied in over a fortnight)

TechNick6425

Sorry for taking so long, but how would you reverse this formula? Say, pointing towards the coordinates of the mouse pointer.

quew8

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.