简体   繁体   English

Libgdx相机使用EulerAngles在中心点上旋转

[英]Libgdx camera rotate around on a center point with EulerAngles

Here is my code: 这是我的代码:

private Quaternion getRotatedQuaternion(float pitch, float yaw, float roll) {
    tempQuat.setEulerAngles(pitch, yaw, roll);
    rotationQuat.mulLeft(tempQuat);

    return rotationQuat;
}

and

            camera.view.setToLookAt(tempPos, tempLookat, Axis.UP);

            Quaternion rotQuat = getRotatedQuaternion(gestureListener.getXAngle(), gestureListener.getYAngle(), 0);
            camera.view.rotate(rotQuat);
            camera.combined.set(camera.projection);

            Matrix4.mul(camera.combined.val, camera.view.val);

This is a camera rotation in the center, on 0,0,0. 这是摄像机在中心0,0,0上的旋转。 Gimbal lock probleme solved by this way. 通过这种方式解决了万向节锁问题。 But how can I add a new center point to it? 但是如何为它添加新的中心点?

Basically I want to rotate the camera around my object, not 0,0,0 基本上我想绕我的物体旋转相机,而不是0,0,0

Thanks 谢谢

I think the only way you could do that is to first, move the camera to the object's position, rotate, and move the camera back to the original spot. 我认为您唯一的方法是首先将相机移至对象的位置,旋转,然后将相机移回原始位置。

So something like: 所以像这样:

Camera.setposition(object.pos)
Camera.rotate()
Camera.setposition(original-camera-pos)

You need to convert from Polar Coordinates (r,θ) to Cartesian Coordinates (x,y) the formula is the following: 您需要将极坐标(r,θ)转换为笛卡尔坐标(x,y)的公式如下:

x = r × cos( θ ) x = r×cos(θ)

y = r × sin( θ ) y = r×sin(θ)

The angle is relative to your object central point (0º to 360º), the radius is the distance from your object to your camera. 角度是相对于对象中心点(0º至360º),半径是从对象到相机的距离。

Here's a simple method that does that for you: 这是为您执行此操作的简单方法:

public static Vector3 returnPosArroundObj(Vector3 posObject, Float angleDegrees, Float radius, Float height) {
    Float angleRadians = angleDegrees * MathUtils.degreesToRadians;
    Vector3 position = new Vector3();
    position.set(radius * MathUtils.sin(angleRadians), height, radius * MathUtils.cos(angleRadians));
    position.add(posObject); //add the position so it would be arround object
    return position;
}

我最后进行了两次Matrox旋转和一个矢量来用作Arcball旋转

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM