Jump to content
  • Advertisement
Sign in to follow this  

2D Mouse to 3D position

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

If you intended to correct an error in the post then please contact us.

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);

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;
//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 :D

Share this post

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!