This topic is now archived and is closed to further replies.


Finding a vector in the direction of the cursor

Recommended Posts

BlueGrass    122
Hi. I''m trying to figure out how to find a 3D directional vector from the camera in the direction of the cursor. At first I tried using the camera''s direction vector and just rotated it based on the x and y axis distances from the center of the screen. But that didn''t work since it didn''t account for perspective. Then I calculated an "up" vector for the camera. So I had a directional vector, an up vector, and the cross vector of both. I translated the directional vector along the cross vector using the x axis distance of the mouse, and then along the up vector using the y axis distance. But that didn''t work either. I don''t know why... maybe the "up" vector was miscalculated. Then I thought I could just use the average of all four directional vectors which could be taken from the clipping plane intersections. But I don''t know how to find the clipping plane equations... So if anyone has any ideas I''d love to hear them! Thanks BlueGrass

Share this post

Link to post
Share on other sites
Scarab0    122
If you have the camera''s forward, up and right vectors like you do, you can get the vector you want more easily than you think. Assuming all those vectors are normalized, you can get the vector you want as follows:

// Vector3D forward, up, right are the normalized camera vectors
// float near is the distance to the near clipping plane
// int x, y are the mouse cursor coordinates

Vector3D cursorDirection = forward * near + up * y + right * x;

At least, that''s how I understand it.

Share this post

Link to post
Share on other sites