Jump to content
  • Advertisement
Sign in to follow this  

Camera panning

This topic is 1897 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'm implementing a Maya-like camera for my SlimDX application.
I succesfully implemented rotation around camera target point and zoom, but I'm stuck trying to implement screen space panning.
I would like to replicate the same grab camera behaviour as ALT+MMB in Maya: translating along camera local X and Y axis.
My camera class is defined as follow:

public Vector3 EyePosition { get; set; }
public Vector3 Target { get; set; }
public Vector3 LookAt { get; set; }
public Vector3 Up { get; set; }
public Vector3 Right { get; set; }
public float FOV { get; set; }
public float RotationSpeed { get; set; }
public Matrix GetLookAtMatrix()
    return Matrix.LookAtLH(this.EyePosition, this.LookAt, this.Up);

The code I'm using to rotate around is:

float deltaX = MouseX - PreviousMouseX;
float deltaY = MouseY - PreviousMouseY;
deltaX *= camera.RotationSpeed;
deltaY *= camera.RotationSpeed;

Matrix matRotAxis, matRotY;

//create direction vector
camera.LookAt = (camera.Target - camera.EyePosition);
camera.LookAt = Vector3.Normalize(camera.LookAt);

//strafe vector
camera.Right = Vector3.Cross(camera.Up, camera.LookAt);
camera.Right = Vector3.Normalize(camera.Right);

//create rotation matrices
matRotAxis = Matrix.RotationAxis(camera.Right, deltaY);
matRotY = Matrix.RotationY(deltaX);
Matrix rotationMatrix = matRotAxis * matRotY;

//rotate up vector
camera.Up = Vector3.TransformCoordinate(camera.Up, rotationMatrix);
camera.Up = Vector3.Normalize(camera.Up);

//update position
camera.EyePosition = Vector3.TransformCoordinate(camera.EyePosition, rotationMatrix);
PreviousMouseX = MouseX;
PreviousMouseY = MouseY;

To pan the camera along its X and Y, I just add the translation delta to both Target and EyePosition,
but this results in strange behaviour. For example my camera.Up vector changes...
How can I translate both EyePosition and Target in camera space?

Share this post

Link to post
Share on other sites

nevermind, solved finding this topic: http://www.gamedev.net/topic/642379-camera-rotation-around-selected-object/


this is the working code if interested:

if (MiddleMousePressed && AltKeyPressed)
    int deltaX = ((ApplicationState)_applicationState).PreviousMouseX - ((ApplicationState)_applicationState).MouseX;
    int deltaY = ((ApplicationState)_applicationState).PreviousMouseY - ((ApplicationState)_applicationState).MouseY;

    Matrix inverseView = Matrix.Invert(camera.View);

    //screen space drag vector
    Vector3 drag = new Vector3(deltaX, -deltaY, 0.0f);
    //transform drag vector to world space
    drag = Vector3.TransformNormal(drag, inverseView);
    //drag = Vector3.Normalize(drag);
    drag *= camera.PanSpeed;

    PreviousMouseX = MouseX;
    PreviousMouseY = MouseY;
else if (LeftMousePressed && AltKeyPressed)
    float deltaX = MouseX - PreviousMouseX;
    float deltaY = MouseY - PreviousMouseY;

    Vector3 drag = new Vector3(-deltaX, deltaY, 0.0f);
    Matrix inverseView = Matrix.Invert(camera.View);
    //transform drag vector to world space
    drag = Vector3.TransformNormal(drag, inverseView);
    drag *= camera.RotationSpeed;
    //create axis from drag and direction vector
    Vector3 axis = Vector3.Cross(drag, camera.Direction);
    axis = Vector3.Normalize(axis);
    //rotate around axis
    Matrix rotation = Matrix.RotationAxis(axis, drag.Length() * Utilities.toDeg);
    //transform direction and up
    Vector3 dir = Vector3.TransformNormal(camera.Direction, rotation);
    //recalculate position
    camera.SetPosition(camera.LookAtPoint - (dir * camera.Distance) );
    //recalculate up
    Vector3 upAxis = Vector3.Cross(camera.Up, dir);
    upAxis.Y = 0.0f;
    upAxis = Vector3.Normalize(upAxis);
    //cross direction and up axis for up vector
    camera.SetUp( Vector3.Cross(dir, upAxis) );

    PreviousMouseX = MouseX;
    PreviousMouseY = MouseY;
else if (AltKeyPressed && MouseWheel != 0)
    //recalculate position
    float zoomFactor = (1 + 0.1f * MouseWheel);
    camera.LookAtPoint += camera.Direction * 1 / zoomFactor;
    camera.SetPosition(camera.LookAtPoint - (camera.Direction * camera.Distance * zoomFactor));

    MouseWheel = 0;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!