How to calculate the direction vector from the yaw angle?

I have a problem where I do not know how to continue calculating the direction vector using Java / LWJGL for rendering in OpenGL.

I have the following system:

  • + X goes to the right of the screen
  • + Z goes to my screen
  • + Y goes to the top of the screen (height)

So I am walking on the XZ plane, now I want to implement / implement the WASD movement, and this should be related to the current direction I am going. (W = forward direction towards camera, S = back, etc.)

I have a yaw angle, which is defined as follows:

  • 0 degrees if you go straight ahead
  • 90 degrees if you go right
  • 180 degrees when turning
  • 270 degrees if you go left

Now I just want a three-dimensional vector representing the yaw direction, how would I do it?

Java- , , :

@Override
protected void mouseMoved(final int dx, final int dy) {
    float yawDelta = dx / 10f;
    float pitchDelta = dy / 10f;
    yaw += yawDelta;
    pitch += pitchDelta;
    System.out.println("yaw = " + yaw);
    direction.updateZero().updateTranslate((float)Math.sin(Math.toRadians(yaw)), 0f, (float)Math.cos(Math.toRadians(yaw))).updateNormalized();
    System.out.println("direction = " + direction);
    updateView();
}

private void checkKeys() {
    if (isKeyCurrentlyDown(Keyboard.KEY_W)) {
        eye.updateTranslate(direction);
        updateView();
    }
    if (isKeyCurrentlyDown(Keyboard.KEY_S)) {
        eye.updateTranslate(direction.negated());
        updateView();
    }
    if (isKeyCurrentlyDown(Keyboard.KEY_A)) {
        eye.updateTranslate(direction.cross(Vector3f.Y.negated()));
        updateView();
    }
    if (isKeyCurrentlyDown(Keyboard.KEY_D)) {
        eye.updateTranslate(direction.cross(Vector3f.Y));
        updateView();
    }
    if (isKeyCurrentlyDown(Keyboard.KEY_Q)) {
        eye.updateTranslate(0.0f, -1.0f, 0.0f);
        updateView();
    }
    if (isKeyCurrentlyDown(Keyboard.KEY_Z)) {
        eye.updateTranslate(0.0f, 1.0f, 0.0f);
        updateView();
    }
}

private void updateView() {
    viewMatrix.identity().fpsView(eye, roll, yaw, pitch);
    Uniforms.setUniformMatrix4(UNIFORM_VIEW_MATRIX, false, viewMatrix);
}

public Matrix4f fpsView(final Vector3f eye, final float rollAngle, final float yawAngle, final float pitchAngle) {
    //roll = rolling your head, Q&E
    //yaw = looking left/right, mouseY
    //pitch = looking up/down, mouseX
    float sinRoll = (float)Math.sin(Math.toRadians(rollAngle));
    float cosRoll = (float)Math.cos(Math.toRadians(rollAngle));
    float sinYaw = (float)Math.sin(Math.toRadians(yawAngle));
    float cosYaw = (float)Math.cos(Math.toRadians(yawAngle));
    float sinPitch = (float)Math.sin(Math.toRadians(pitchAngle));
    float cosPitch = (float)Math.cos(Math.toRadians(pitchAngle));

    //TODO cannot roll yet
    Vector3f xAxis = new Vector3f(
        cosYaw,
        -sinPitch * sinYaw,
        -cosPitch * sinYaw
    );
    Vector3f yAxis = new Vector3f(
        0.0f,
        cosPitch,
        -sinPitch
    );
    Vector3f zAxis = new Vector3f(
        sinYaw,
        sinPitch * cosYaw,
        cosPitch * cosYaw
    );

    return multiply(
        xAxis.getX(),               xAxis.getY(),               xAxis.getZ(),               0.0f,   //X column
        yAxis.getX(),               yAxis.getY(),               yAxis.getZ(),               0.0f,   //Y column  
        zAxis.getX(),               zAxis.getY(),               zAxis.getZ(),               0.0f,   //Z column
        0.0f,                       0.0f,                       0.0f,                       1.0f    //W column
    ).translate(eye);
}

- eye.updateTranslate(), eye. - ?

+4
2

, .

 R = yawMat.pitchMat.rollMat 

:

    yawMat={ { cosZ, -sinZ, 0 }, { sinZ, cosZ , 0 }, {0,0,1 } };

    pitchMat =  { { cosY , 0 , sinY }, { 0, 1 , 0 }, { -sinY, 0, cosY } };

    rollMat = { {1,0,0 }, {0,cosX,-sinX }, {0,sinX,cosX } };

, 3x3 R . , .

- .

4x4

T = {{R00,R01,R02,X},{R10,R11,R12,Y},{R20,R21,R22,Z},{0,0,0,1}}

, 3x3 , , , :

R = yawMat.I.I = yawMat

:

R = { { cosZ, -sinZ, 0 }, { sinZ, cosZ , 0 }, {0,0,1 } }

.

, , :

Vector3f xAxis = new Vector3f(
    cosYaw*cosPitch,
    cosYaw* sinPitch*sinRoll - sinYaw*cosRoll,
    cosYaw*sinPitch*cosRoll + sinYaw*sinRoll
);

Vector3f yAxis = new Vector3f(
    sinYaw*cosPitch,
    sinYaw*sinPitch*sinRoll + cosYaw*cosRoll,
    sinYaw*sinPitch*cosRoll - cosYaw*sinRoll
);
Vector3f zAxis = new Vector3f(
    -sinPitch,
    cosPitch*sinRoll,
    cosPitch * cosYaw
);

x → y → z, X, Y, Z

+1

y 0

x = sin (yaw)
z = cos (yaw)

+1

All Articles