Jump to content
  • Advertisement
Sign in to follow this  

[Solved] Moving Object With Mouse - C# Directx

This topic is 2802 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 some code that partially works but once it goes a certain distance it stops moving.

The distance is calculated using sin rule, the angles of the last and current direction dot the direction of X (1,0,0).

float distance = (float)(Math.Sin(Math.Acos(Vector3.Dot(Direction, new Vector3(1, 0, 0)))) * 
((Camera.Position - MoveTool.Position).Length() / Math.Sin(Math.Acos(Vector3.Dot(d, new Vector3(1, 0, 0))))));

MoveTool.Position.X += (d - Direction).X * distance;
Direction = d;

Direction and d are just unit vectors based on the mouses current position in 3d space. Which I get using this:

Vector3 d = Vector3.Unproject(new Vector3(e.X, e.Y, 1f), Device.Viewport,
Device.Transform.Projection, Camera.View, Matrix.Identity);

d.Multiply(1 / d.Length());

d being the current position, and Direction being the previous direction of the mouse.

distance is the length from the camera position to the new position MoveTool should be at. I multiply it by the difference between d and Direction, which should be the change of the x direction. Then add it to the position of the MoveTool.

This is the result:

The position of the mouse is off in that video, but it shows the general movement and how the position isn't changing with the mouse.

[Edited by - TheSkyline13 on October 18, 2010 6:50:40 PM]

Share this post

Link to post
Share on other sites
I can give you an alternative way to do this.

Get the line down the axes your dragging along, like the yellow line here for the red axes.

Then, get the line that the mouse point makes. (should be easy using UnProject)

Calculate the point on the axes line that is closest to the the mouse line.

Whenever you move the mouse, do the same thing and get the difference and apply that difference to the models location.

Here is the code I used find the closest point to intersection of the two lines.

static ArgoVector3 getDragPoint(ArgoVector3 *dragLine, ArgoVector3 *unProjLine)
ArgoVector3 P0(dragLine[0]);
ArgoVector3 Vp(dragLine[1]);
ArgoVector3 Q0(unProjLine[0]);
ArgoVector3 Vq(unProjLine[1]);
float a;
float b[2];
float c[2];
float s;






ArgoVector3 dragPoint(P0+Vp*s);

return dragPoint;

video if your interested

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!