2
\$\begingroup\$

I've been hitting my head on the wall for a while with this, and I'm almost sure I'm overcomplicating it.

I have a variable for yaw in degrees (and also pitch and roll, but those should be irrelevant). I want to turn that variable into normalized 2D movement, so the distance traveled per frame should always be a constant.

I was easily able to to workout movement when I had a fixed view direction, but translating it with yaw is what's confusing me. I've been trying a combination of converting to radians, then using cos(x) and sin(x) but it only works if I'm looking in a certain direction.

\$\endgroup\$
1
\$\begingroup\$

Yep, was definitely overthinking it. Here's the quick version, probably some ways to optimize it:

        double x = Math.sin(Math.toRadians(yaw));
        double z = Math.cos(Math.toRadians(yaw));
        double xo = Math.sin(Math.toRadians(yaw + 90)); // 90 offset of current direction
        double zo = Math.cos(Math.toRadians(yaw + 90));

        if(Keyboard.isKeyDown(Keyboard.KEY_W)) {
            position.z -= MOVESPEED * z;
            position.x += MOVESPEED * x;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
            position.z -= MOVESPEED * zo;
            position.x += MOVESPEED * xo;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
            position.z += MOVESPEED * zo;
            position.x -= MOVESPEED * xo;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_S)) {
            position.z += MOVESPEED * z;
            position.x -= MOVESPEED * x;
        }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.