www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: lawless_c on June 23, 2015, 03:00:53 pm

Title: Ray picking (im doing something wrong but im not sure what)
Post by: lawless_c on June 23, 2015, 03:00:53 pm
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?
Title: Re: Ray picking (im doing something wrong but im not sure what)
Post by: EgonOlsen on June 23, 2015, 03:20:51 pm
This might help: http://www.jpct.net/forum2/index.php/topic,4181.msg29291.html (http://www.jpct.net/forum2/index.php/topic,4181.msg29291.html)
Title: Re: Ray picking (im doing something wrong but im not sure what)
Post by: lawless_c on June 30, 2015, 06:50:02 pm
yup that was it, removed the titlebar and all my touches became perfectly accurate.