What's the preferred method to select units with the mouse, i.e., you have a unit on screen that you want to either control or see its stats and you hover your mouse over the unit and click to select it?
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?
Selecting Units
Started by Qaia, Sep 05 2012 10:11 AM
2 replies to this topic
Sponsor:
#2 Members - Reputation: 419
Posted 05 September 2012 - 10:23 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.
#3 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.
My development blog: Electronic Meteor






