Ray picking (im doing something wrong but im not sure what)

Started by lawless_c, June 23, 2015, 03:00:53 PM

Previous topic - Next topic

lawless_c

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.


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



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?


lawless_c

yup that was it, removed the titlebar and all my touches became perfectly accurate.