Moving objects with Mouse Picking

XainFaith

12-07-2009 06:41:03

Hi there i have recently been developing as small level editor using Mogre and C# and i have mouse picking so objects selected but i can not seem to get and object to move using the mouse. There was a sample of sorts for this in the tutorials but it does not seem to work nore does the origanal from the tutorials compile but here is what i got and i will describe my problem in a little more depth.

protected virtual void onLeftDragged(MouseEventArgs e)
{
//normalise mouse coordinates to [0,1]
//we could have used the panel's width/height in pixels instead of viewport's width/height
float scrx = (float)e.X / mWindow.Width;
float scry = (float)e.Y / mWindow.Height;

Ray ray = cam.GetCameraToViewportRay(scrx, scry);
RaySceneQuery query = mgr.CreateRayQuery(ray);
query.SetSortByDistance(true);
RaySceneQueryResult results = query.Execute();

foreach (RaySceneQueryResultEntry entry in results)
{
if (entry.worldFragment != null)
{
this.mCurrentObject.Position = entry.worldFragment.singleIntersection;
}
}
return;
}

For the most part this codes nothing mostly because it never gets a World fragment and i am not sure why.

All i really want to do is move the object with the mouse like any 3D modeling program could if someone can tell me what i am doing wrong or if i am missing something that would be great.

Anyways thanks for taking the time to read my post.

Spanky

12-07-2009 07:09:12

The world fragments, from what I remember, are things like Terrain. If you don't have terrain, you won't get a world fragment. If you are interested in the entities that you have intersected with, such as the floor plane or a wall, the 'movable' is what you are after. Check that value and it will most likely be what you are looking for.

Shawn

XainFaith

12-07-2009 07:15:24

Well i can select and i hold the object selected in a variable but i want to beable to move it even without any other items so no terrian walls or things of that sort just the one model being able to be moved.

So like blender 3d does you select and object and you can move it around in 3d space.

Spanky

12-07-2009 17:13:18

Ah I see what you are saying. In order to do that, you don't actually need another object then.

The first and most basic way to move an object is to use the ray that you've already calculated and call GetPoint or whatever it is and pass it a constant value [maybe how far it currently is]. You can then just set the object that you already stored to that position. That's not the best approach though since it is not really relative to anything and will appear to move in and out towards the camera when moving to the sides.

From there though, you can actually start doing "axis aligned" or "plane aligned" movement. If you want to keep the object on the 2D plane that it currently exists on [using the x/y plane as an example], you can do that. First, create a plane with the objects start position and straight up as the point and normal. This plane only needs to be calculated once, at the start of movement. Now, when you go to move the object, you have a new ray [under your cursor] and you have the original plane. Get the intersection point between these two and then put your object there. The Z value won't have changed so you can now move the object around on the "flat" plane. You can do this for other planes as well.

In order to a single axis [think the standard issue x, y, or z translation in most 3D packages] you can do the same thing as above but clamp the "extra" axis to the starting value. So if you are moving on the x axis only, still use the same x/y plane stuff you did before but clamp the Y value to be the same as the starting positions [which you can get from the plane you calculated earlier]. This will effectively move the object along a single axis only.

You can even do screen-space movement if you want to but I would recommend getting these 2 modes working first as they are the most heavily used. You don't actually need the intersection of the "world" to do any of this. The "world" fragments are actual pieces of geometry such as the master terrain but if you don't have any of that, it obviously won't work.