Jump to content
  • Advertisement

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 this post

Link to post
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.
Vector3 projPos = Vector3.Add(localAhead, v3ProjectileOffset);
// 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));


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!