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?
Forward Direction:
dx = sin(yaw)
dz = cos(yaw)
Thanks! I wouldn't have figured that out myself! But, what would they be for sidesteping and backwards?
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.
Thanks!
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?
With pitch as well, it's slightly more complicated.
dx = cos(pitch) * sin(yaw)
dy = sin(pitch)
dz = cos(pitch) * cos(yaw)
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;
}
Is that another question or a reply? Because the topic has already been answered (presumable since TechNick6425 hasn't replied in over a fortnight)
Sorry for taking so long, but how would you reverse this formula? Say, pointing towards the coordinates of the mouse pointer.
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.