First object

Hi!



I have a little problem with choosing an object. I use the code:



        MouseInput thisMouse=MouseInput.get();
        if (thisMouse.isButtonDown(0)){
        Vector2f screenPos=new Vector2f();
        screenPos.set(thisMouse.getXAbsolute(),thisMouse.getYAbsolute());
        Vector3f worldCoords=display.getWorldCoordinates(screenPos,0);
        Ray mouseRay=new Ray(cam.getLocation(),worldCoords.subtractLocal(cam.getLocation()));
               
        PickResults pr = new BoundingPickResults();
       
        pr.setCheckDistance(true);
       
        scene.findPick( mouseRay, pr );
        for(int i = 0;i < ile; i++) {       
           if( b2[i].getWorldBound().intersects( mouseRay ) ) { b2[i].setRandomColors();}
        }
        }



It change the color of Box that I click, but when I have some other object behind object that I have click is change color too. How I can fix it ?? I want only first object change color.

TIA everyone.

since uve already set the checkDistance(true), just do pr.getPickData(0).getTargetMesh().setRandomColors()



the 0th target is always the first one u hit~

If I have some object behind, trat ocject change color only.

When the first intersection == true, cancel the ray…



Hunter

Please give me a code, I don't understand what I must do. :frowning:

aderal said:

Please give me a code, I don't understand what I must do. :(


When you loop  through the PickResults, the 0th one is the object that is closest...
So basically, you change this for loop


for(int i = 0;i < ile; i++) {      
        if( b2[i].getWorldBound().intersects( mouseRay ) ) { b2[i].setRandomColors();}
}

U give me the same code that I wrote in my first post, hunter maybe u Can wrote the code for me.