Skip to content

Instantly share code, notes, and snippets.

@mikelyndon
Created March 4, 2017 23:10
Show Gist options
  • Save mikelyndon/399e60ed7ac1875aa7d58aed6c7caa2f to your computer and use it in GitHub Desktop.
Save mikelyndon/399e60ed7ac1875aa7d58aed6c7caa2f to your computer and use it in GitHub Desktop.
Code used to animate rocks shown in my GDC 2017 presentation for Sidefx.
//check the distance from the rock to the waypoint1
float distanceToWayPt1 = distance(@P,v@wayPoint1);
f@dist1 = distanceToWayPt1;
//check the distance from the rock to waypoint2
float distanceToWayPt2 = distance(@P,v@wayPoint2);
// Phase 0
// If: Start Frame has been reached
// AND the rock is further than 0.5 units from waypoint 1
// AND the rock is currently in phase 0
// then move the rock towards waypoint1
if (@Frame > i@startFrame && distanceToWayPt1 > 0.5 && i@phase == 0) {
//Decrease the velocity as it approaches waypoint 1
float easeIn1 = pow(fit(distanceToWayPt1,5,0.6,1,0.01), 1);
f@ease = easeIn1;
@P += v@toWayPoint1 * 0.65 * easeIn1;
}
// If the rock is less than 0.5 units from waypoint 1 then enter phase 1
if (distanceToWayPt1 < 0.5 && @phase == 0) @phase = 1;
// Phase 1
if (@phase == 1){
// Add to the hang counter that knows how many frames the rock has been in phase 1
@hangCounter += 1;
// Update the last frame the rock was hanging
i@lastHangFrame = int(@Frame);
// Update hangPosition with the current position
v@hangPosition = @P;
}
// If the rock has been hanging around for 15 frames move to phase 2
if (@hangCounter == 15) @phase = 2;
// Phase 2
if (@phase == 2){
// Lerp between the last hangPosition and the final position
// using a power term to accelerate the rock over the given time
f@wayPtLerp = pow(clamp((@Frame - @lastHangFrame) / 10,0,1),4);
@P = lerp(@hangPosition,v@wayPoint2,@wayPtLerp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment