Author Topic: Ray picking (im doing something wrong but im not sure what)  (Read 820 times)

Offline lawless_c

  • int
  • **
  • Posts: 91
    • View Profile
I'm trying to do ray picking, partially on my own as i don't intend to use "minCalDistance" or Object3d's directly

However it only seems to partially work. I can hit objects i tap but the collision does not occur exactly at the point i picked. but seems to be offset vertically.

Code: [Select]
public static SimpleVector[] touch(MotionEvent e,FrameBuffer fb) {

     int x = (int)e.getX();
     int y = (int)e.getY();

             SimpleVector raydirection = Interact2D.reproject2D3DWS(camera, fb, x, y).normalize();
              SimpleVector cameraposition= new SimpleVector(camera.getPosition());

             return sphereIntersect(cameraPosition, ray,object_positions,objectradius);

}






My sphere intersection function



Code: [Select]
public static SimpleVector[] sphereIntersect(SimpleVector rayOrigin,SimpleVector rayDirection, SimpleVector sphereOrigin, float sphereRadius )
 {

        sphereOrigin.sub(rayOrigin);

        double c = (double) sphereOrigin.length();
        double v = (double) sphereOrigin.calcDot(rayDirection);
        double d = sphereRadius * sphereRadius - (c * c - v * v);

        if (d < 0.0) {
            return new SimpleVector[0];
        }
     
        float distance = (float) (v - Math.sqrt(d));
        rayDirection.scalarMul(distance);
        SimpleVector r1 = new SimpleVector(rayDirection.calcAdd(rayOrigin));

        return new SimpleVector[]{r1};
   }


Perhaps it's an issue with how android handles screen coordinates?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net

Offline lawless_c

  • int
  • **
  • Posts: 91
    • View Profile
Re: Ray picking (im doing something wrong but im not sure what)
« Reply #2 on: June 30, 2015, 06:50:02 pm »
yup that was it, removed the titlebar and all my touches became perfectly accurate.