Public Group

# [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.

## 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 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;	Vp.normalize();	Vq.normalize();	a=1.0/(1.0-(Vp.dot(Vq)*Vp.dot(Vq)));	b[0]=a;	b[1]=Vp.dot(Vq)*a;	c[0]=ArgoVector3(Q0-P0).dot(Vp);	c[1]=ArgoVector3(P0-Q0).dot(Vq);	s=b[0]*c[0]+b[1]*c[1];	ArgoVector3 dragPoint(P0+Vp*s);	return dragPoint;}

##### Share on other sites
Thanks for the help, that makes a lot more sense to me that way then the way I was trying to get it to work.

• 14
• 10
• 9
• 49
• 12
• ### Forum Statistics

• Total Topics
631389
• Total Posts
2999725
×