|
|
|
@ -13,6 +13,7 @@ const Arc = { |
|
|
|
|
prevEndX: 0, |
|
|
|
|
prevEndY: 0, |
|
|
|
|
radius: Random.num(100, 200), |
|
|
|
|
speed: 0, |
|
|
|
|
theta: Random.num(RAD.t90, RAD.t360) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -32,9 +33,9 @@ const Arc = { |
|
|
|
|
return arc; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
step: function(arc, bounds, speed) { |
|
|
|
|
step: function(arc, bounds) { |
|
|
|
|
// Ensure constant velocity and theta between 0 and 2π.
|
|
|
|
|
const delta = speed / arc.radius; |
|
|
|
|
const delta = arc.speed / arc.radius; |
|
|
|
|
arc.length -= delta; |
|
|
|
|
|
|
|
|
|
arc.theta += (arc.clockwise ? -delta : +delta); |
|
|
|
@ -99,6 +100,11 @@ const Arc = { |
|
|
|
|
return arc; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
changeSpeed: function(arc, newSpeed) { |
|
|
|
|
arc.speed = newSpeed * 1; |
|
|
|
|
return arc; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
reverse: function(arc) { |
|
|
|
|
arc.clockwise = !arc.clockwise; |
|
|
|
|
|
|
|
|
@ -113,32 +119,44 @@ const Arc = { |
|
|
|
|
return arc; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
match: function(arc, arcToMatch) { |
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
follow: function(arc, arcToFollow) { |
|
|
|
|
if (arc.clockwise !== arcToFollow.clockwise) { |
|
|
|
|
arc = Arc.reverse(arc); |
|
|
|
|
} |
|
|
|
|
arc = (arc.clockwise !== arcToFollow.clockwise ? Arc.reverse(arc) : arc); |
|
|
|
|
|
|
|
|
|
if (Math.abs(arc.theta - arcToFollow.theta) > 0.2) { |
|
|
|
|
arc = Arc.changeRadius(arc, 50); |
|
|
|
|
} else { |
|
|
|
|
arc = Arc.changeRadius(arc, arcToFollow.radius); |
|
|
|
|
} |
|
|
|
|
const prevD = Math.pow( |
|
|
|
|
Math.pow(arcToFollow.endX - arc.prevEndX, 2) + |
|
|
|
|
Math.pow(arcToFollow.endY - arc.prevEndY, 2) |
|
|
|
|
, 0.5); |
|
|
|
|
|
|
|
|
|
return arc; |
|
|
|
|
}, |
|
|
|
|
const currD = Math.pow( |
|
|
|
|
Math.pow(arcToFollow.endX - arc.endX, 2) + |
|
|
|
|
Math.pow(arcToFollow.endY - arc.endY, 2) |
|
|
|
|
, 0.5); |
|
|
|
|
|
|
|
|
|
// "How much of movement is in the correct direction"
|
|
|
|
|
const ratio = (prevD - currD) / arc.speed; |
|
|
|
|
|
|
|
|
|
goto: function (arc, x, y, speed) { |
|
|
|
|
const prevD = Math.pow(Math.pow(x - arc.prevEndX, 2) + Math.pow(y - arc.prevEndY, 2), 0.5); |
|
|
|
|
const currD = Math.pow(Math.pow(x - arc.endX, 2) + Math.pow(y - arc.endY, 2), 0.5); |
|
|
|
|
const ratio = (prevD - currD) / speed; |
|
|
|
|
// TODO adjust speed
|
|
|
|
|
// TODO turn in the correct direction
|
|
|
|
|
|
|
|
|
|
if (currD < 10) { |
|
|
|
|
throw new Error(`Arc end of (${arc.endX},${arc.endY}) is within 50px of (${x},${y})`); |
|
|
|
|
if (currD < 20) { |
|
|
|
|
// if (Math.abs(arc.centerX - arcToFollow.centerX) < 10 && Math.abs(arc.centerY - arcToFollow.centerX) < 10) {
|
|
|
|
|
arc = Arc.changeRadius(arc, arcToFollow.radius); |
|
|
|
|
if (arc.speed > arcToFollow.speed) { |
|
|
|
|
arc = Arc.changeSpeed(arc, arc.speed - 1); |
|
|
|
|
} |
|
|
|
|
} else if (ratio < 0.8) { |
|
|
|
|
// arc = (ratio < 0 ? Arc.reverse(arc) : arc);
|
|
|
|
|
arc = Arc.changeRadius(arc, 20); |
|
|
|
|
} else { |
|
|
|
|
arc = Arc.changeRadius(arc, 400); |
|
|
|
|
|
|
|
|
|
if (arc.speed < (arcToFollow.speed + 2)) { |
|
|
|
|
arc = Arc.changeSpeed(arc, arc.speed + 1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return arc; |
|
|
|
|