• 14
• 12
• 9
• 10
• 13

# [.net] vector3.Unproject problem

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

## Recommended Posts

Hi there, I keep reading every where that vector3.Unproject unprojects a vector from the screen to world(e.g. mouse coordinates). However, what ever vector i give the function the resulting vector is unchanged and is equal to my camera coordinates :S. My world is 3D but i only use the X and Y coordinates and the camera always looks down perpendicular on the plane so it's kinda like 2D. Is that a part of the problem or am i just using the function wrong? here is my code, i am using C#: Vector3 mousePostion3D = new Vector3(0, 0, 0); mousePostion3D.X = MousePosition.X; mousePostion3D.Y = MousePosition.Y; mousePostion3D.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World); Please help, thanks in advance

##### Share on other sites
Hi,

IIRC Unproject returns the unprojected vector, so try

mousePostion3D = mousePostion3D.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

Regards,
Andre

##### Share on other sites
unproject doesn't return anything(it's type is void), it unprojects the vector that calls it.

##### Share on other sites
You are right, sorry. I haven't worked with MDX for quite some time.

What do your projection matrices look like?

Regards,
Andre

##### Share on other sites

Like Andre said, check your matrices. If you're working with a pure device, device.Transform.* might be silently returning identity matrices. OTOH, it should actually be throwing InvalidCallExceptions when you try to retrieve values from a pure device, so are you sure these matrices are being set up correctly before you use them?

##### Share on other sites
Thanks for your help i have found the problem, it seems that i need to project the vector from 000 to the screen to get a Z value and then change the x and y values and then unproject.

Vector3 mousePostion3D = new Vector3(0, 0, 0);

mousePostion3D.Project(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

mousePostion3D.X = MousePosition.X;
mousePostion3D.Y = MousePosition.Y;

mousePostion3D.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

its all in the Z value.