private void ensureInBounds() { if (bounds == null) { bounds = new Rect(0, 0, width, height); } float xMin = bounds.x + radius; float xMax = bounds.w + bounds.x - radius; float yMin = bounds.y + radius; float yMax = (bounds.h + bounds.y) - radius; if (pos.x < xMin) { pos.x = xMin; vel.x *= COLLISION_SCALE; } else if (pos.x > xMax) { pos.x = xMax; vel.x *= COLLISION_SCALE; } if (pos.y < yMin) { pos.y = yMin; vel.y *= COLLISION_SCALE; } else if (pos.y > yMax) { pos.y = yMax; vel.y *= COLLISION_SCALE; } Float p1 = new Float(pos.x); Float p2 = new Float(pos.y); Float v1 = new Float(vel.x); Float v2 = new Float(vel.y); // If anything is NaN -- make new Point and Velocity if (p1.isNaN(p1) || p2.isNaN(p2) || v1.isNaN(v1) || v2.isNaN(v2)) { pos = new Point(random(width), random(height)); // Place new point randomly vel = new Vector(0.0f, 0.0f); // Start it out with no movement } }