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

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

Previous topic - Next topic

Nodin

How can I take a touch event on the display and project it into world coordinates and pick a world object?

Previously using jPCT I had success using the object ids with something like;

SimpleVector position=new SimpleVector(Interact2D.reproject2D3D(camera, buffer, x, y));
int[] res=Interact2D.pickPolygon(theWorld.getVisibilityList(), position);

..but that's not supported in jPCT-AE?

Thanks!

EgonOlsen

These methods in Interact2D work in camera space, i.e. on the visiblity list. For compiled objects, that visibility list actually doesn't exist (in fact it does, but it's content has changed), which is why you can't use these methods on compiled objects. In AE, all objects are being rendered as compiled object, which is why the method is gone. You have to use something like calcMinDistance() instead. This thread has some information about it:http://www.jpct.net/forum2/index.php/topic,1468.0.html.

Nodin

Thanks, I did finally get a touch point to project a ray into the world, but I can't pick an object.

Using calcMinDistanceAndObject3D always returns empty, even when I can see my ray hitting an object.

Object[] result = getWorld().calcMinDistanceAndObject3D(camera.getPosition(), ray, 10000F);

result[0] always has a float
result[1] is always null

Any ideas?

EgonOlsen

ray has to be transformed in world space before (not visible in your code snippet if you actually do this or not) and the objects you want to pick have to be colliders, i.e. Object3D.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS); has to be set.

Nodin

Thanks, it was the collision check setting!

Cloned objects don't have the setting of the original.

pritom057


still it does not work perfectly

I did this


//SimpleVector position = new SimpleVector(Interact2D.reproject2D3DWS(world.getCamera(), fb, (int) x,(int) y));
SimpleVector position = new SimpleVector(Interact2D.reproject2D3D(world.getCamera(), fb, (int)x,(int)y, 10.f));
   
   position.matMul(world.getCamera().getBack().invert3x3());
   position.add(world.getCamera().getPosition());
   
   Object[] result = world.calcMinDistanceAndObject3D(world.getCamera().getPosition(), position, .01F);


but the problem is it always returns the object which is neat from camera

I have also used reproject2D3DWS(..) to same thing happened
any idea how can we do this??

EgonOlsen

It's an engine's bug...and it's in desktop jPCT too. Strange that nobody (including myself) ever noticed it. I've uploaded a version of AE that should fix it here: http://www.jpct.net/jpct-ae/download/alpha/jpct_ae.jar as well as a fixed jar for desktop jPCT here: http://www.jpct.net/download/beta/jpct.jar.

Please note that in your example, the upper bars obviously count for the touch events, i.e. if you touch at the beginning of the actual gl canvas, the y coordinate isn't 0 but something like 50. You have to take this into account when giving the y-coordinate to Interact2D. In your example, i have to touch 50 pixels above the trees to hit them.

Ilse

Hi,

I want to do the same like you, but I can get it working.

I use the demo and set the "plane" -> plane.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

then (with the x and y from the touch event ) I do the same like you:

SimpleVector position = new SimpleVector(Interact2D.reproject2D3D(world.getCamera(), fb, (int)x,(int)y, 10.f));         
position.matMul(world.getCamera().getBack().invert3x3());
position.add(world.getCamera().getPosition());         
Object[] result = world.calcMinDistanceAndObject3D(world.getCamera().getPosition(), position, .01F);

but the resulting float is always -> 1.0E12 == Object3D.RAY_MISSES_BOX

can you give mw a hint or a working example

Thanks
Ilse

P.S. I am using the last version from the main page (http://www.jpct.net/jpct-ae/download/alpha/jpct_ae.jar)

EgonOlsen

#8
.01F as the last parameter of calcMinDistance of much too low. Anything farer away than this value is will be excluded from the calculation. Try something like 100, 1000 or 10000 (depending on your scene).

smither

I have the same problem that Ilse had, i always get 1.0E12 no matter what value i use for the last parameter.

Any idea?

EgonOlsen

If you are sure that the collision set properly and that the last parameter is fine too, try to adjust Config.collideOffset to a higher value and see if that helps.

smither

What's more expensive in terms of performance? Call to calcMinDistanceAndObject3D or just check a collision between the position the user click on the screen?

Because if i can have a confirmation from the listener I won't have to figure out how to fix the issue unless the difference between these 2 approaches is too much.

EgonOlsen

That depends on the type of collision detection you are using!?

EgonOlsen

Also make sure that the direction vector given to calcMinDistanceAndObject3D is a unit vector...

EgonOlsen

Because this is a common topic and the forum contains some wrong code for picking, i decided to add a page to the wiki: http://www.jpct.net/wiki/index.php/Picking