gpt4 book ai didi

java - 从 2D Joystick 获取 3D 旋转轴和角度

转载 作者:行者123 更新时间:2023-11-29 08:01:42 26 4
gpt4 key购买 nike

我在屏幕左下角有一个 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());
}

Joystick 类中我当前的 getAxis() 方法:

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

最佳答案

我只会使用 3 个 vector 来定义相机:位置、向前、向上(右 = 交叉(向前,向上))。然后对于您的情况,您总是在寻找 (0,0,0),所以只需像这样在输入上更新这 3 个 vector :

上/下:

right = cross(forward, up);
posititon = normalized(position + up*(input*zoomFactor)) * length(position); //zoomFactor might be useful if you are looking from close to reduce the move but is optional
forward = normalized((0,0,0)-position);
up = cross(right, forward);//should already be normalized Note: might be cross(forward, right)

向左/向右:

right = cross(forward, up);
position = normalized(position + right*(input*zoomFactor)) * length(position);
forward = normalized((0,0,0)-position);
right = cross(forward, up); //you do need to update it
up = cross(right, forward); //Note: might be cross(forward, right)

向左/向右倾斜:

up = normalize(up + right*input); //no zoom factor needed here

放大/缩小:

position = position + forward*input;//in your case you might just want to set it to some percentage: position = position + forward*(length(position) * (input));

这种方法应该只适用于通常情况下的小输入值。相对于输入的角度变化将是 alpha = atan(input) 所以如果输入是无穷大角度将改变 90 度。但是你可以很容易地保存/加载状态,你可以手动设置相机位置和伴随 vector 。因此,您拥有调用“lookAt”所需的一切。

关于java - 从 2D Joystick 获取 3D 旋转轴和角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com