Frunk
Frunk

Reputation: 180

Move camera to the direction it's facing

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.

Upvotes: 0

Views: 191

Answers (1)

Nybbit
Nybbit

Reputation: 293

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

Upvotes: 2

Related Questions