我在屏幕左下角有一個2D虛擬操縱桿。我有一個在原點(0,0,0)以3D繪製的球體......我的相機可以圍繞球體運行。我正在嘗試使用操縱桿移動相機,但不知道如何才能做到這一點。我需要從操縱桿創建一個軸角旋轉,並更新使用四元數表示其方向的攝像機角度。以下是我目前有:從2D操縱桿獲取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()
遇到HOD從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;
}
感謝您的幫助,誰知道這將是如此簡單哈哈......我知道我很近就需要使用'setEulerAngles()'和'leftMul()'並修改我的'getAxis()'方法在「操縱桿」課堂上,瞧! – DRiFTy