Hello. For the last two weeks, I've been struggling with one thing... 3D object picking. And I'm near getting it right! Works great when facing front! With a first person style camera, I can go up, down, forward, backward, strafe left, straft right, and it works. Problem is, when I rotate the camera, the other end of the ray that is not the mouse end goes off in another place other than the camera, completely throwing it off! So I'm going to go step by step, and see if you guys can spot what went wrong.
The first step was to normalize the mouse device coordinates, or in my case, touch coordinates:
public static float[] getNormalizedDeviceCoords(float touchX, float touchY){
float[] result = new float[2];
result[0] = (2f * touchX) / Render.camera.screenWidth - 1f;
result[1] = 1f - (2f * touchY) / Render.camera.screenHeight;
return result;
}
which in turn is converted into Homogeneous Clip Coordinates:
float[] homogeneousClipCoords = new float[]{normalizedDeviceCoords[0], normalizedDeviceCoords[1], -1f, 1f};
The next step was to convert this Homogeneous Clip Coordinates into Eye Coordinates:
public static float[] getEyeCoords(float[] clipCoords){
float[] invertedProjection = new float[16];
Matrix.invertM(invertedProjection, 0, Render.camera.projMatrix, 0);
float[] eyeCoords = new float[4];
Matrix.multiplyMV(eyeCoords, 0, invertedProjection, 0 ,clipCoords, 0);
float[] result = new float[]{eyeCoords[0], eyeCoords[1], -1f, 0f};
return result;
}
Next was to convert the Eye Coordinates into World Coordinates and normalize it:
public static float[] getWorldCoords(float[] eyeCoords){
float[] invertedViewMatrix = new float[16];
Matrix.invertM(invertedViewMatrix, 0, Render.camera.viewM, 0);
float[] rayWorld = new float[4];
Matrix.multiplyMV(rayWorld, 0, invertedViewMatrix, 0 ,eyeCoords, 0);
float length = (float)Math.sqrt(rayWorld[0] * rayWorld[0] +
rayWorld[1] * rayWorld[1] +
rayWorld[2] * rayWorld[2]);
if(length != 0){
rayWorld[0] /= length;
rayWorld[1] /= length;
rayWorld[2] /= length;
}
return rayWorld;
}
Putting this all together gives me a method to get the ray direction I need:
public static float[] calculateMouseRay(){
float touchX = MainActivity.touch.x;
float touchY = MainActivity.touch.y;
float[] normalizedDeviceCoords = getNormalizedDeviceCoords(touchX, touchY);
float[] homogeneousClipCoords = new float[]{normalizedDeviceCoords[0], normalizedDeviceCoords[1], -1f, 1f};
float[] eyeCoords = getEyeCoords(homogeneousClipCoords);
float[] worldCoords = getWorldCoords(eyeCoords);
return worldCoords;
}
I then test for the Ray / Sphere intersection using this with double precision:
public static boolean getRaySphereIntersection(float[] rayOrigin, float[] spherePosition, float[] rayDirection, float radius){
double[] v = new double[4];
double[] dir = new double[4];
// Calculate the a, b, c and d coefficients.
// a = (XB-XA)^2 + (YB-YA)^2 + (ZB-ZA)^2
// b = 2 * ((XB-XA)(XA-XC) + (YB-YA)(YA-YC) + (ZB-ZA)(ZA-ZC))
// c = (XA-XC)^2 + (YA-YC)^2 + (ZA-ZC)^2 - r^2
// d = b^2 - 4*a*c
v[0] = (double)rayOrigin[0] - (double)spherePosition[0];
v[1] = (double)rayOrigin[1] - (double)spherePosition[1];
v[2] = (double)rayOrigin[2] - (double)spherePosition[2];
dir[0] = (double)rayDirection[0];
dir[1] = (double)rayDirection[1];
dir[2] = (double)rayDirection[2];
double a = (dir[0] * dir[0]) +
(dir[1] * dir[1]) +
(dir[2] * dir[2]);
double b = (dir[0] * v[0] +
dir[1] * v[1] +
dir[2] * v[2]) * 2.0;
double c = (v[0] * v[0] +
v[1] * v[1] +
v[2] * v[2]) - ((double)radius * (double)radius);
// Find the discriminant.
//double d = (b * b) - c;
double d = (b * b) - (4.0 * a * c);
Log.d("d", String.valueOf(d));
if (d == 0f) {
//one root
}
else if (d > 0f) {
//two roots
double x1 = -b - Math.sqrt(d) / (2.0 * a);
double x2 = -b + Math.sqrt(d) / (2.0 * a);
Log.d("X1 X2", String.valueOf(x1) + ", " + String.valueOf(x2));
if ((x1 >= 0.0) || (x2 >= 0.0)){
return true;
}
if ((x1 < 0.0) || (x2 >= 0.0)){
return true;
}
}
return false;
}
After a week and a half of playing around with this chunk of code, and researching everything I could on google, I found out by sheer accident that the sphere position to use in this method must be the transformed sphere position extracted from the model matrix, not the position itself. Which not one damn tutorial or forum article mentioned! And works great using this. Haven't tested the objects modelView yet though. To visually see the ray, I made a class to draw the 3D line, and noticed that it has no trouble at all with one end being my mouse cursor. The other end, which is the origin, is sort of working. And it only messes up when I rotate left or right as I move around in a FPS style camera. Which brings me to my next point. I have no idea what the ray origin should be for the camera. And I have 4 choices. 3 of them worked but gave me the same results.
Ray Origin Choices:
1. Using just the camera.position.x, camera.position.y, and camera.position.z for my ray origin worked flawlessly straight due to the fact that the ray origin remained in the center of the screen, but messed up when I rotated the camera, and moved off screen as I was rotating. Now theoretically, even if you were facing at an angle, you still are fixated at that point, and the ray origin shouldn't be flying off away from the center of the screen at all. A point is a point after all.
2.Using the cameras model matrix (used for translating and rotating the camera, and later multiplied to the cameras view matrix), specifically -modelMatrix[12], -modelMatrix[13], and -modelMatrix[14] (note I am using negative), basically gave me nearly the same results. Only difference is that camera rotations play a role in the cameras positions. Great facing straight, but the ray origin is no longer centered at different angles.
3.Using the camera's view matrix didn't work at all, positive or negative, using 12, 13, and 14 in the matrix.
4.Using the camera's inverted view matrix (positive invertedViewMatrix[12], invertedViewMatrix[13], and invertedViewMatrix[14]) did work, but gave me what probably seemed like the same results as #2.
So basically, I'm having difficulty getting the other end of the ray, which is the ray origin. Shooting the ray to the mouse pointer was no problem, like I said. With the camera end of the ray being off, it throws off the accuracy a lot at different camera angles other than straight. If anyone has any idea's, please let me know. I'm sure my math is correct. If you need any more information, such as the camera, or how I render the ray, which I don't think is needed, I can show that too. Thanks in advance!