Author Topic: How to do 2D click to 3D object picking?  (Read 21465 times)

Offline Nodin

  • byte
  • *
  • Posts: 13
    • View Profile
How to do 2D click to 3D object picking?
« on: June 11, 2010, 05:10:05 am »
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!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #1 on: June 11, 2010, 07:45:53 am »
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.

Offline Nodin

  • byte
  • *
  • Posts: 13
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #2 on: June 11, 2010, 07:52:44 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #3 on: June 11, 2010, 09:53:25 pm »
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.

Offline Nodin

  • byte
  • *
  • Posts: 13
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #4 on: June 12, 2010, 05:08:04 pm »
Thanks, it was the collision check setting!

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

Offline pritom057

  • byte
  • *
  • Posts: 27
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #5 on: July 04, 2010, 04:26:50 pm »

still it does not work perfectly

I did this
Code: [Select]

//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??

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #6 on: July 04, 2010, 09:41:32 pm »
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.

Offline Ilse

  • byte
  • *
  • Posts: 1
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #7 on: July 18, 2010, 09:03:28 am »
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)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #8 on: July 18, 2010, 10:25:57 am »
.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).
« Last Edit: August 31, 2010, 08:11:16 pm by EgonOlsen »

Offline smither

  • byte
  • *
  • Posts: 13
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #9 on: August 31, 2010, 07:11:03 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #10 on: August 31, 2010, 08:14:03 pm »
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.

Offline smither

  • byte
  • *
  • Posts: 13
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #11 on: September 01, 2010, 08:55:35 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #12 on: September 01, 2010, 11:09:18 pm »
That depends on the type of collision detection you are using!?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #13 on: September 01, 2010, 11:28:54 pm »
Also make sure that the direction vector given to calcMinDistanceAndObject3D is a unit vector...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #14 on: September 06, 2010, 09:57:42 pm »
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