public void changeSpeed(SmartblobTri blob, float secondsSinceLastCall) { float minSq = minSpeed * minSpeed; float maxSq = maxSpeed * maxSpeed; for (MovCorner c : blob.allMovCorners()) { float dy = c.speedY, dx = c.speedX; float speedSq = dy * dy + dx * dx; if (minSq <= speedSq && speedSq <= maxSq) continue; if (speedSq == 0) continue; // rare. it will be pushed on by springs and probably have nonzero speed next // time. float speed = (float) Math.sqrt(speedSq); float newSpeed = Math.max(minSpeed, Math.min(speed, maxSpeed)); float mult = newSpeed / speed; c.speedY *= mult; c.speedX *= mult; } }
public void changeSpeed(LayeredZigzag blob, float secondsSinceLastCall) { for (MovLine ld : blob.allLineDatas()) { if (ld.springTightness == 0) { // This is an optimization, not a behavior change, because force is multiplied by // springTightness continue; } MovCorner a = ld.adjacentCorners.get(0), b = ld.adjacentCorners.get(1); float dy = b.y - a.y; float dx = b.x - a.x; float distance = (float) Math.sqrt(dx * dx + dy * dy); if (distance == 0) continue; float wantToAddToDistance = ld.targetDistance - distance; // positive or negative // wantToAddToDistance = // (float)(2*MathUtil.sigmoid(5*wantToAddToDistance/ld.targetDistance)-1)*ld.targetDistance; wantToAddToDistance = (float) (2 * MathUtil.sigmoid(multParamOfSigmoid * wantToAddToDistance / ld.targetDistance) - 1); float normDy = dy / distance, normDx = dx / distance; float addToEachMomentum = secondsSinceLastCall * wantToAddToDistance * mult * ld.springTightness; float addToMomentumY = normDy * addToEachMomentum; float addToMomentumX = normDx * addToEachMomentum; b.addToSpeedY += addToMomentumY / b.mass; a.addToSpeedY -= addToMomentumY / a.mass; b.addToSpeedX += addToMomentumX / b.mass; a.addToSpeedX -= addToMomentumX / a.mass; } }