Krynn's picture

Camera Orbit Around Vector

Does anyone know how to implement an orbiting camera? By this I mean the camera would be locked to a single lookat point, but the user may still move the mouse, and when this is done the camera would rotate around said point like a satellite would do to a planet. I cannot seem to find any source code that replicates this behavior, but would really appreciate it if someone could provide an example. Oh, and this is my first post.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
winterhell's picture

You'd have the Vector3 targetPoint, float cameraToTargetDistance,  Vector2 anglesXY
anglesXY would determine the direction of the camera position relatively to the point.
Your Vector3 cameraPos = targetPoint + Vector3.Transform(new Vector3(0,0,cameraToTargetDistance),Matrix4.CreateRotationX(anglesXY.X)*Matrix4.CreateRotationY(anglesXY.Y));// or something like that
Then your view matrix would be Matrix4.LookAt( cameraPos, new Vector3(0,1,0), targetPoint);

You need the delta XY mouse positions frame to frame basis to change X and Y angles( X mouse should change the Y angle and vice versa)
Your camera can also auto orbit by some degree on X and/or Y each frame.
I guess you'd want an option to look around while the camera keeping constant orbit, in which case you'd have another set of XY angles that determine the target your camera is looking, which would be derrived from the cameraPos just like the above expression, though you'd want to use different variables for the angles and probably to not change the original targetPoint.
Hope this helps.