Author Topic: Object Selector  (Read 3605 times)

Offline goodm

  • byte
  • *
  • Posts: 3
    • View Profile
Object Selector
« on: March 19, 2012, 06:58:00 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object Selector
« Reply #1 on: March 19, 2012, 08:25:35 pm »
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.

Offline goodm

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Object Selector
« Reply #2 on: March 20, 2012, 01:51:10 pm »
Can you provide any small example? Thanks.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Object Selector
« Reply #3 on: March 20, 2012, 02:01:49 pm »
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...
« Last Edit: March 20, 2012, 02:04:57 pm by Thomas. »

Offline kburden000

  • byte
  • *
  • Posts: 7
    • View Profile
Re: Object Selector
« Reply #4 on: March 23, 2012, 05:01:21 pm »
Here's a routine that might be a little more pluggable.
Code: [Select]
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.
« Last Edit: March 23, 2012, 05:18:33 pm by kburden000 »