Converts a mouse coordinate into a ray cast from screen location into distance along a line that is obscured by mouse pointer
| 1210 | // Converts a mouse coordinate into a ray cast from screen location into |
| 1211 | // distance along a line that is obscured by mouse pointer |
| 1212 | olc::vf3d ScreenRay(const olc::vf2d& vScreenPos) const |
| 1213 | { |
| 1214 | // Create parallel ray from screen along projected depth |
| 1215 | olc::vf3d ray_parallel = { |
| 1216 | (2.0f * vScreenPos.x) / vScreenSize.x - 1.0f, |
| 1217 | 1.0f - (2.0f * vScreenPos.y) / vScreenSize.y, |
| 1218 | 1.0f, 1.0f |
| 1219 | }; |
| 1220 | // Invert that screen point into view space |
| 1221 | olc::vf3d ray_projected = matProjectionInv * ray_parallel; |
| 1222 | ray_projected.w = 0.0f; |
| 1223 | // Invert into world space (relative to 0,0,0) |
| 1224 | olc::vf3d ray_world = matViewInv * ray_projected; |
| 1225 | ray_world.w = 0.0f; |
| 1226 | // Return normalised ray to be tidy |
| 1227 | return ray_world.norm(); |
| 1228 | } |
| 1229 | |
| 1230 | protected: |
| 1231 |