I'm making a networked game which has a birds-eye view and I'm trying to convert the 2D mouse coordinates into 3D space. Right now I have a perfectly flat grid and it intends to remain perfectly flat, all I really want is the mouse coordinates to translate onto this grid.
I had a solution figured out that worked fine if the camera was perfectly straight looking down which I thought was going to be enough for what I wanted, but now I've found that it would be nicer if the camera had a slight tilt to it which completely messes with what I have. Could someone help me out in what I'm missing?
nearsourceV = GD.Viewport.Unproject(new Vector3(mouse.X, mouse.Y, 0f), camera.Projection, camera.View, camera.World); farsourceV = GD.Viewport.Unproject(new Vector3(mouse.X, mouse.Y, 1f), camera.Projection, camera.View, camera.World); mouseCoordinatesIn3D = Vector3.Normalize(farsourceV - nearsourceV); zoomMultiplier = -mouseCoordinatesIn3D.Y; zoomMultiplier = camera.CameraOffset.Y / zoomMultiplier; mouseCoordinatesIn3D = mouseCoordinatesIn3D * zoomMultiplier; mouseCoordinatesIn3D.X = mouseCoordinatesIn3D.X + camera.CameraOffset.X; mouseCoordinatesIn3D.Z = mouseCoordinatesIn3D.Z + camera.CameraOffset.Z; //Sends data and moves character
I figured I needed to include the camera rotation into it somehow, but I'm not really sure how or where.