List of usage examples for com.badlogic.gdx.utils Bits get
public boolean get(int index)
From source file:com.badlogic.ashley.core.Family.java
License:Apache License
private static String getBitsString(Bits bits) { StringBuilder stringBuilder = new StringBuilder(); int numBits = bits.length(); for (int i = 0; i < numBits; ++i) { stringBuilder.append(bits.get(i) ? "1" : "0"); }/* ww w. j a v a 2 s .c o m*/ return stringBuilder.toString(); }
From source file:com.mygdx.game.pathfinding.NavMesh.java
License:Apache License
/** * Get a random triangle on the navigation mesh, on any of the allowed mesh parts. * The probability distribution is even in world space, as opposed to triangle index, * meaning large triangles will be chosen more often than small ones. * <p/>//from w ww . jav a 2s .c o m * Example usage, to get a random point on the second navigation mesh part: * allowedMeshParts.clear(); * allowedMeshParts.set(1); * Triangle randomTri = navmesh.getRandomTriangle(allowedMeshParts); * Vector3 randomPoint = new Vector3(); * randomTri.getRandomPoint(randomPoint); * * @param allowedMeshParts Bits representing allowed mesh part indices. * @return A random triangle. */ public Triangle getRandomTriangle(Bits allowedMeshParts) { tmpFloatArrayGetRandomTriangle.clear(); tmpFloatArrayGetRandomTriangle.ordered = true; tmpTriArrayGetRandomTriangle.clear(); tmpTriArrayGetRandomTriangle.ordered = true; // To get a uniform distribution over the triangles in the mesh parts // we must take areas of the triangles into account. for (int mpIndex = 0; mpIndex < graph.getMeshPartCount(); mpIndex++) { if (allowedMeshParts.get(mpIndex)) { for (int triIndex = 0; triIndex < graph.getTriangleCount(mpIndex); triIndex++) { Triangle tri = graph.getTriangleFromMeshPart(mpIndex, triIndex); float integratedArea = 0; if (tmpFloatArrayGetRandomTriangle.size > 0) { integratedArea = tmpFloatArrayGetRandomTriangle .get(tmpFloatArrayGetRandomTriangle.size - 1); } tmpFloatArrayGetRandomTriangle.add(integratedArea + tri.area()); tmpTriArrayGetRandomTriangle.add(tri); } } } if (tmpFloatArrayGetRandomTriangle.size == 0) { return null; } float r = MathUtils.random(0f, tmpFloatArrayGetRandomTriangle.get(tmpFloatArrayGetRandomTriangle.size - 1)); int i; for (i = 0; i < tmpFloatArrayGetRandomTriangle.size; i++) { if (r <= tmpFloatArrayGetRandomTriangle.get(i)) { break; } } return tmpTriArrayGetRandomTriangle.get(i); }
From source file:com.mygdx.game.pathfinding.NavMesh.java
License:Apache License
/** * Ray tests the navmesh along up/down axis, if no triangles are found, it makes an * exhaustive search of all triangles on the navmesh. * * TODO: Exhaustive search is somewhat expensive depending on amount of * triangles in navmesh, maybe something like quadtrees can be used? * * @param fromPoint Test point/* w w w .ja v a 2s.c o m*/ * @param closestPoint Output for closest point on closest triangle * @param allowedMeshParts Indices of which mesh parts to ray test. If null, do only an exhaustive search. * @return The closest triangle */ public Triangle getClosestTriangle(Vector3 fromPoint, Vector3 closestPoint, Bits allowedMeshParts) { Triangle fromTri = null; float minDst2 = Float.POSITIVE_INFINITY; if (allowedMeshParts != null) { for (int meshPartIndex = 0; meshPartIndex < graph.getMeshPartCount(); meshPartIndex++) { if (!allowedMeshParts.get(meshPartIndex)) { continue; } Triangle tri = verticalRayTest(fromPoint, tmpGetClosestTriangle, meshPartIndex); float dst2 = fromPoint.dst2(tmpGetClosestTriangle); if (dst2 < minDst2) { minDst2 = dst2; fromTri = tri; closestPoint.set(tmpGetClosestTriangle); } } } // Exhaustive scan through all the tris to find the closest tri and point if (fromTri == null) { for (int i = 0; i < graph.getNodeCount(); i++) { Triangle tri = graph.getTriangleFromGraphIndex(i); float dst2 = GeometryUtils.getClosestPointOnTriangle(tri.a, tri.b, tri.c, fromPoint, tmpGetClosestTriangle); if (dst2 < minDst2) { minDst2 = dst2; fromTri = tri; closestPoint.set(tmpGetClosestTriangle); } } } return fromTri; }
From source file:com.mygdx.game.pathfinding.NavMeshGraph.java
License:Apache License
/** * Get an array of the vertex indices from the mesh. Any vertices which share the same position will be counted * as a single vertex and share the same index. That is, position duplicates will be filtered out. * * @param mesh/*from w w w . jav a 2s . c o m*/ * @return */ private static short[] getUniquePositionVertexIndices(Mesh mesh) { FloatBuffer verticesBuffer = mesh.getVerticesBuffer(); int positionOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4; // Number of array elements which make up a vertex int vertexSize = mesh.getVertexSize() / 4; // The indices tell us which vertices are part of a triangle. short[] indices = new short[mesh.getNumIndices()]; mesh.getIndices(indices); // Marks true if an index has already been compared to avoid unnecessary comparisons Bits handledIndices = new Bits(mesh.getNumIndices()); for (int i = 0; i < indices.length; i++) { short indexI = indices[i]; if (handledIndices.get(indexI)) { // Index handled in an earlier iteration continue; } int vBufIndexI = indexI * vertexSize + positionOffset; float xi = verticesBuffer.get(vBufIndexI++); float yi = verticesBuffer.get(vBufIndexI++); float zi = verticesBuffer.get(vBufIndexI++); for (int j = i + 1; j < indices.length; j++) { short indexJ = indices[j]; int vBufIndexJ = indexJ * vertexSize + positionOffset; float xj = verticesBuffer.get(vBufIndexJ++); float yj = verticesBuffer.get(vBufIndexJ++); float zj = verticesBuffer.get(vBufIndexJ++); if (xi == xj && yi == yj && zi == zj) { indices[j] = indexI; } } handledIndices.set(indexI); } return indices; }