C# SharpDX how to rotate camera around the target

Started by
2 comments, last by Zototh 6 years, 6 months ago

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.

Advertisement
  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.

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));
  

 

This topic is closed to new replies.

Advertisement