# Having trouble with trying to get 3d mouse coords

This topic is 3006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! Ive been working on getting a little dot to move around flat 3D terrain that is controlled by a mouse input. x = e.X; y = e.Y; Vector3 near = new Vector3(x, y, 0); Vector3 far = new Vector3(x, y, 1); near.Unproject(device.Viewport, projectionMatrix, viewMatrix, Matrix.Identity); far.Unproject(device.Viewport, projectionMatrix, viewMatrix, Matrix.Identity); Vector3 ray = near - far; richTextBox1.Text = near.ToString(); Is this right? Im getting incorrect results and im wondering if anyone can tell me what im doing wrong? thanks:)

##### Share on other sites
h,maybe we can work together,i make that too, and you code maybe is wrong,you need convert to 2d coordenates(you Screem ) to 3d World, next you need detect a collision with you terrain with "D3DXINTERSECTION" function.

check this.
http://www.gamedev.net/community/forums/topic.asp?topic_id=521246.

##### Share on other sites
sorry but im using managed directx via c#

##### Share on other sites
nvm i solved it

x = e.X;
y = e.Y;
Vector3 p1 = new Vector3(x, y, 0);
Vector3 p2 = new Vector3(x, y, 1);
p1.Unproject(device.Viewport, projectionMatrix, viewMatrix, Matrix.Identity);
p2.Unproject(device.Viewport, projectionMatrix, viewMatrix, Matrix.Identity);
Vector3 ray;

float wy = 0;
float wx= ((wy - p1.Y) * (p2.X - p1.X)) / (p2.Y - p1.Y) + p1.X;
float wz = ((wy - p1.Y) * (p2.Z - p1.Z)) / (p2.Y - p1.Y) + p1.Z;
ray = new Vector3(wx, wy, wz);

Thanks anyway!:D

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631431
• Total Posts
3000039
×