www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: goodm on March 19, 2012, 06:58:00 pm

Title: Object Selector
Post by: goodm 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.
Title: Re: Object Selector
Post by: EgonOlsen 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.
Title: Re: Object Selector
Post by: goodm on March 20, 2012, 01:51:10 pm
Can you provide any small example? Thanks.
Title: Re: Object Selector
Post by: Thomas. on March 20, 2012, 02:01:49 pm
You can see it in this physics example (http://www.jpct.net/forum2/index.php/topic,2622.0.html) 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...
Title: Re: Object Selector
Post by: kburden000 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.