Answers for "Trying to make a freelook camera"
http://answers.xenko.com/questions/373/trying-to-make-a-freelook-camera.html
The latest answers for the question "Trying to make a freelook camera"Answer by jwollen
http://answers.xenko.com/answers/379/view.html
First of all, X is right, Y is up and consequently Z is backwards. The reason being, that a view matrix transforms into the XY-plane (which is pointing right and up from your point of view). So you want to **subtract from Z when going forward** and **add to X when going right**.
Vector3 localTranslation = new Vector3();
if (Input.IsKeyDown(Keys.W))
localTranslation.Z -= speed;
if (Input.IsKeyDown(Keys.A))
localTranslation.X -= speed;
if (Input.IsKeyDown(Keys.S))
localTranslation.Z += speed;
if (Input.IsKeyDown(Keys.D))
localTranslation.X += speed;
Also, you add those movements to the camera entity's global position, when you really want to move around in view-space.
CameraComponent cameraComponent = ...;
TransformationComponent transformation = cameraComponent.Entity.Transformation;
Matrix view, projection;
cameraComponent.Calculate(out projection, out view);
Matrix cameraTransform = view;
cameraTransform.Invert();
Vector3 globalTranslation = Vector3.TransformNormal(localTranslation, cameraTransform);
transformation.Translation += globalTranslation;
For the rotation, you experience 'squeezing' because you add those quaternions. That's not chaining rotations. Quaternions are meant to be multiplied. Also, just as with the translation, you want to rotate in the camera's local space. (Also note, that a positive rotation around X turns the camera up, so you probably want to negate it, too. MousePosition.Y is 0 at the top of the window and 1 at the bottom).
Quaternion localRotation = Quaternion.RotationX(-RelMousePos.Y * Sensitivity) * Quaternion.RotationY(-RelMousePos.X * Sensitivity);
transformation.Rotation = transformation.Rotation * localRotation; // But see next paragraph!
Right now the CameraComponent seems to misbehave a bit. It will always look to the left of the entity. That's why you will need to replace the last line with the following:
Quaternion applyQuirkyRotation = Quaternion.RotationY(MathUtil.PiOverTwo);
Quaternion undoQuirkyRotation = Quaternion.RotationY(-MathUtil.PiOverTwo);
transformation.Rotation = undoQuirkyRotation * localRotation * applyQuirkyRotation * transformation.Rotation;
Finally, the CameraComponent can use one of 3 ways to determine the view direction. (1) Relative to the entity. (2) Looking towards a Target, or (3) with an explicit view matrix. When you design your camera controller you should probably check which one applies.Sat, 04 Oct 2014 18:21:31 GMTjwollen