# How do i rotate my view on its own center

PROBLEM

I am trying to translate and rotate my view, the translation works fine but when rotating the rotation is happening on the center of the world(0,0,0) instead of the view position.

EXAMPLE

What is happening:

What i need to happen:

CODE

This is the code i use

    Matrix Rotation = Matrix.RotationX(gameObject.transform.Rotation.X) *
Matrix.RotationY(gameObject.transform.Rotation.Y) *
Matrix.RotationZ(gameObject.transform.Rotation.Z);

Vector3 Target = Vector3.UnitZ;

Target += gameObject.transform.Position;

Matrix Translation = LookAtLH(gameObject.transform.Position, Target, Vector3.UnitY);

View = Matrix.Multiply(Rotation, Translation);


i followed directxtutorial.com to create this code What should i do to rotate on the center of my view

• You probably jusy need to swap translate and rotate. Translate * rotate != Rotate * translate – Andrew Wilson May 30 '18 at 20:47
• @AndrewWilson I know that but if i do that the movement bugs for example if i do x++ instead of going forward where i am looking at it will go forward according to the world like it is explained in the website i liked – pekira May 30 '18 at 21:17

I tend to use Polar Coordinates. This means I tend to only mess with the Theta (in your case) or the Phi for any controls regarding looking (and is very intuitive for me) and means that any controls for panning just take the angles into account.

Here's part of my camera's implementation below:

 public void HandleKeyboardInput()
{
// TODO: Mark that the camera pos or target changed when it does

bool w = Keyboard.GetState().IsKeyDown(Keys.W);
bool a = Keyboard.GetState().IsKeyDown(Keys.A);
bool s = Keyboard.GetState().IsKeyDown(Keys.S);
bool d = Keyboard.GetState().IsKeyDown(Keys.D);

if (!(w || a || s || d))
{
return;
}

updateCamera = true;
bool shift = Keyboard.GetState().IsKeyDown(Keys.LeftShift);

// camera panning
if (!shift)
{
if (w)
{
pos.X += (float)Math.Sin(theta);
pos.Z += (float)Math.Cos(theta);
}
if (s)
{
pos.X -= (float)Math.Sin(theta);
pos.Z -= (float)Math.Cos(theta);
}

if (a)
{
pos.X += (float)Math.Cos(theta);
pos.Z -= (float)Math.Sin(theta);
}
if (d)
{
pos.X -= (float)Math.Cos(theta);
pos.Z += (float)Math.Sin(theta);
}
}
// camera twisting
else
{
if (w && phi < -Utilities.thirdPi)
{
phi += (float)Math.PI / 180;
}
if (s && phi > Utilities.halfPi * -0.97)
{
phi -= (float)Math.PI / 180;
}

if (a)
{
theta += (float)Math.PI / 180;
if (theta > Math.PI * 2)
{
theta = 0;
}
}
if (d)
{
theta -= (float)Math.PI / 180;
if (theta < 0)
{
theta = (float)Math.PI * 2;
}
}
}

//Target is based on Position, that way they are always synchronus
tar.X = pos.X + radius * (float)Math.Sin(theta) * (float)Math.Cos(phi);
tar.Z = pos.Z + radius * (float)Math.Cos(theta) * (float)Math.Cos(phi);
tar.Y = pos.Y + radius * (float)Math.Sin(phi);
}

const float FIELD_OF_VIEW = Utilities.halfPi;
public Matrix BuildProjection()
{
return Matrix.CreatePerspectiveFieldOfView(
FIELD_OF_VIEW,
gd.DisplayMode.AspectRatio,
1f, 500f);
}

public Matrix BuildView()
{
return Matrix.CreateLookAt(pos, tar, new Vector3(0f, 1f, 0f));
}

public Matrix BuildWorld()
{
return Matrix.Identity;// CreateWorld(tar, Vector3.Forward, Vector3.Up);
}


I use similar code for twisting models, pointing models at things, etc. Let me know if I can make this answer more helpful to you.

• thank you a lot for your answer but my problem is simply that the camera is rotating on center of the world instead of its own center so your answer doesn't really help. – pekira May 31 '18 at 14:32
• If you specifically look at how I adjust the target of the camera based on the position of the camera, I think you may change your mind. Note: If you were to apply similar tracking of Theta in your code before the line "LookAtLH(gameObject.transform.Position, Target, Vector3.UnitY);" you should have a fix. I pasted much more code so you could see the context of it, in case you had questions. – blurry May 31 '18 at 14:53
• how do i know the value of radius? – pekira May 31 '18 at 15:00
• Apologies that I didn't give that context. "Radius" is defined in my code as the distance between Pos and Tar; and I adjusted Radius as a way to "Zooming in/out." In your case, you don't need anything regarding Phi I don't think (until you're changing it.) NOTE: In my code my "ground" plane is the XY plane, and my "depth" is on the Z axis. – blurry May 31 '18 at 15:04
• I tried theta but didn't work. – pekira May 31 '18 at 15:26