Gemma Lamont
Gemma Lamont

Reputation: 71

LibGDX Camera rotation Issue

I am trying to rotate my perspective camera around my model. The model is at the centre (0,0,0) point. This is my rotate camera method:

private void rotateCameraAroundModel() {
    camera.position.set(0,0,0);
    camera.position.rotate(Vector3.Y, 5);
    camera.position.add(0f, 0f, 200f);
    camera.up.set(Vector3.Y);
    camera.lookAt(0,0,0);
    camera.update();
}

I am trying to go to the centre, rotate by 5 degrees, then return to the same distance away. However, the rotate doesn't seem to be working and I can't figure out why, any help is greatly appreciated.

Upvotes: 1

Views: 452

Answers (1)

Gemma Lamont
Gemma Lamont

Reputation: 71

I figured it out :)

private void rotateCameraAroundModel(float angle) {
    camera.position.set(centreX, centerY, centerZ);
    camera.rotate(Vector3.Y, (float) Math.toDegrees(angle)); // Rotate around Y axis by angle in degrees
    float x = (float) (centerX + radius * (Math.sin(totalPhi)));
    float z =  (float) (centerZ + radius * (Math.cos(totalPhi)));
    camera.position.add(x, 0f, z); //Move back out 2m using pythagorean theorem to calculate the position on the circle
    camera.up.set(Vector3.Y);
    camera.lookAt(0, 0, 0);
    camera.update();
}

So I needed to calculate the new position on the circle whilst rotating, which I did using some basic trigonometry

Upvotes: 1

Related Questions