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

#### lawless_c

• int
• Posts: 91
##### Ray picking (im doing something wrong but im not sure what)
« 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?