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

Offline Ulrick

  • byte
  • *
  • Posts: 18
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #15 on: November 16, 2010, 11:01:01 am »
Hi Egon,

just to be sure I'm doing it right, in JPCT-AE, if I use the following:

Object3D picked=world.getObject(result[1]);

I get always a null pointer.

while if I use the following:

 Object3D picked=(Object3D)result[1];

I get it right.

Is it correct?

Thanks,

Ulrick

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #16 on: November 16, 2010, 12:03:54 pm »
result is the return value of which method here?

Offline Ulrick

  • byte
  • *
  • Posts: 18
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #17 on: November 16, 2010, 03:54:11 pm »
I got your sample code from the Wiki (http://www.jpct.net/wiki/index.php/Picking):

-- EXTRACT --
SimpleVector dir=Interact2D.reproject2D3D(camera, frameBuffer, x, y);
int[] res=Interact2D.pickPolygon(world.getVisibilityList(), dir);

In res, you'll find the polygon- and the object number (in that order), if the picking actually picked something. If not, res is null. Your picked Object3D is now

(*) Object3D picked=world.getObject(res[1]);
-- EXTRACT --

But that works probably for the Desktop version of JPCT (I didn't tested it).

In JPCT-AE, doing the same, will just give a null pointer executing the marked (*) line of code

While casting to Object3D return the right value:

 Object3D picked=(Object3D)result[1];

I was writing just to be sure it was the correct way in JPCT-AE.

Thanks,

Ulrick

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #18 on: November 16, 2010, 08:33:32 pm »
What really confuses me, is that there is no pickPolygon(...)-method in AE...so i don't get how you can compare the output of those two methods if one doesn't exist ??? That said, the way that uses the cast is the correct one for AE and so it's written in the wiki:
Quote
It's the only one that is available in jPCT-AE
.

Offline jmachete

  • byte
  • *
  • Posts: 2
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #19 on: December 20, 2010, 03:15:09 pm »
I guys,

I'm following the wiki but I still haven't make this work yet. I'm doing this inside the method "onDrawFrame", so I'm a bit confusing in terms of how I can get the picked object in the end.

When I reproject what is going to be my X and Y, the position of my camera?

SimpleVector dir=Interact2D.reproject2D3DWS(cam, fb, (int)cam.getXAxis().x, (int)cam.getXAxis().y);

and who I pickup the object?

Object3D picked=res[1] ????

this is the code I'm using:

Code: [Select]
public void onDrawFrame(GL10 gl) {

try {
if (!stop) {
Camera cam = world.getCamera();

SimpleVector dir=Interact2D.reproject2D3DWS(cam, fb, (int)cam.getXAxis().x, (int)cam.getXAxis().y);
dir.matMul(world.getCamera().getBack().invert3x3());
dir.add(world.getCamera().getPosition());  

if (turn != 0) {
world.getCamera().rotateY(-turn);
}

if (touchTurn != 0) {
world.getCamera().rotateY(touchTurn);
touchTurn = 0;
}

if (touchTurnUp != 0) {
world.getCamera().rotateX(touchTurnUp);
touchTurnUp = 0;
}

if (move != 0) {
world.getCamera().moveCamera(cam.getDirection(), move);
}

Object[] res=world.calcMinDistanceAndObject3D(cam.getPosition(), dir, 10000 /*or whatever*/);
if(res[1]!=null)
Object3D picked=res[1]; //??????????????? I'm confuse

fb.clear(back);
world.renderScene(fb);
world.draw(fb);
blitNumber(lfps, 5, 5);

fb.display();

if (box != null) {
box.rotateX(0.01f);
}

if (sun != null) {
sun.rotate(sunRot, plane.getTransformedCenter());
}

if (System.currentTimeMillis() - time >= 1000) {
lfps = fps;
fps = 0;
time = System.currentTimeMillis();
}
fps++;
} else {
if (fb != null) {
fb.dispose();
fb = null;
}
}
} catch (Exception e) {
e.printStackTrace();
Logger.log("Drawing thread terminated!", Logger.MESSAGE);
}

Hope anyone can give me some light.

Cheers
Joao
« Last Edit: December 21, 2010, 12:17:10 pm by jmachete »

Offline raft

  • quad
  • ******
  • Posts: 1966
    • View Profile
    • http://www.aptalkarga.com
Re: How to do 2D click to 3D object picking?
« Reply #20 on: December 21, 2010, 02:25:38 pm »
you need to cast the result to Object3D.

Code: [Select]
if(res[1]!=null)
   Object3D picked=(Object3D) res[1];

Offline jmachete

  • byte
  • *
  • Posts: 2
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #21 on: December 21, 2010, 02:58:26 pm »
But from where I can get the X and Y values from:

SimpleVector dir=Interact2D.reproject2D3DWS(cam, fb, x, y);


??


Offline raft

  • quad
  • ******
  • Posts: 1966
    • View Profile
    • http://www.aptalkarga.com
Re: How to do 2D click to 3D object picking?
« Reply #22 on: December 21, 2010, 03:13:24 pm »
x, y are the screen coordinates that you want to pick the Object3D behind. it depends on your application but typically the coordinates user touched at screen. something like this:

override onTouchEvent in your activity
Code: [Select]
int screenX = -1, screenY = -1;

@Override
public boolean onTouchEvent(MotionEvent event) {
   switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
            screenX = (int) event.getX();
            screenY = (int) event.getY();
            break;
        }
case MotionEvent.ACTION_UP: {
            screenX = -1;
            break;
        }
}

then in your onDrawFrame method:
Code: [Select]
if (screenX != -1) {
   SimpleVector dir=Interact2D.reproject2D3DWS(cam, fb, screenX, screenY);
   //...
}

Offline PaldxD

  • byte
  • *
  • Posts: 25
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #23 on: February 22, 2011, 02:04:06 pm »
Why this never work?
i set all collisions right, and res[1] is null ever
:(

Code: [Select]
{
if (xpos != -1) {
SimpleVector dir = Interact2D.reproject2D3DWS(cam,
fb, (int) xpos, (int) ypos);
dir.matMul(world.getCamera().getBack().invert3x3());
dir.add(world.getCamera().getPosition());
Object[] res = world.calcMinDistanceAndObject3D(
cam.getPosition(), dir, 10000);
if (res[1] != null)
player_Corpo.setAdditionalColor(RGBColor.WHITE);
else
player_Corpo.setAdditionalColor(RGBColor.BLUE);
}
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #24 on: February 22, 2011, 02:10:41 pm »
Because you are mixing things there. You are doing a kind of multiple world space transformation, which makes no sense. Have a look at this thread: http://www.jpct.net/forum2/index.php/topic,1932.0.html

Offline PaldxD

  • byte
  • *
  • Posts: 25
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #25 on: February 22, 2011, 02:30:18 pm »
tried that too.
i wanna make a mp3 player with jpct-ae, and i need check if the click has in player or in pauser for example.


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #26 on: February 22, 2011, 02:39:16 pm »
Well, "tried that too" is a bit vague. The method definitely works fine. If it doesn't work for you, you did something wrong...but that's impossible to tell given the information you provided. Do you have a simple test case of something?

Offline PaldxD

  • byte
  • *
  • Posts: 25
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #27 on: February 22, 2011, 03:12:25 pm »
 i'm really dumb, works fine thx.

Offline SpiRaiL

  • byte
  • *
  • Posts: 1
    • View Profile
Re: How to do 2D click to 3D object picking?
« Reply #28 on: April 13, 2011, 11:45:29 pm »
ok, so i followed the whole thread and have it working as expected.

Now i want to do things with the object that i picked, but i have many objects.

I have a class "box" and i extend the object3D class.

and then i pick one. But i want to be able to drag that specific box around the screen.
If i to getID on a box it has the id = 0;

but when i cast the result for the picker, and do getID it has the id 1.
Should that make sense?

Really, i want to "pick" my box class, not the object in the super class (im not a Java expert, so maybe i dont know what im talking about).

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: How to do 2D click to 3D object picking?
« Reply #29 on: April 14, 2011, 12:04:50 am »
That's most likely because your extended class uses another instance of Object3D in the constructor to create the box. So this first instance gets 0 and yours gets 1, as it's the next Object3D being created. What's the actual problem with this behaviour?