public void test() { AABB copy = blocks.get(0).bounds.copy(); copy.offset(0.025f, 0, 0); if (!copy.collides(blocks.get(1).bounds)) { blocks.get(0).bounds = copy; } }
protected static Collision AABBvsCircle(AABB A, Circle B) { Vector2 AtoB = B.getPosition().cpy().sub(A.getCenter()); float xe = A.getDimensions().x / 2; float ye = A.getDimensions().y / 2; Vector2 closest = new Vector2(MathUtils.clamp(AtoB.x, -xe, xe), MathUtils.clamp(AtoB.y, -ye, ye)); boolean inside = false; if (AtoB.epsilonEquals(closest, (float) 1e-4)) { inside = true; if (Math.abs(AtoB.x) > Math.abs(AtoB.y)) { if (closest.x > 0.0f) { closest.x = xe; } else { closest.x = -xe; } } else { if (closest.y > 0.0f) { closest.y = ye; } else { closest.y = -ye; } } } Vector2 normal = new Vector2(AtoB).sub(closest); float d = normal.len2(); float r = B.radius; // No collision if (d > r * r && !inside) { return null; } d = (float) Math.sqrt(d); float penetration = r + d; if (inside) { normal.set(AtoB).mul(1.0f).nor(); } else { normal.set(AtoB).mul(-1.0f).nor(); } return new Collision(normal, penetration); }
public void Draw(Canvas canvas) { canvas.drawBitmap(ret, pos.x, canvas.getHeight() - (pos.y + scale.y), null); // testing line------------------------------------------------// if (state == STATE.DRAGGING) { paint.setColor(Color.RED); paint.setStrokeWidth(10); // From startPos to current dragging pos canvas.drawLine( startPos.x, canvas.getHeight() - startPos.y, fingerPos.x, canvas.getHeight() - fingerPos.y, paint); } // draw the rubbish------------------------------------------// for (int i = 0; i < totalDragged; ++i) { if (rubbishPile.get(i).GetActive()) rubbishPile.get(i).Draw(canvas); } paint.setARGB(255, 255, 0, 0); paint.setStrokeWidth(120); // how thick you want the text to be in terms of pixel paint.setTextSize(60); canvas.drawText("Active: " + totalDragged, 130, 80, paint); bound.DrawDebug(canvas); }
@Override public boolean intersects(AABB aabb) { if (aabb.getDimensions() != 2) { throw new IllegalArgumentException(); } if (x1 > aabb.getMaximum(0) || x2 < aabb.getMinimum(0)) { return false; } if (y1 > aabb.getMaximum(1) || y2 < aabb.getMinimum(1)) { return false; } return true; }
public AABB getAABB() { if (aabb == null) { aabb = new AABB(minX, minY, maxX, maxY); } else { aabb.set(minX, minY, maxX, maxY); } return aabb; }
/** * *********************************************************************************** Call when * finger started dragging on screen * *********************************************************************************** */ public void StartDrag(float xTouchPos, float yTouchPos, AABB finger) { // if collide and still got rubbish-------------------------------------------------// if (bound.AABB_Det(finger) && totalDragged < rubbishPile.size()) { // increase index----------------------------------------------------// ++totalDragged; fingerPos.Set(xTouchPos, yTouchPos); state = STATE.DRAGGING; dragging = true; } else dragging = false; }
public BroadPhase(AABB worldAABB, PairCallback callback) { if (debugPrint) { System.out.println("BroadPhase()"); } // array initialization m_proxyPool = new Proxy[Settings.maxProxies]; pairBuffer = new BufferedPair[Settings.maxPairs]; m_bounds = new Bound[2][2 * Settings.maxProxies]; m_queryResults = new int[Settings.maxProxies]; for (int i = 0; i < 2 * Settings.maxProxies; i++) { m_bounds[0][i] = new Bound(); m_bounds[1][i] = new Bound(); } for (int i = 0; i < Settings.maxProxies; i++) { pairBuffer[i] = new BufferedPair(); } m_pairManager = new PairManager(); m_pairManager.initialize(this, callback); assert worldAABB.isValid(); m_worldAABB = new AABB(worldAABB); m_proxyCount = 0; Vec2 d = worldAABB.upperBound.sub(worldAABB.lowerBound); m_quantizationFactor = new Vec2(Integer.MAX_VALUE / d.x, Integer.MAX_VALUE / d.y); for (int i = 0; i < Settings.maxProxies - 1; ++i) { m_proxyPool[i] = new Proxy(); m_proxyPool[i].setNext(i + 1); m_proxyPool[i].timeStamp = 0; m_proxyPool[i].overlapCount = INVALID; m_proxyPool[i].userData = null; } m_proxyPool[Settings.maxProxies - 1] = new Proxy(); m_proxyPool[Settings.maxProxies - 1].setNext(PairManager.NULL_PROXY); m_proxyPool[Settings.maxProxies - 1].timeStamp = 0; m_proxyPool[Settings.maxProxies - 1].overlapCount = INVALID; m_proxyPool[Settings.maxProxies - 1].userData = null; m_freeProxy = 0; m_timeStamp = 1; m_queryResultCount = 0; }
/** * Tests the createAABB method. * * @since 3.1.0 */ @Test public void createAABB() { Vector2[] vertices = new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, -1.0), new Vector2(1.0, -1.0)}; Polygon p = new Polygon(vertices); AABB aabb = p.createAABB(Transform.IDENTITY); TestCase.assertEquals(-1.0, aabb.getMinX(), 1.0e-3); TestCase.assertEquals(-1.0, aabb.getMinY(), 1.0e-3); TestCase.assertEquals(1.0, aabb.getMaxX(), 1.0e-3); TestCase.assertEquals(1.0, aabb.getMaxY(), 1.0e-3); // try using the default method AABB aabb2 = p.createAABB(); TestCase.assertEquals(aabb.getMinX(), aabb2.getMinX()); TestCase.assertEquals(aabb.getMinY(), aabb2.getMinY()); TestCase.assertEquals(aabb.getMaxX(), aabb2.getMaxX()); TestCase.assertEquals(aabb.getMaxY(), aabb2.getMaxY()); Transform tx = new Transform(); tx.rotate(Math.toRadians(30.0)); tx.translate(1.0, 2.0); aabb = p.createAABB(tx); TestCase.assertEquals(0.500, aabb.getMinX(), 1.0e-3); TestCase.assertEquals(0.634, aabb.getMinY(), 1.0e-3); TestCase.assertEquals(2.366, aabb.getMaxX(), 1.0e-3); TestCase.assertEquals(2.866, aabb.getMaxY(), 1.0e-3); }
public static boolean testBoxPoint(final AABB box, final float x, final float y) { AABB point = new AABB(1f, 1f); point.update(new Vector(x, y)); return testBoxBox(box, point); }
public static boolean testCirclePoint(final Circle c1, final float x, final float y) { AABB box = new AABB(1f, 1f); box.update(new Vector(x, y)); return testCircleAABB(c1, box); }
public AABB getBoundingBox() { return AABB.fromMinMax(min, max); }
// Call MoveProxy as many times as you like, then when you are done // call Flush to finalized the proxy pairs (for your time step). void moveProxy(int proxyId, AABB aabb) { if (debugPrint) { System.out.println("MoveProxy()"); } if (proxyId == PairManager.NULL_PROXY || Settings.maxProxies <= proxyId) { return; } assert (aabb.isValid()) : "invalid AABB"; int boundCount = 2 * m_proxyCount; Proxy proxy = m_proxyPool[proxyId]; // Get new bound values BoundValues newValues = new BoundValues(); computeBounds(newValues.lowerValues, newValues.upperValues, aabb); // Get old bound values BoundValues oldValues = new BoundValues(); for (int axis = 0; axis < 2; ++axis) { oldValues.lowerValues[axis] = m_bounds[axis][proxy.lowerBounds[axis]].value; oldValues.upperValues[axis] = m_bounds[axis][proxy.upperBounds[axis]].value; } for (int axis = 0; axis < 2; ++axis) { Bound[] bounds = m_bounds[axis]; int lowerIndex = proxy.lowerBounds[axis]; int upperIndex = proxy.upperBounds[axis]; int lowerValue = newValues.lowerValues[axis]; int upperValue = newValues.upperValues[axis]; int deltaLower = lowerValue - bounds[lowerIndex].value; int deltaUpper = upperValue - bounds[upperIndex].value; bounds[lowerIndex].value = lowerValue; bounds[upperIndex].value = upperValue; // // Expanding adds overlaps // // Should we move the lower bound down? if (deltaLower < 0) { int index = lowerIndex; while (index > 0 && lowerValue < bounds[index - 1].value) { Bound bound = bounds[index]; Bound prevBound = bounds[index - 1]; int prevProxyId = prevBound.proxyId; Proxy prevProxy = m_proxyPool[prevBound.proxyId]; ++prevBound.stabbingCount; if (prevBound.isUpper() == true) { if (testOverlap(newValues, prevProxy)) { m_pairManager.addBufferedPair(proxyId, prevProxyId); } ++prevProxy.upperBounds[axis]; ++bound.stabbingCount; } else { ++prevProxy.lowerBounds[axis]; --bound.stabbingCount; } --proxy.lowerBounds[axis]; // b2Swap(*bound, *prevEdge); Bound tmp = new Bound(bound); bound.set(prevBound); prevBound.set(tmp); --index; } } // Should we move the upper bound up? if (deltaUpper > 0) { int index = upperIndex; while (index < boundCount - 1 && bounds[index + 1].value <= upperValue) { Bound bound = bounds[index]; Bound nextBound = bounds[index + 1]; int nextProxyId = nextBound.proxyId; Proxy nextProxy = m_proxyPool[nextProxyId]; ++nextBound.stabbingCount; if (nextBound.isLower() == true) { if (testOverlap(newValues, nextProxy)) { m_pairManager.addBufferedPair(proxyId, nextProxyId); } --nextProxy.lowerBounds[axis]; ++bound.stabbingCount; } else { --nextProxy.upperBounds[axis]; --bound.stabbingCount; } ++proxy.upperBounds[axis]; // b2Swap(*bound, *nextEdge); // wasn't actually swapping! bounds[index] and // bounds[index+1] need to be swapped by VALUE Bound tmp = new Bound(bound); bound.set(nextBound); nextBound.set(tmp); ++index; } } // // Shrinking removes overlaps // // Should we move the lower bound up? if (deltaLower > 0) { int index = lowerIndex; while (index < boundCount - 1 && bounds[index + 1].value <= lowerValue) { Bound bound = bounds[index]; Bound nextBound = bounds[index + 1]; int nextProxyId = nextBound.proxyId; Proxy nextProxy = m_proxyPool[nextProxyId]; --nextBound.stabbingCount; if (nextBound.isUpper()) { if (testOverlap(oldValues, nextProxy)) { m_pairManager.removeBufferedPair(proxyId, nextProxyId); } --nextProxy.upperBounds[axis]; --bound.stabbingCount; } else { --nextProxy.lowerBounds[axis]; ++bound.stabbingCount; } ++proxy.lowerBounds[axis]; // b2Swap(*bound, *nextEdge); // Bound tmp = bound; // bound = nextEdge; // nextEdge = tmp; Bound tmp = new Bound(bound); bound.set(nextBound); nextBound.set(tmp); ++index; } } // Should we move the upper bound down? if (deltaUpper < 0) { int index = upperIndex; while (index > 0 && upperValue < bounds[index - 1].value) { Bound bound = bounds[index]; Bound prevBound = bounds[index - 1]; int prevProxyId = prevBound.proxyId; Proxy prevProxy = m_proxyPool[prevProxyId]; --prevBound.stabbingCount; if (prevBound.isLower() == true) { if (testOverlap(oldValues, prevProxy)) { m_pairManager.removeBufferedPair(proxyId, prevProxyId); } ++prevProxy.lowerBounds[axis]; --bound.stabbingCount; } else { ++prevProxy.upperBounds[axis]; ++bound.stabbingCount; } --proxy.upperBounds[axis]; // b2Swap(*bound, *prevEdge); // Bound tmp = bound; // bound = prevEdge; // prevEdge = tmp; Bound tmp = new Bound(bound); bound.set(prevBound); prevBound.set(tmp); --index; } } } if (s_validate) { validate(); } }