我在左下角的屏幕上有一个 2D 虚拟操纵杆。我在原点 (0, 0, 0) 处绘制了一个 3D 球体……我的相机可以绕球体旋转。我正在尝试使用操纵杆移动相机,但不知道该怎么做。我需要从我的操纵杆创建一个轴角旋转并更新使用四元数表示其方向的摄像机角度。这是我目前拥有的:
我的相机旋转存储为四元数:
// The current rotation
public Quaternion rotation = new Quaternion();
// The temp quaternion for the new rotation
private Quaternion newRotation = new Quaternion();
旋转和相机通过以下方法更新:
// Update the camera using the current rotation
public void update(boolean updateFrustum)
{
float aspect = camera.viewportWidth / camera.viewportHeight;
camera.projection.setToProjection(Math.abs(camera.near), Math.abs(camera.far), camera.fieldOfView, aspect);
camera.view.setToLookAt(camera.position, tmp.set(camera.position).add(camera.direction), camera.up);
// Rotate the current view matrix using our rotation quaternion
camera.view.rotate(rotation);
camera.combined.set(camera.projection);
Matrix4.mul(camera.combined.val, camera.view.val);
if (updateFrustum)
{
camera.invProjectionView.set(camera.combined);
Matrix4.inv(camera.invProjectionView.val);
camera.frustum.update(camera.invProjectionView);
}
}
public void updateRotation(float axisX, float axisY, float axisZ, float speed)
{
// Update rotation quaternion
newRotation.setFromAxis(Vector3.tmp.set(axisX, axisY, axisZ), ROTATION_SPEED * speed * MathHelper.PIOVER180);
rotation.mul(newRotation);
// Update the camera
update(true);
}
我目前updateRotation()
这样打电话:
// Move the camera
if (joystick.isTouched)
{
// Here is where I'm having trouble...
// Get the current axis of the joystick to rotate around
tmpAxis = joystick.getAxis();
axisX = tmpAxis.X;
axisY = tmpAxis.Y;
// Update the camera with the new axis-angle of rotation
// joystick.getSpeed() is just calculating the distance from
// the start point to current position of the joystick so that the
// rotation will be slower when closer to where it started and faster
// as it moves toward its max bounds
controller.updateRotation(axisX, axisY, axisZ, joystick.getSpeed());
}
我目前getAxis()
在Joystick
课堂上的方法:
public Vector2d getAxis()
{
Vector2d axis = new Vector2d(0.0f, 0.0f);
float xOffset = 0;
float yOffset = 0;
float angle = getAngle();
// Determine x offset
xOffset = 1f;
// Determine y offset
yOffset = 1f;
// Determine positive or negative x offset
if (angle > 270 || angle < 90)
{
// Upper left quadrant
axis.X = xOffset;
}
else
{
axis.X = -xOffset;
}
// Determine positive or negative y offset
if (angle > 180 && angle < 360)
{
// Upper left quadrant
axis.Y = yOffset;
}
else
{
axis.Y = -yOffset;
}
return axis;
}