Sign in to follow this  

Having trouble with trying to get 3d mouse coords

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 this post

Link to post
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.

Share this post

Link to post
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

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this