Object Selector

Started by goodm, March 19, 2012, 06:58:00 PM

Previous topic - Next topic

goodm

I looking for some method to select object by touch it?
I know that I need some loop through all objects to check is current place what MotionEvent pointing is not in collision detect with some object.
I already have method which return a SimpleVector from 2D touch surface.

Only question is how to check if this vector is not in collision with some object.

EgonOlsen

You can add a collision listener to your objects, enable the proper collision modes and do a World.calcMinDistance with your SimpleVector. That will trigger the listener telling you which object has been hit. Search the forum for mouse picking and you should find more about this topic.

goodm

Can you provide any small example? Thanks.

Thomas.

#3
You can see it in this physics example in checkInteraction() method, there is complete code for get object by touch on screen and move with it by moving of finger on the screen...

edit: you have to just replace body.setLinearVelocity... method to object.translate...

kburden000

#4
Here's a routine that might be a little more pluggable.
private int selectAnyObjectAt( int mouseX, int mouseY){
SimpleVector ray=Interact2D.reproject2D3DWS(mCamera, mFrameBuffer, mouseX, mouseY).normalize();
Object[] res = mWorld.calcMinDistanceAndObject3D(mCamera.getPosition(), ray, 10000F);
if (res==null || res[1] == null || res[0] == (Object)Object3D.RAY_MISSES_BOX) {
Log.d("SELECTION", "You missed! x="+mouseX+" y="+mouseY);
selectedObject = null;
return -1;
}
Object3D obj = (Object3D)res[1];
Log.d("SELECTION", "x="+mouseX+" y="+mouseY+" id2="+obj.getID()+" name="+obj.getName());
selectedObject = obj;
return obj.getID();
}


This just gets you the selected object. It's up to you to figure how to move it about, based on the physics and layout of your world.