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

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

Previous topic - Next topic

Ulrick

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

EgonOlsen

result is the return value of which method here?

Ulrick

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

EgonOlsen

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:
QuoteIt's the only one that is available in jPCT-AE
.

jmachete

#19
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:


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

raft

you need to cast the result to Object3D.

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

jmachete

But from where I can get the X and Y values from:

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


??


raft

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

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:
if (screenX != -1) {
   SimpleVector dir=Interact2D.reproject2D3DWS(cam, fb, screenX, screenY);
   //...
}


PaldxD

Why this never work?
i set all collisions right, and res[1] is null ever
:(

{
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);
}
}

EgonOlsen

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

PaldxD

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.


EgonOlsen

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?

PaldxD


SpiRaiL

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).

EgonOlsen

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?