X-axis of cameraPosition works fine, but Z-axis keeps decreasing which makes camera keeps approaching the target. Even if I try to rotate camera back, the camera is still approaching the target. I tired to search some solution but only get similar code as mine.
Update: cameraDirection.Normalize() does not work, so I do not use cameraDirection*distance to get cameraPosition. How to use Normalize()?
Here’s my Camera class, might be worth taking a look at to see how what you’re doing differs. OrbitAround(Vector3 orbitPt, float x, float y) is where you’d start looking.
If I were doing it over again I’d just setup a quaternion and rotate the position-orbitPt for the new position and recalculate the direction from that rather than the yaw and pitch nonsense.
Update: cameraDirection.Normalize() does not work, so I do not use cameraDirection*distance to get cameraPosition. How to use Normalize()?
It works, you’re probably using Matrix.CreateLookAt incorrectly. It looks at a target point, not along a vector, so it has to be cameraPos + cameraDirection for the cameraTarget.
The matrix and quaternion calculations produce precision errors over time, these are typically rounding down errors so it will wind your position down.
You need to get the distance at the time you order the rotation. Like when you issue the order be it programatically or keypress. You save that distance ( Only ) at that time and keep reusing it later.
// class level declaration
float desiredOrbitalDistance = 1f;
position_ = orbitPt - direction_ * (orbitPt - position_).Length();
I assume this part equals to: cameraPosition = target - cameraDirection*distance, but cameraPosition.Z is still decreasing no matter which direction I rotate.
Then I do this:
int ang = Mouse.GetState().X - prevMouseState.X;
currentCameraDirection = Vector3.TransformNormal
(cameraDirection, Matrix.CreateFromAxisAngle(actor.world.Up, (-MathHelper.PiOver4 / 900) * ang));
I get vector of currentCameraDirection - previous cameraDirection, then use position to minus this vector*distance, so I can get the new position, and create view matrix based on the new position. The problem of Z-axis still exist…
I have tried to get new distance. Although the distance works fine now, the camera keeps flashing back and forward.
The following is part of position output:
well here i wrote this out really quick so you can get the gist of it as i don’t really have time to make a full example.
// c camera position t target position p transformed position of the camera
// dist is the distance to maintain from the target
// rotation in this case ranges from 0 to 1f
float rotation = .9f;
Vector3 c = new Vector3(2.0f, 0f, 0f);
Vector3 t = new Vector3(7.0f, 0f, 0f);
Vector3 t_forward = new Vector3(0.0f, 1f, 0f);
//float dist = Vector3.Distance(t, c);
float dist = 12f;
Matrix m = Matrix.CreateFromAxisAngle(t_forward, (float)(Math.PI * 2f) * rotation);
Vector3 p = Vector3.Normalize(c - t) * dist;
p = Vector3.Transform(p, m);
Console.WriteLine("dist: " + dist + " c: " + c + " p will be the new c distance is stored p: " + p);
// output
//dist: 12 c: { X: 2 Y: 0 Z: 0}
//p will be the new c, distance is stored, p: { X: -9.708203 Y: 0 Z: -7.053424}
Thanks for your help. Your code and my test code of a single rotation output the correct position, while the position outputs are not correct once I update and rotate the camera. That is the what I am confused with. What I did is simply add 0.01 to angle every tick, but get a wired result.
I assume it is not a mouse nor math problem, so I will try to write my own update method instead of overriding Update() method.