Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# knownasilya/gist:3691081

Created Sep 10, 2012
Get axes for triangle and AABB.
 public static Vector3d[] getAABBAxes( Vector3d[] vertices ) { Vector3d[] axes = new Vector3d[ 6 ]; // get vertices Vector3d p0 = vertices[ 0 ]; Vector3d p1 = vertices[ 1 ]; Vector3d p2 = vertices[ 2 ]; Vector3d p3 = vertices[ 3 ]; Vector3d p4 = vertices[ 4 ]; Vector3d p5 = vertices[ 5 ]; Vector3d p6 = vertices[ 6 ]; Vector3d p7 = vertices[ 7 ]; Vector3d edge0 = new Vector3d(); Vector3d edge1 = new Vector3d(); Vector3d edge2 = new Vector3d(); Vector3d edge3 = new Vector3d(); Vector3d edge4 = new Vector3d(); Vector3d edge5 = new Vector3d(); // subtract the two different vertices to get the edge vector edge0.sub( p1, p0 ); edge1.sub( p2, p0 ); edge2.sub( p4, p0 ); edge3.sub( p7, p5 ); edge4.sub( p7, p3 ); edge5.sub( p7, p6 ); // get perpendicular vector/normal // cross-product of two edges that are perpendicular to each other // once for all faces. Vector3d normal0 = new Vector3d(); Vector3d normal1 = new Vector3d(); Vector3d normal2 = new Vector3d(); Vector3d normal3 = new Vector3d(); Vector3d normal4 = new Vector3d(); Vector3d normal5 = new Vector3d(); normal0.cross( edge0, edge2 ); normal0.normalize(); axes[ 0 ] = normal0; normal1.cross( edge2, edge1 ); normal1.normalize(); axes[ 1 ] = normal1; normal2.cross( edge1, edge0 ); normal2.normalize(); axes[ 2 ] = normal2; normal3.cross( edge4, edge5 ); normal3.normalize(); axes[ 3 ] = normal3; normal4.cross( edge4, edge3 ); normal4.normalize(); axes[ 4 ] = normal4; normal5.cross( edge5, edge3 ); normal5.normalize(); axes[ 5 ] = normal5; return axes; } public static Vector3d[] getTriangleAxes( Vector3d[] vertices ) { Vector3d[] axes = new Vector3d[ 4 ]; // get the current vertex Vector3d p0 = vertices[ 0 ]; // get the next vertex Vector3d p1 = vertices[ 1 ]; Vector3d p2 = vertices[ 2 ]; Vector3d edge1 = new Vector3d(); Vector3d edge2 = new Vector3d(); Vector3d edge3 = new Vector3d(); // subtract the two to get the edge vector edge1.sub( p1, p0 ); edge2.sub( p2, p0 ); edge3.sub( p2, p1 ); // 1 Surface normal Vector3d normal = new Vector3d(); normal.cross( p0, p1 ); normal.normalize(); axes[ 0 ] = normal; // 3 Edge normals Vector3d normal1 = new Vector3d(); normal1.cross( edge1, edge2 ); normal1.normalize(); axes[ 1 ] = normal1; Vector3d normal2 = new Vector3d(); normal2.cross( edge1, edge3 ); normal2.normalize(); axes[ 2 ] = normal2; Vector3d normal3 = new Vector3d(); normal3.cross( edge3, edge2 ); normal3.normalize(); axes[ 3 ] = normal3; return axes; }

### knownasilya commented Sep 10, 2012

 I have replaced the getAABBAxes method with a constant variable: private static final Vector3d[] AABB_AXES = { new Vector3d( -1.0, 0.0, 0.0 ), new Vector3d( 0.0, 0.0, -1.0 ), new Vector3d( 0.0, -1.0, 0.0 ) };
to join this conversation on GitHub. Already have an account? Sign in to comment