Author Topic: Object picking - weird behavior  (Read 2811 times)

Offline Parezator

  • byte
  • *
  • Posts: 6
    • View Profile
Object picking - weird behavior
« on: September 09, 2012, 09:38:20 pm »
Hi,
I've read the posts about picking, but i can't get the picking to work properly.
In onChangeSurface i add a cube (like in HelloWorld)
Code: [Select]
cube = Primitives.getCube(10);
cube.calcTextureWrapSpherical();
cube.setTexture("texture");
//cube.translate(0, 0, 0);
cube.strip();
cube.build();
cube.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

world.addObject(cube);

In onTouchEvent i am trying to rotate this cube when picked using the code mentioned in almost all topics
Code: [Select]
public boolean onTouchEvent(MotionEvent me) {


xpos = me.getX();
ypos = me.getY();

SimpleVector dir=Interact2D.reproject2D3DWS(world.getCamera(), fb, (int)xpos, (int)ypos).normalize();
Logger.log("touch: x" + xpos + " y"+ypos);
Object[] res=world.calcMinDistanceAndObject3D(world.getCamera().getPosition(), dir, 100000);
Logger.log(""+res[0]);
if(res[1] != null)
{
Logger.log("picked");
Object3D obj = (Object3D) res[1];
obj.rotateX(50);

}
I tried to edit the ignoreIfLarger parameter and collideOffset - Not a significant difference

Problem: The bottom half of visible part of the cube is unpickable (it doesnt response to touches) and it responses even to touches above the cube.
Has anyoned faced this problem or see the solution?
Im using SE X10 mini
« Last Edit: September 10, 2012, 07:35:46 pm by Parezator »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object picking - weird behavior
« Reply #1 on: September 09, 2012, 11:09:21 pm »
I'm not sure if this causes the problem, but its not advised to execute code that deals with jPCT related stuff directly in the event handler methods, because it might interfere with the rendering thread which can cause all kinds of funny side effects. Try to set some flags only in the event handlers and evaluate them in onDrawFrame instead.

Offline Parezator

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Object picking - weird behavior
« Reply #2 on: September 10, 2012, 04:15:27 pm »
Thanks for quick answer.
I tried your idea but nothing changed. I just cant see what i'm doing wrong. I'll keep looking...
Maybe it could be solved just by some "offsetting" the ypos but i dont think this is the right attitude (and propably it wouldnt work on all resolutions).

The problem is propably in collision detection setting because when i placed there another cube falling on top of the first one, it didnt work properly (didnt recognize collision at all).
Code: [Select]
//onsurfacechanged
//...
cube = Primitives.getCube(10);
cube.calcTextureWrapSpherical();
cube.setTexture("texture");
cube.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
cube.strip();
cube.build();
cube.rotateY(0.7853f);


world.addObject(cube);


cube2 = Primitives.getCube(10);
cube2.calcTextureWrapSpherical();
cube2.setTexture("texture");
cube2.translate(0, -50, 0);
cube2.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

cube2.strip();
cube2.build();
cube2.rotateY(0.7853f);
//...
//on drawframe
//...  ellipsoid - tried with different values - no change
cube2.translate(0, 0.08f, 0);
SimpleVector posun = cube2.checkForCollisionEllipsoid(new SimpleVector(0, 0.08f,0), ellipsoid, 8);
/* if(cube2.checkForCollision(new SimpleVector(0, 1,0), 10) != Object3D.NO_OBJECT)
{
cube2.translate(0, -0.5f, 0);
}*/
cube2.translate(posun);
It just passes throught the first one.

Quote
its not advised to execute code that deals with jPCT related stuff directly in the event handler methods, because it might interfere with the rendering thread
So i'm not able to implement game logic (for example moving a car forward) in separate thread? (to keep game speed fps independent)
Sorry for my english, if there is anything you dont understand i'll try to make myself clear.
« Last Edit: September 10, 2012, 04:17:52 pm by Parezator »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object picking - weird behavior
« Reply #3 on: September 10, 2012, 05:32:46 pm »
Just print out your touch coordinates to check if 0,0 really is where you think it is. The title bar for example introduces some offset, so that the origin of the framebuffer isn't the first visible pixel in the upper left. Maybe you are just fooled by that. About the threading...you can do it, if you synchronize it properly, but i wouldn't advise to do it that way. Frame rate independant game logic is better done by taking the elapsed ticks or time into account, not by realing on some unpredictable and non-deterministic logic thread.

Offline Parezator

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Object picking - weird behavior
« Reply #4 on: September 10, 2012, 07:35:27 pm »
Thanks! It was the title bar. Now i feel really stupid.
Ill take your advice about the threading.
Now I have figured out the problem with collision detection, so im really looking forward to make something meaningful with this engine.