# DX11 C# SharpDX how to rotate camera around the target

This topic is 452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to rotate my camera around the target horizontally and vertically. Example if q-e pressed only horizontally rotated. And when 1-2 pressed vertically rotated around its direction

Example Cameraview :

        public Matrix View { get; set; }
public Vector3 eye { get; set; } = new Vector3(0, 0, -5);
public Vector3 target { get; set; } = new Vector3(0, 0, 0);
public Vector3 Translation = new Vector3(0, 0, 0);

View = Matrix.Translation(Translation.X, Translation.Y, Translation.Z)* Matrix.LookAtLH(eye, target, Vector3.UnitY);

I think rotation must be based on target point not the camera itself.

##### Share on other sites
1. Translate its matrix by the distance from current position its target's position.
2. Then rotate around that position.
3. Then when you translate it back to its original position it should be rotated around the target.

##### Share on other sites

I had a similar situation with weapon firing a projectile. this is how I handled it. idk if it will help though or even the most effective way.

i don't store my position, rotation and scaling in vectors. I use a matrix that is updated each frame. It is also not full tested it.



// my projectile , equivelent to your camera.
Projectile p = pProjectile.Clone<Projectile>();
// retrieve the position vector from the matrix.
Vector3 pos = Vector3.TransformCoordinate(new Vector3(0, 0, 0), this.World);

// retrieve the direction the weapon is facing.
Vector3 localAhead = Vector3.TransformCoordinate(new Vector3(0, 0, 1), this.World);
// retrieve the up direction.
Vector3 localUp= Vector3.TransformCoordinate(new Vector3(0, 1, 0), this.World);

// retrieve the target position
Vector3 tarPos = Vector3.TransformCoordinate(new Vector3(0, 0, 0), soTarget.World);

// this offsets the projectile position to just outside the weapon position, not needed for you.
// now i set the objects world to the position. looking at the target's position,
// with up being relative the projectile's facing direction
p.SetWorld(Matrix.LookAtLH(projPos, tarPos, localUp));


1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013758
×