Selecting multiple units in 3D

Started by
7 comments, last by daniel_i_l 17 years, 12 months ago
So far I've implemented Ray picking for my 3D terrain engine, I can click on a unit or the map and it will tell me were I clicked. It does it by shooting a ray from the camera to the mouse and checking if it hits a unit (a sphere around the unit) or the terrain. But how do I make it select multiple units if I drag the mouse and make a 2D square on the screen? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
By the same way you created a ray from a single point, you can create a box from the four corner points (two corners actually). Extend the box infinitely or at least a long range, and check the objects against the box.

Illco
Point detection won't work for more than a single unit. You will need to generate lines from the edges of your box, and perform intersection tests between the units and the lines.
Hmm.. deleted my first post because I figured out a better way to do it. How about this:

Variables:
vLowerleft, vUpperright - 2 rays that make your drag frustrum.
vToObject - the object you are checking, do this for every object.
vForward, vRight, vUp - orientation of the camera/player.

You want to select the object if vToObject is left of vUpperright, below vUpperright, right of vLowerleft, and above vLowerleft. And left, right, above, and below are arbitrary based on your camera orientation.

For each object:
1) Get the vector to it (vToObject).
2) If vToObject dot vForward < 0, trivially reject the object; its behind you.
3) Take each vector (vUpperright, vLowerleft, vToObject) and dot it with vUp. Lets call the results high, low, and object, respectively. Remeber, these are floats, not vectors.
4) If object is between low and high, its in the drag frustrum. If not, skip the object.
5 & 6) Do the same for vRight. Dot each vector with vRight and make sure the object is between the two.

If you need more help visualizing whats going on, mathworld.com has some solid visuals of the dot product.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
Thanks, I want to try that but I have one question.
Lets say I have:
gluLookAt(10,40,20, 0,-0.2,-1, 0,1,0);

How do I get the vUp, vRight, and vForward vectors?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Quote:Original post by daniel_i_l
Lets say I have:
gluLookAt(10,40,20, 0,-0.2,-1, 0,1,0);

How do I get the vUp, vRight, and vForward vectors?
You can find it documented here, among other places.
I would think it woudl be trivial to just generate 4 "clip planes" from that box with normals facing inwards, and do a plane/bounding box test

cheers
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Quote:Original post by Ademan555
I would think it woudl be trivial to just generate 4 "clip planes" from that box with normals facing inwards, and do a plane/bounding box test

cheers
-Dan


Its not quite that easy. A rectangle on your screen gets mapped to a view frustrum in the 3D world, not a bounding box. Basically, instead of having straight edges, its more of 3D trapezoid. But testing the 4 clip planes is exactly what the algorithm is doing.

To get the forward, right, and up vectors from your opengl call, your parameters include the eye point, look at point, and up vector.
Forward is the look at point minus eye point.
Up is given.
Right is Up Cross Forward (or Forward X Up if you are using left-handed coord. system, I think thats what openGL uses).

There is a little problem with my algorithm. You don't know how long vUpperright and vLowerleft rays are in the 3D world. Make sure you normalize vUpperright, vLowerleft and vToObject before you dot them to your camera directions.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
blaze - I tried you algorithm and it worked! Thanks!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

This topic is closed to new replies.

Advertisement