I’m trying to create a RTS style camera where the camera is always looking at a point on the map and also rotates around this point.
If I were to implement this in Unity (which I have some experience with), I would place the camera as a child to some GameObject with the correct distance, and then translate/rotate that GameObject, resulting in the camera moving as well.
So I’m trying to mimick this behaviour here with MonoGame.
I’m positioning my camera using the targets transform (matrix) and an offset vector, which specifies the distance of the camera should follow the target.
var newCameraPosition = target.Position - (target.Forward * offset.Z) + (target.Up * offset.Y) + (target.Right * offset.X); camera.Position = newPosition;
So in my mind, I should now be able to rotate the target and the camera should always keep the correct distance.
I want the camera to be rotatable by moving the mouse. When the mouse is moved horizontally, the camera should rotate around the Y-axis. When the mouse is moved vertically, the camera should rotate around the X-axis.
When rotating around a single axis, it works as expected.
target.Rotation *= Quaternion.CreateFromAxisAngle(target.Up, mouseDelta.X);
But as soon as I introduce the second axis I get weird behaviour. When I move the mouse horizontally the camera rotates around the Y-axis, but also “bobs” up and down around the X-axis. And vice versa when moving the mouse vertically.
target.Rotation *= ( Quaternion.CreateFromAxisAngle(target.Up, mouseDelta.X) * Quaternion.CreateFromAxisAngle(target.Right, mouseDelta.Y) );
I have also tried using
Quaternion.CreateFromYawPitchRoll with identical results.
I’m using the transform class from MonoGame.Extended (github)
I’m probably doing something weird or not working with Quaternions correctly.
Any obvious mistakes?