Members - Reputation: 104
Posted 05 September 2012 - 10:11 AM
I was thinking of reading the (x,y) screen coordinates (not the world coordinates) of the unit and comparing that with the (x,y) mouse coordinates. But I'm not sure if this approach is commonly used or efficient. I was also thinking if it would be possible to associate a hitbox with the mouse and project that into the world.
Are there other approaches commonly used?
Members - Reputation: 419
Posted 05 September 2012 - 10:23 AM
Members - Reputation: 623
Posted 05 September 2012 - 10:48 AM
The technique you are looking for is called ray casting. In most cases you need to take the screen coordinates, normalize them, and cast a ray from that origin. Next you check for collisions with that ray. If you are using a graphics or game engine, its likely they have a built-in ray casting feature.
Also, as a point becomes a ray when projected in 3D, a rectangle would become a frustum. So if you plan in the future on also doing rectangle selection to select several units at once, you would create a smaller sub-frustum inside your viewing frustum and use that to check what units are intersecting it. But for clicking on one unit, a ray is all you need.