Need Help Making A "Perfect Pan Method"

josephbt

25-11-2009 12:07:55

Hi to you all Mogre users.
First of all, i must apologize for any incorrect writing.
I'm brazilian so my english isn't very good.
I've been browsing the forums for a resoanable amount of time now.
And I couldn't find something that was much helpfull. So here I am.

I'm developing some sort of a 3D Environment App to edit meshes and stuff.
The project is at it's very beggining stages but i'ts working already.
I've recently started developing basic navigation methods: Pan, Zoom and Rotate.
They're all done using the mouse, like any other 3D Object Editor(such as Google SketchUp).

The problem is I can't get the pan function to work.
As seen in Google sketchUp, whenever you "pan" something, the solid or "whatever"
that is behind the mouse cursor remains at all times under it.
That's the effect I've been unsuccesfully trying to achieve.

I'm using MOIS to capture mouse input.
The method I developed is signed Pan(MOIS.MouseState_NativePtr mouseState),
and is executed every time I press the middle mouse button
and move the mouse. It has only one parameter wich is the return data
of the MouseMoved Event. Below is the code I currently have, but it doesn't work
at all.


public void Pan(MOIS.MouseState_NativePtr mouseState)
{
float width = this.Workspace.Viewport.ActualWidth;
float height = this.Workspace.Viewport.ActualHeight;

Vector2 lastMouse = new Vector2(mouseState.X.abs, mouseState.Y.abs);
Vector2 mouse = new Vector2(mouseState.X.abs + mouseState.X.rel, mouseState.Y.abs + mouseState.Y.rel);

Ray lastMouseRay = this.Workspace.Camera.GetCameraToViewportRay(lastMouse.x / width, lastMouse.y / height);
Ray mouseRay = this.Workspace.Camera.GetCameraToViewportRay(mouse.x / width, mouse.y / height);

RaySceneQuery raySceneQuery = this.Workspace.SceneManager.CreateRayQuery(lastMouseRay);
raySceneQuery.SetSortByDistance(true, 2);
RaySceneQueryResult result = raySceneQuery.Execute();

Vector3 lastProjection = lastMouseRay.GetPoint(result.Back.distance);

float distanceX = ((lastProjection.x * mouse.x) / lastMouse.x) - lastProjection.x;
float distanceY = ((lastProjection.y * mouse.y) / lastMouse.y) - lastProjection.y;

this.Workspace.CameraNode.Translate(distanceX, distanceY, 0.0f);
}


If someone can find out what's wrong with this code, or give me some hints on other aproaches
to panning functions, i would more than apreciate. Thanks in advance.

P.S.: My zoom function works very nice and has one nice feature. It zooms throught
the normal casted by the mouse cursor. Wich makes zooming pretty effective.
In case anyone needs one, i'd be more than happy to share it.