# 2D Mouse to 3D position

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

## Recommended Posts

hi guys
I want to transform my mouse coordinates into my 3D system.
However, I want to check if someone clicks on the boundingbox.
I tried it with raycasting, but I failed >.<
I get for f 0.0 nearly everywhere on my screen (even though i dont click on the box), except in the top left corner of the screen, where i get null.
My values for my planes are kinda weird aswell.
For Example: My nearpoint gets for his x 0.007199... and my farpoint x 71,99... when i click on the point where x should be around 4.

Here is my code:
setting the Matrizes:

 World = Matrix.Identity; View = Matrix.CreateLookAt(new Vector3(0, 0.1f, 0 + 0.01f), new Vector3(0, 0, 0), Vector3.Up); Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.01f, 1000f); 

Unprojecting:
public Vector3 Unproject() { Vector3 nearPoint = new Vector3(ms.X, ms.Y, 0); Vector3 farPoint = new Vector3(ms.X, ms.Y, 1); Viewport viewport = GraphicsDevice.Viewport; nearPoint = viewport.Unproject(nearPoint, Projection, View, World); farPoint = viewport.Unproject(farPoint, Projection, View, World); Vector3 direction = farPoint - nearPoint; direction.Normalize(); //Ray(Vector3 position, Vector3 direction); Ray ray = new Ray(nearPoint, direction); BoundingBox b = new BoundingBox(new Vector3(0, 0f, 0), new Vector3(1f, 1f, 1f)); float? f = ray.Intersects(b); if (f != null) { Vector3? v = ray.Position * f; return v; } }

Plz help me

##### Share on other sites
Instead of transforming mouse coordinates into 3D, try the other way around: Convert the 3D into 2D, simply disregard the Z-axis and do regular 2D collision checking..

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013710
×