Look at cursor

 Author: Daniel Jackson

The idea behind this is simple enough, we make the player look towards the mouse. However, when you realize the mouse is always right with the camera and not actually in the world things get a bit more complicated. Knowing that our biggest problem was trying to do a ray cast from the position of the mouse on screen, to a position in world space. From there we could take those coordinates and rotate the player to face them. When we tried to do this, everything was wrong, the start point of the ray wasn't connected to the mouse correctly, it was moving in a strange way, and the "end" or what we could see from the camera was in an incorrect position.

In the end I got the mouse position and converted them to ndc space so the math could be done correctly and saved them into a vector that represented them on the near plane. Then, I had to un-project the matrices to get a position that would act as a start and end point to a line. Finally, I saved the start position and subtracted the start and end points to get a direction, then did a ray-to-aabb collision test and rotated the transform to the orientation facing the position specified by the test.

Here the position of the mouse is displayed using the red circle, the transform has been rotated so the Z (the blue line) is facing the mouse position:



Comments

Popular posts from this blog

Adding assets to every enemy -- Owen Meyers

Sphere to AABB collision