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?
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)
yup that was it, removed the titlebar and all my touches became perfectly accurate.