• Advertisement
Sign in to follow this  

MDX, D3D9, C#: Mouse dragging 3D objects

This topic is 2651 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 have a Direct3D application with small models scatered all over in 3D space and I need to be able to move them around with the mouse. I've already got the selection code working so they become active when clicked on, but I need them to move proportionatly with the mouse cursor when dragging them. So I record the start and end points of change in mouse position, then try to project this line into an equivalent distance in the 3D world coordinates such that the object moves the same amount of screen distance as the mouse cursor did. I can't get it to track correctly though. Usually the object travels farther than the mouse pointer did and so gets ahead of the mouse pointer.

In the code below, the position vector represents the change in position, which is then added to the position vector of the model to relocate it.


device.Transform.World = Matrix.Scaling(model.scale, model.scale, model.scale) * Matrix.RotationZ(model.rotate_z) * Matrix.RotationY(model.rotate_y) * Matrix.Translation(model.position);

Vector3 world1_v = Vector3.Unproject( new Vector3( last_mouse_pos.X, last_mouse_pos.Y, 1f ), device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

Vector3 world2_v = Vector3.Unproject(new Vector3( current_mouse_pos.X, current_mouse_pos.Y, 1f), device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

float deltaZ = world2_v.Z - world1_v.Z;
float deltaX = world2_v.X - world1_v.X;
float deltaY = world2_v.Y - world1_v.Y;

model.position += new Vector3( deltaX, deltaY, deltaZ );

[Edited by - Pyronaught on October 21, 2010 1:30:56 PM]

Share this post


Link to post
Share on other sites
Advertisement
I got it working. Here's the code that keeps the model movement tied to the mouse pointer movement:

device.Transform.World = Matrix.Scaling(model.scale, model.scale, model.scale) * Matrix.RotationZ(model.rotate_z) * Matrix.RotationY(model.rotate_y) * Matrix.Translation(model.position);

Vector3 screen_v = Vector3.Project(new Vector3(0, 0, 0), device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);
float dx = screen_v.X – current_mouse_pos.X;
float dy = screen_v.Y – current_mouse_pos.Y;

Vector3 world1_v = Vector3.Unproject(new Vector3( last_mouse_pos.X + dx, last_mouse_pos.Y + dy, screen_v.Z), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity);

Vector3 world2_v = Vector3.Unproject(new Vector3(current_mouse_pos.X + dx, current_mouse_pos.Y + dy, screen_v.Z), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity);


float deltaZ = world2_v.Z - world1_v.Z;
float deltaX = world2_v.X - world1_v.X;
float deltaY = world2_v.Y - world1_v.Y;

model.position += new Vector3( deltaX, deltaY, deltaZ );

Share this post


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

  • Advertisement