use of spacegraph.space2d.phys.collision.AABB in project narchy by automenta.
the class DynamicTreeFlatNodes method expandBuffers.
private void expandBuffers(int oldSize, int newSize) {
m_aabb = BufferUtils.reallocateBuffer(AABB.class, m_aabb, oldSize, newSize);
m_userData = BufferUtils.reallocateBuffer(Object.class, m_userData, oldSize, newSize);
m_parent = BufferUtils.reallocateBuffer(m_parent, oldSize, newSize);
m_child1 = BufferUtils.reallocateBuffer(m_child1, oldSize, newSize);
m_child2 = BufferUtils.reallocateBuffer(m_child2, oldSize, newSize);
m_height = BufferUtils.reallocateBuffer(m_height, oldSize, newSize);
// Build a linked list for the free list.
for (int i = oldSize; i < newSize; i++) {
m_aabb[i] = new AABB();
m_parent[i] = (i == newSize - 1) ? NULL_NODE : i + 1;
m_height[i] = -1;
m_child1[i] = -1;
m_child2[i] = -1;
}
m_freeList = oldSize;
}
use of spacegraph.space2d.phys.collision.AABB in project narchy by automenta.
the class DynamicTreeFlatNodes method createProxy.
@Override
public final int createProxy(final AABB aabb, Object userData) {
final int node = allocateNode();
// Fatten the aabb
final AABB nodeAABB = m_aabb[node];
nodeAABB.lowerBound.x = aabb.lowerBound.x - Settings.aabbExtension;
nodeAABB.lowerBound.y = aabb.lowerBound.y - Settings.aabbExtension;
nodeAABB.upperBound.x = aabb.upperBound.x + Settings.aabbExtension;
nodeAABB.upperBound.y = aabb.upperBound.y + Settings.aabbExtension;
m_userData[node] = userData;
insertLeaf(node);
return node;
}
use of spacegraph.space2d.phys.collision.AABB in project narchy by automenta.
the class DynamicTreeFlatNodes method raycast.
@Override
public void raycast(TreeRayCastCallback callback, RayCastInput input) {
final Tuple2f p1 = input.p1;
final Tuple2f p2 = input.p2;
float p1x = p1.x, p2x = p2.x, p1y = p1.y, p2y = p2.y;
float vx, vy;
float rx, ry;
float absVx, absVy;
float cx, cy;
float hx, hy;
float tempx, tempy;
r.x = p2x - p1x;
r.y = p2y - p1y;
assert ((r.x * r.x + r.y * r.y) > 0f);
r.normalize();
rx = r.x;
ry = r.y;
// v is perpendicular to the segment.
vx = -1f * ry;
vy = 1f * rx;
absVx = Math.abs(vx);
absVy = Math.abs(vy);
// Separating axis for segment (Gino, p80).
// |dot(v, p1 - c)| > dot(|v|, h)
float maxFraction = input.maxFraction;
// Build a bounding box for the segment.
final AABB segAABB = aabb;
// Vec2 t = p1 + maxFraction * (p2 - p1);
// before inline
// temp.set(p2).subLocal(p1).mulLocal(maxFraction).addLocal(p1);
// Vec2.minToOut(p1, temp, segAABB.lowerBound);
// Vec2.maxToOut(p1, temp, segAABB.upperBound);
tempx = (p2x - p1x) * maxFraction + p1x;
tempy = (p2y - p1y) * maxFraction + p1y;
segAABB.lowerBound.x = p1x < tempx ? p1x : tempx;
segAABB.lowerBound.y = p1y < tempy ? p1y : tempy;
segAABB.upperBound.x = p1x > tempx ? p1x : tempx;
segAABB.upperBound.y = p1y > tempy ? p1y : tempy;
// end inline
nodeStackIndex = 0;
nodeStack[nodeStackIndex++] = m_root;
while (nodeStackIndex > 0) {
int node = nodeStack[--nodeStackIndex] = m_root;
if (node == NULL_NODE) {
continue;
}
final AABB nodeAABB = m_aabb[node];
if (!AABB.testOverlap(nodeAABB, segAABB)) {
continue;
}
// Separating axis for segment (Gino, p80).
// |dot(v, p1 - c)| > dot(|v|, h)
// node.aabb.getCenterToOut(c);
// node.aabb.getExtentsToOut(h);
cx = (nodeAABB.lowerBound.x + nodeAABB.upperBound.x) * .5f;
cy = (nodeAABB.lowerBound.y + nodeAABB.upperBound.y) * .5f;
hx = (nodeAABB.upperBound.x - nodeAABB.lowerBound.x) * .5f;
hy = (nodeAABB.upperBound.y - nodeAABB.lowerBound.y) * .5f;
tempx = p1x - cx;
tempy = p1y - cy;
float separation = Math.abs(vx * tempx + vy * tempy) - (absVx * hx + absVy * hy);
if (separation > 0.0f) {
continue;
}
int child1 = m_child1[node];
if (child1 == NULL_NODE) {
subInput.p1.x = p1x;
subInput.p1.y = p1y;
subInput.p2.x = p2x;
subInput.p2.y = p2y;
subInput.maxFraction = maxFraction;
float value = callback.raycastCallback(subInput, node);
if (value == 0.0f) {
// The client has terminated the ray cast.
return;
}
if (value > 0.0f) {
// Update segment bounding box.
maxFraction = value;
// temp.set(p2).subLocal(p1).mulLocal(maxFraction).addLocal(p1);
// Vec2.minToOut(p1, temp, segAABB.lowerBound);
// Vec2.maxToOut(p1, temp, segAABB.upperBound);
tempx = (p2x - p1x) * maxFraction + p1x;
tempy = (p2y - p1y) * maxFraction + p1y;
segAABB.lowerBound.x = p1x < tempx ? p1x : tempx;
segAABB.lowerBound.y = p1y < tempy ? p1y : tempy;
segAABB.upperBound.x = p1x > tempx ? p1x : tempx;
segAABB.upperBound.y = p1y > tempy ? p1y : tempy;
}
} else {
nodeStack[nodeStackIndex++] = child1;
nodeStack[nodeStackIndex++] = m_child2[node];
}
}
}
use of spacegraph.space2d.phys.collision.AABB in project narchy by automenta.
the class DynamicTreeFlatNodes method validateMetrics.
private void validateMetrics(int node) {
if (node == NULL_NODE) {
return;
}
int child1 = m_child1[node];
int child2 = m_child2[node];
if (child1 == NULL_NODE) {
assert (child1 == NULL_NODE);
assert (child2 == NULL_NODE);
assert (m_height[node] == 0);
return;
}
assert (child1 != NULL_NODE && 0 <= child1 && child1 < m_nodeCapacity);
assert (child2 != child1 && 0 <= child2 && child2 < m_nodeCapacity);
int height1 = m_height[child1];
int height2 = m_height[child2];
int height;
height = 1 + MathUtils.max(height1, height2);
assert (m_height[node] == height);
AABB aabb = new AABB();
aabb.combine(m_aabb[child1], m_aabb[child2]);
assert (aabb.lowerBound.equals(m_aabb[node].lowerBound));
assert (aabb.upperBound.equals(m_aabb[node].upperBound));
validateMetrics(child1);
validateMetrics(child2);
}
use of spacegraph.space2d.phys.collision.AABB in project narchy by automenta.
the class DynamicTreeFlatNodes method drawTree.
public void drawTree(DebugDraw argDraw, int node, int spot, int height) {
AABB a = m_aabb[node];
a.getVertices(drawVecs);
color.set(1, (height - spot) * 1f / height, (height - spot) * 1f / height);
argDraw.drawPolygon(drawVecs, 4, color);
argDraw.getViewportTranform().getWorldToScreen(a.upperBound, textVec);
argDraw.drawString(textVec.x, textVec.y, node + "-" + (spot + 1) + '/' + height, color);
int c1 = m_child1[node];
int c2 = m_child2[node];
if (c1 != NULL_NODE) {
drawTree(argDraw, c1, spot + 1, height);
}
if (c2 != NULL_NODE) {
drawTree(argDraw, c2, spot + 1, height);
}
}
Aggregations