How to do 2D click to 3D object picking?

Started by Nodin, June 11, 2010, 05:10:05 AM

Previous topic - Next topic

androidman

Hi EgonOlsen,

Based on http://www.jpct.net/wiki/index.php/Picking, it didn't work when i touch on object but if i touch on another area that is beside object --> it work.


SimpleVector p = Interact2D.reproject2D3D(camera, frameBuffer, (int)touchPos.x,(int)touchPos.y).normalize();
Object[] objs= world.calcMinDistanceAndObject3D(camera.getPosition(), p, 1000);


how can i fix it?

i am using  the jpct-ae version that was posted at http://www.jpct.net/forum2/index.php/topic,1561.225.html.




EgonOlsen

You are not working in world space but in camera space. Please look at that wiki page again and use the ....WS-method that it mentions. Also make sure that your touch coordinates match your frame buffer coordinates. Sometimes a status bar or similar shifts the whole thing by a few pixels.

androidman

Thank you very much, EgonOlsen.

You are right. it worked. :D

ginopeloso

What if I don't want the first object in the direction of my click but the one behind it?
In my model I have some objects which are transparent (like glasses); if the user clicks these ones I have to ignore them and try to pick another object behind the clicked one in the same direction of the click (if this one is transparent again the one behind, until I pick a non-transparent object or nothing)

I tried with the following code but did not succeed; the app continues to pick always the same object:

SimpleVector dir = Interact2D.reproject2D3D(camera, frameBuffer, screenX, screenY);
Object[] res = world.calcMinDistanceAndObject3D(camera.getPosition(), dir, 10000);
Object3D clickedObject = (Object3D)res[1];
while (clickedObject != null && clickedObject.getName().startsWith("transparent")) {
dir.z = clickedObject.getTransformedCenter().z;
res = world.calcMinDistanceAndObject3D(camera.getPosition(), dir, 10000);
clickedObject = (Object3D)res[1];
}


Is there a way for doing it?

EgonOlsen

Just make all transparent objects invisible before doing the picking (and reset them afterwards).