When objects are duplicated(picking)....

Hi, all.
I want to make a opengl program about ‘picking’.
but, I have a problem.
I click the red sphere in the 1st picture.

After I drag the red sphere to green one like 2nd picture,
I want to select the green one, but I cannot move the green one and I can move only red one.

If I move the red one(don’t duplicate green and red) then,
I select the green one.

I think it is relevant to depth.
but I cannot solve the problem.
help please…

You didn’t describe how your pick algorithm works. In general, if you have more than one object at a single location, then you need to provide some way to allow the user to select among them. (In your particular example, your red and green spheres aren’t quite at identical locations, though they do overlap.)

In the case of identical locations, obviously, your program cannot read minds, so there is no way for it to unambiguously determine for itself what the user intends. The approach you need will involve feedback. For example, if you determine that more than one object exists at one location and that is the location the cursor is over, then you could allow the user to, say, scroll the mouse wheel, and with each tick of the wheel highlight a different object at the location of the cursor. Once the user sees the desired object is highlit, then they could, say, click the left mouse button to select it.

If your concern is specifically the example you’ve demonstrated, where two objects aren’t in identical locations but simply overlap, then you need to describe your pick algorithm. No one can debug it if they don’t know what it is doing. Presumably, it has something to do with the order that objects are drawn and/or picked. I’d guess that the red sphere is picked last, so even if the green sphere is picked, then the red one overrides that (to the extent they overlap). Alternately, the red sphere is picked first and then you stop checking to see if any other object is also located there and never even bother to see if the green sphere ought to be picked.

Oh… sorry I fogot writing the source code.
I found how to solve a problem…
zMax of selectBuf[](in picking) refer to fault data(min value).
so I rivised the correct data(max value).

thank you for your answer.

hits = glRenderMode (GL_RENDER);
if ( hits <= 0 ) 
int stack = selectBuf[0];
unsigned int zMax = selectBuf[1];

g_nSelect = selectBuf[3];
int index = 3 + stack;
int i;
for ( i = 1; i < hits; i++ ) {  // if(hits>1)
	stack = selectBuf[index];
	if ( zMax > selectBuf[index+1] ) {
		zMax = selectBuf[index+1];
		g_nSelect = selectBuf[index+3];
	index += 3 + stack;