Skip to content

Instantly share code, notes, and snippets.

@ase34
Last active February 6, 2017 20:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ase34/11375298 to your computer and use it in GitHub Desktop.
Save ase34/11375298 to your computer and use it in GitHub Desktop.
bukkit plane/line intersection
/*
* See the whitepaper at https://docs.google.com/file/d/0B9Sf4F-ig8lPUlhOdHZ2ZGtzdnM
*/
public static double determinant(double[][] matrix) {
return matrix[0][0] * matrix[1][1] * matrix[2][2] + matrix[0][1] * matrix[1][2] * matrix[2][0] + matrix[0][2] * matrix[1][0] * matrix[2][1] - matrix[0][2] * matrix[1][1] * matrix[2][0] - matrix[0][1] * matrix[1][0] * matrix[2][2] - matrix[0][0] * matrix[1][2] * matrix[2][1];
}
public static Vector getIntersection(Vector linePoint, Vector lineDirection, Vector planeOrigin, Vector planeXDirection, Vector planeYDirection) {
double[][] coefficients = {
{lineDirection.getX(), -planeXDirection.getX(), -planeYDirection.getX()},
{lineDirection.getY(), -planeXDirection.getY(), -planeYDirection.getY()},
{lineDirection.getZ(), -planeXDirection.getZ(), -planeYDirection.getZ()}
};
double[] solutions = {
planeOrigin.getX() - linePoint.getX(),
planeOrigin.getY() - linePoint.getY(),
planeOrigin.getZ() - linePoint.getZ()
};
double[][] dMatrix = {
{solutions[0], coefficients[0][1], coefficients[0][2]},
{solutions[1], coefficients[1][1], coefficients[1][2]},
{solutions[2], coefficients[2][1], coefficients[2][2]}
};
double[][] sMatrix = {
{coefficients[0][0], solutions[0], coefficients[0][2]},
{coefficients[1][0], solutions[1], coefficients[1][2]},
{coefficients[2][0], solutions[2], coefficients[2][2]}
};
double[][] tMatrix = {
{coefficients[0][0], coefficients[0][1], solutions[0]},
{coefficients[1][0], coefficients[1][1], solutions[1]},
{coefficients[2][0], coefficients[2][1], solutions[2]}
};
double det = determinant(coefficients);
double d = determinant(dMatrix) / det;
double s = determinant(uMatrix) / det;
double t = determinant(vMatrix) / det;
return new Vector(s, t, 0);
}
@blablubbabc
Copy link

Isn't determinant missing + matrix[0][2] * matrix[1][0] * matrix[2][1] ?

@ase34
Copy link
Author

ase34 commented Apr 30, 2014

@blablubbabc Yes, it is actually missing... It's really strange how this works though. Anyway, I really thank you for finding this stupid mistake of mine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment