0

I have a camera that has X, Y and Z Coordinates.

The camera also has a Yaw and a pitch.

int cameraX = Camera.getX();
int cameraY = Camera.getY();
int cameraZ = Camera.getZ();
int cameraYaw = Camera.getYaw();
int cameraPitch = Camera.getPitch();

The yaw has 2048 units in 360 degrees, so at 160 degrees the getYaw() method will return 1024.

Currently I move the camera forward by just setting the Y + 1 in each loop.

Camera.setY(Camera.getY() + 1);

How would I set the camera X and Y to the direction I'm facing (The Yaw)? I don't want to use the pitch in this situation, just the Yaw.

Frunk
  • 180
  • 1
  • 2
  • 11
  • matrix maths is generally the way it is done, rotation can also be done with quaternions. you want look at creating a lookAt function. take a look at the answer to this http://stackoverflow.com/questions/19740463/lookat-function-im-going-crazy – DanielCollier Apr 16 '17 at 17:52
  • The problem isn't changing the camera rotation. I just need to go to the direction the camera is facing. – Frunk Apr 16 '17 at 17:55
  • this involves rotation, you have to rotate the forward and up vectors. then you move along the forward vector – DanielCollier Apr 16 '17 at 17:59

1 Answers1

2

If I understand your question correctly, you're trying to get the camera to move in the direction you're looking (in 2D space, you're only moving horizontally).

I made a small LookAt header-only library for C++, but here is part of it rewritten in Java. What this code does is it takes a rotation and a distance, then calculates how far you need to move (in both the x and y coordinate) to get there.

// Returns how far you need to move in X and Y to get to where you're looking
// Rotation is in degrees, distance is how far you want to move
public static double PolarToCartesianX(double rotation, double distance) {
    return distance * Math.cos(rotation * (Math.PI / 180.0D));
}

public static double PolarToCartesianY(double rotation, double distance) {
    return distance * Math.sin(rotation * (Math.PI / 180.0D));
}
Nybbit
  • 293
  • 2
  • 10