use of spacegraph.util.math.Tuple2f in project narchy by automenta.
the class ChainShape method createLoop.
/**
* Create a loop. This automatically adjusts connectivity.
*
* @param vertices an array of vertices, these are copied
* @param count the vertex count
*/
public void createLoop(final Tuple2f[] vertices, int count) {
assert (m_vertices == null && m_count == 0);
assert (count >= 3);
m_count = count + 1;
m_vertices = new Tuple2f[m_count];
for (int i = 1; i < count; i++) {
Tuple2f v1 = vertices[i - 1];
Tuple2f v2 = vertices[i];
// If the code crashes here, it means your vertices are too close together.
if (MathUtils.distanceSquared(v1, v2) < Settings.linearSlop * Settings.linearSlop) {
throw new RuntimeException("Vertices of chain shape are too close together");
}
}
for (int i = 0; i < count; i++) {
m_vertices[i] = new Vec2(vertices[i]);
}
m_vertices[count] = new Vec2(m_vertices[0]);
m_prevVertex.set(m_vertices[m_count - 2]);
m_nextVertex.set(m_vertices[1]);
m_hasPrevVertex = true;
m_hasNextVertex = true;
}
use of spacegraph.util.math.Tuple2f in project narchy by automenta.
the class EdgeShape method computeAABB.
@Override
public void computeAABB(AABB aabb, Transform xf, int childIndex) {
final Tuple2f lowerBound = aabb.lowerBound;
final Tuple2f upperBound = aabb.upperBound;
final Rot xfq = xf;
final float v1x = (xfq.c * m_vertex1.x - xfq.s * m_vertex1.y) + xf.pos.x;
final float v1y = (xfq.s * m_vertex1.x + xfq.c * m_vertex1.y) + xf.pos.y;
final float v2x = (xfq.c * m_vertex2.x - xfq.s * m_vertex2.y) + xf.pos.x;
final float v2y = (xfq.s * m_vertex2.x + xfq.c * m_vertex2.y) + xf.pos.y;
lowerBound.x = v1x < v2x ? v1x : v2x;
lowerBound.y = v1y < v2y ? v1y : v2y;
upperBound.x = v1x > v2x ? v1x : v2x;
upperBound.y = v1y > v2y ? v1y : v2y;
lowerBound.x -= radius;
lowerBound.y -= radius;
upperBound.x += radius;
upperBound.y += radius;
}
use of spacegraph.util.math.Tuple2f in project narchy by automenta.
the class PolygonShape method computeAABB.
@Override
public final void computeAABB(final AABB aabb, final Transform xf, int childIndex) {
final Tuple2f lower = aabb.lowerBound;
final Tuple2f upper = aabb.upperBound;
final Tuple2f v1 = vertex[0];
final float xfqc = xf.c;
final float xfqs = xf.s;
final float xfpx = xf.pos.x;
final float xfpy = xf.pos.y;
lower.x = (xfqc * v1.x - xfqs * v1.y) + xfpx;
lower.y = (xfqs * v1.x + xfqc * v1.y) + xfpy;
upper.x = lower.x;
upper.y = lower.y;
for (int i = 1; i < vertices; ++i) {
Tuple2f v2 = vertex[i];
// Vec2 v = Mul(xf, m_vertices[i]);
float vx = (xfqc * v2.x - xfqs * v2.y) + xfpx;
float vy = (xfqs * v2.x + xfqc * v2.y) + xfpy;
lower.x = lower.x < vx ? lower.x : vx;
lower.y = lower.y < vy ? lower.y : vy;
upper.x = upper.x > vx ? upper.x : vx;
upper.y = upper.y > vy ? upper.y : vy;
}
lower.x -= radius;
lower.y -= radius;
upper.x += radius;
upper.y += radius;
}
use of spacegraph.util.math.Tuple2f in project narchy by automenta.
the class PolygonShape method computeCentroidToOut.
public final void computeCentroidToOut(final Tuple2f[] vs, final int count, final Vec2 out) {
assert (count >= 3);
out.set(0.0f, 0.0f);
float area = 0.0f;
// pRef is the reference point for forming triangles.
// It's location doesn't change the result (except for rounding error).
final Tuple2f pRef = pool1;
pRef.setZero();
final Tuple2f e1 = pool2;
final Tuple2f e2 = pool3;
final float inv3 = 1.0f / 3.0f;
for (int i = 0; i < count; ++i) {
// Triangle vertices.
final Tuple2f p1 = pRef;
final Tuple2f p2 = vs[i];
final Tuple2f p3 = i + 1 < count ? vs[i + 1] : vs[0];
e1.set(p2).subbed(p1);
e2.set(p3).subbed(p1);
final float D = Tuple2f.cross(e1, e2);
final float triangleArea = 0.5f * D;
area += triangleArea;
// Area weighted centroid
e1.set(p1).added(p2).added(p3).scaled(triangleArea * inv3);
out.addLocal(e1);
}
// Centroid
assert (area > Settings.EPSILON);
out.scaled(1.0f / area);
}
use of spacegraph.util.math.Tuple2f in project narchy by automenta.
the class PolygonShape method validate.
/**
* Validate convexity. This is a very time consuming operation.
*
* @return
*/
public boolean validate() {
for (int i = 0; i < vertices; ++i) {
int i1 = i;
int i2 = i < vertices - 1 ? i1 + 1 : 0;
Tuple2f p = vertex[i1];
Tuple2f e = pool1.set(vertex[i2]).subbed(p);
for (int j = 0; j < vertices; ++j) {
if (j == i1 || j == i2) {
continue;
}
Tuple2f v = pool2.set(vertex[j]).subbed(p);
float c = Tuple2f.cross(e, v);
if (c < 0.0f) {
return false;
}
}
}
return true;
}
Aggregations