开发者

3D Camera looking at arbitrary point, but never rolling to do so

开发者 https://www.devze.com 2023-02-01 07:44 出处:网络
(In C#/XNA 4.0) I have Vector3 cameraPosition, Vector3 targetPosition I want to have my camera look at the target, always \'facing\' it, but never rolling

(In C#/XNA 4.0)

I have Vector3 cameraPosition, Vector3 targetPosition

I want to have my camera look at the target, always 'facing' it, but never rolling

So roll always is neutral, to view the target the camera always either adjusts pitch or yaw

I've tried countless combinations of methods and information I find here and on the web but I haven't found anything that works properly. I think my issue may be my 'Up' vector (which I've tried .Up, 1,0,0, 0,1,0, 0,0,1)

When I move my camera I do:

  CameraPosition += moveSpeed * vectorToAdd;
  UpdateViewMatrix();

UpdateViewMatrix() is.. well, I've tried everything I have seen. At most simple...

   View = Matrix.CreateLookAt(CameraPosition, targetPosition, upVector);

Where upVector has been Vector3.Up, 1, 0, 0; 0, 1, 0; 0, 0, 1, or other more 'proper' attempts to ge开发者_运维技巧t my actual up vector. This sounds like it's probably my problem..

This should be dead simple, help!


Ends up I was confusing myself somewhere about what things should look like and what I was actually considering "up" in my worldspace. I had to use up vector of (0, 0, 1) and things worked great


Assuming a Y-up world space, absence of roll means that camera side vector has Y = 0. You have to get your view direction vector (normalize(targetPos - cameraPos)), and get such side vector that it's orthogonal to view and has Y = 0, in other words view.x * side.x + view.z * side.z = 0, view.y = 0. This is a simple 2d perpendicular, side = normalize(vector3(-view.z, 0, view.x)).

After you have the side vector, you can get up = cross(view, side) and construct the view matrix.

Depending on the handedness of your coordinate system, you may need to negate the side vector.

Note that there's a singularity at view.y = +-1 (side is a zero-length vector); this is because the roll is not defined for this pitch angle - any roll value gives the same transformation.

0

精彩评论

暂无评论...
验证码 换一张
取 消