rku
October 3, 2017, 2:04pm
#1
I have a mesh at the (0, 0, 0)
and camera further away looking At that point. I want to rotate camera around the mesh, however it does not always work as expected. When camera is facing mesh from the front moving mouse up/down on X axis rotates around the mesh as expected:
However if i look at mesh from the side then moving mouse up/down on X axis rotates around mesh as if rotation was in mesh local space:
This is how i attempt rotation:
camera_->GetNode()->RotateAround(Vector3::ZERO, Quaternion(
input->GetMouseMoveY() * 0.1f * lookSensitivity_,
input->GetMouseMoveX() * 0.1f * lookSensitivity_, 0
), TS_WORLD);
Any idea how i could make it rotate so that moving mouse on X axis would make camera go up/down same way like it works when looking at model from the front?
stark7
October 3, 2017, 2:24pm
#2
Hey here is something that I started coding just last night for my own purposes, sorry it’s in C#:
class CameraOrbitComponent : Component
{
public float _verticalTheta { get; set; } = 0f;
public float _horizontalTheta { get; set; } = 0f;
public Node _targetNode { get; set; }
public Node _cameraNode { get; set; }
public CameraOrbitComponent()
{
ReceiveSceneUpdates = true;
}
protected override void OnUpdate(float timeStep)
{
Vector3 targetPos = _targetNode.WorldPosition;
Vector3 heading = targetPos - _cameraNode.WorldPosition;
float radius = heading.Length;
float moveSpeed = 10.0f;
if (this.Application.Input.GetKeyDown(Key.A)) _horizontalTheta -= moveSpeed;
if (this.Application.Input.GetKeyDown(Key.D)) _horizontalTheta += moveSpeed;
_cameraNode.LookAt(_targetNode.Position, Vector3.Up);
_cameraNode.Rotation = new Quaternion(_cameraNode.Rotation.PitchAngle, _cameraNode.Rotation.YawAngle, 0f);
_cameraNode.Position = new Vector3(radius * (float)Math.Cos(_horizontalTheta), _cameraNode.Position.Y, radius * (float)Math.Sin(_horizontalTheta));
}
}
1 Like
I use RotateAround for Quatter’s camera with pre-multiplied Quaternions:
node_->RotateAround(targetPosition_,
Quaternion(rotation.x_, Vector3::UP) * Quaternion(rotation.y_, node_->GetRight()),
TS_WORLD);
2 Likes
stark7
October 3, 2017, 3:08pm
#4
Your solution is so much better - I will be stea… umm, learn from it thank you very much.
rku
October 3, 2017, 4:25pm
#5
@Modanung that has issue similar to gimbal lock. Besides what exactly is a rotation
? A delta rotation? Absolute rotation of some kind?
Eugene
October 3, 2017, 4:29pm
#6
I suppose, yaw&pitch angles. And nobody usualy cares about gimbal lock for cameras unless you truly need three degrees of freedom.
rku
October 3, 2017, 4:41pm
#7
I am doing model viewer app. It gets really awkward when trying to look at the head of character if there is gimbal lock.
@Modanung thanks for that snipped, it led to a successful solution:
camera_->GetNode()->RotateAround(Vector3::ZERO,
Quaternion(input->GetMouseMoveX() * lookSensitivity_, camera_->GetNode()->GetUp()) *
Quaternion(input->GetMouseMoveY() * lookSensitivity_, camera_->GetNode()->GetRight()),
TS_WORLD
);
2 Likes