Roll and Rig a Cube in After Effects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- What's up? Joey here at School of Motion and welcome to the 19 of 30 days of After Effects. Today's video is going to be half an animation class and half a class about rigging and expressions. What we're going to try to do is tackle the problem, which is actually much trickier than I thought it would be. How do you make a cube or a square that can roll accurately? You know, if you think about it, there's a lot of logistical problems involved in doing something like that. First, I'm gonna show you how to animate the cube once you have it rigged. Then for the geeks out there, I know that there are some geeks out there, I'm gonna walk you step-by-step through how I built a rig. I'm gonna show you the expressions and explain how they work, then of course, I'm gonna give you everything you need to build the rig for free. Or if you just want to practice your animation skills, you can grab the completed rig too. All you need to do is sign up for a free student account so you can grab the project files from this lesson, as well as stuff from any other lesson on this site. Now, I want to go into After Effects and show you some cool stuff, so let's go do that. For the first part of this video, we're just gonna talk about how to animate a cube kind of tumbling once you have the rig set up. And then, after we do that, I will walk through how i actually came up with it and made this rig and I'll copy and paste the expression code on to the site. So if you guys don't want to watch that part, feel free to just copy and paste the code and it should work for you. There's a lot of things going on here that make this animation work and the rig is part of it. There's also just a lot of animation principles and really precise key framing and animation curve manipulation. I wanted to talk about that first. What I have here is a copy of the scene with no animation on it. And I do have my rig set up. The way this rig works, is there's a bunch of nulls, and the nulls all do different things and we'll talk about that in the second part of this video. But the one that you control is this null here, Box Control 01, and I labeled this 01, because in the demo I had two boxes, so I had two sets of controls. So this null literally, if you just move it left to right, like this, the box rolls correctly, based on where that null is. So if you just wanted to box to just kind of simply roll across the screen, all you need to do is move the null, it's really easy. I wanted it to feel like the box got kicked or something and sort of landed like this. The good thing about having a rig that takes a lot of the manual labor out, is I literally only have to keyframe one thing. The X position. The rotation and really the trick is that the box has to move up and down a little bit as it rotates to always keep it touching the ground. If you look at this box of just Y null right here, that actually moves up and down. Let me move this box back and forth. If you keep an eye on it, it's this null right here. It actually moves up and down as the box rolls. That's kind of what's doing the trick there. Why don't we start out by just animating the X position of this box. We'll have it start off-screen, I'll put a keyframe here. And then, let's go forward a couple of seconds, and we'll have it roll out to the middle of the screen. And I want to make sure that it lands totally flat on the ground. It's gonna be pretty tricky to do that, because all I'm animating is the X position and I can kind of eyeball it and say, oh, that looks right. But how do I actually check and make sure that it's flat on the ground? Let me unlock this null. This null here, the B Rotate, for Box Rotate, if I open up the rotation properties of that null, the Z rotation has an expression on it. And that expression is what's actually setting the rotation and then I have my box parented to that null. So that null is rotating, the box is parented to the null, and that's why the box rotates. What I can do is, I can just reveal the rotation property and slowly adjust my X position until I get this to be a flat zero. And so I can just click on the X position and use my arrow keys. And you can see if I hit up and down, it actually jumps over and misses the perfectly zeroed out rotation. But if you hold command and use the arrow keys, it adjusts the numbers at sort of a smaller scale, so now I can really precisely dial that in. Now I know that the box is flat. If we just do a quick grand preview of that, you've already got your box kind of tumbling with two keyframes. That's why I love rigs and expressions, because it takes a lot of time to set them up, but once you do, you can get all kinds of really complex movement with almost no effort. Think about the speed of this, right? If someone kicked this little box guy and he was gonna land over here, what would happen, and this is where having some animation training and you know, reading a few books about animation and just kind of learn as much as you can, it can help you understand how you should animate things, right? If you kick something and it's tumbling through the air, what basically happens is every time it makes contact with the ground, it's gonna lose some of its energy. Because this box is right now, constantly in contact with the ground, it's gonna be losing momentum all the way through the animation. What it should be doing is moving quickly at the beginning and then slowly, slowly, slowly coming to a stop. Let's select those keyframes, hit F9, to easy ease them. Let's go to the Animation Curve Editor and let's bend the Bezier, like this. What I'm doing is I'm saying, the first keyframe, there is no easing out. It just pops right out really quickly and then that last keyframe, right here, I want it to ease into very slowly. Now it looks like it got kicked and it's sort of slowing down there. There's a lot of stuff wrong with this right now. Obviously, when the box tips over here, it shouldn't slowly settle into the ground like that. Because the box has to follow the rules of gravity, it's gonna tip and land and the way I had it work in this demo, and don't worry about the bouncing, I'll show you all that too. It sort of lands here and doesn't quite have enough energy and then it bounces back the other way. Let's make it do that. What I actually want it to do is when it gets here, I want the box to be a little bit further, I'm just adjusting the X position so it ends up not quite at a 45 degree angle. The weight is still on the left side of the box, so it's gonna have to fall back down. Now let's look at this. It's getting there, it's better, okay? But, it feels like the box is defying gravity, like it's just slowly lifting up its foot at the end there. And so what I really want is, I want that last move. I just want this move to feel like that's where all of that energy really starts to slow down. What I want, at this point of the animation, I just don't want that box moving quickly. What I'm gonna do is, I'm gonna hold command, I'm gonna put another keyframe here. And I'm gonna scoot that keyframe backwards. And what this is doing, is it's letting me create a type of curve where there's a really fast move at the beginning, and then after a certain point, it flattens out very quickly. And it's easier to do this with three keyframes than it is with two. Now if I play this, you can see that it's sort of, all that momentum dies out kind of all at once. I'm just gonna scoot this over a little bit and try to find a sweet spot for it. I might want to move this a little bit, maybe so that the box is lifted up a little bit before it really starts to lose its energy. So it's getting there. But what's happening is, when this box does this final sort of fall right there, it's easing into that keyframe, which I don't want. I need to manipulate these curves, I need to bend them and really make them. And you can see, we're starting to get some weird little points and things like that. And that's gonna be okay. Normally when you guys have seen me in the Animation Curve Editor, I try to make the curves really smooth and avoid stuff like this. That is a rule that just in general can make your animations feel smoother. When things are obeying gravity and hitting the ground, that's a different story. Because when things hit the ground, they stop instantly, and the energy transfers instantly to a different direction. When you have stuff like this, you are gonna have little points in your animation curves. Now it's feeling better, but it's happening too quickly. I just need to flatten it out a little bit. That's better, okay. And really you can see how I'm just doing little adjustments to these Bezier curves and it really can make or break your animation. This just takes practice, just watching your animation and figuring out what the problems are with it, okay? I like how this part feels. And then it leans up and I want it to hang there for a second and then, I want it to start coming back the other way. I'm actually gonna move this keyframe a little closer. And now, it's gonna tip back this way. Let's try 10 frames. So I hit Shift + Page Down. Jumps me forward 10 frames. And sometimes I just like to work right in the Curve Editor, 'cause it's just a nice visual way of working. Hold Command. Click on this dash line and it'll add another keyframe. And then I can pull that keyframe down and I want that cube to over shoot and come back a little too far. And the way this is gonna work, is it's gonna ease out of that first keyframe, and it's actually going to ease into this keyframe, but what I need to do is go to the frame where it hits the ground and make sure that my curve is not easing at that point. And this is a little confusing. It's actually hard to explain, but you want to make sure that as the cube falls, it's accelerating. And it has to accelerate an acceleration and animation curve means it's getting steeper and steeper and steeper and steeper. Once it hits the ground and it starts to come back up, now it's fighting gravity and that's when it can start easing. You can help it if you need to. You can put a keyframe right here and then you have control over this and you can make it even steeper if you want to. I'm gonna try without doing that. Let's see what we get. It leans and comes back, okay? Cool. Now that lean, I like what it's doing. I kind of want it to happen a little bit faster. I'm just gonna move all these keyframes closer. Alright, maybe not that fast. You can really just, depends how anal retentive you are, I guess, I could really do this all day long. So the box hits and leans and I'm just gonna pull this handle out a little bit more. There we go. And you can see it almost makes it. And it's even, this is unintentional, I didn't do this on purpose, but, it even leans a little bit farther, like it's trying to reach and it doesn't quite. And it's kind of interesting, so I'm gonna leave that, but I just want to make it not so strong. There we go. So falls and then it comes back. It comes back this way and then I'm gonna have it overshoot one more time. Each time there's a move, it's gonna take less and less time because you know, the distance it has to fall is getting less and less. Let's just go forward a few frames and let's move this keyframe back to here, so it's just barely off the ground. Let's pull these handles out, let's double check. When the box is touching the ground. So now, the box is touching the ground on this frame, but I can see that this curve is starting to slow down already. I need to make sure it doesn't do that. I'm gonna pull this Bezier handle out so it's steeper on the point of the animation curve where the box is touching the ground. And then, it's gonna one more position here what's actually gonna settle on to the ground. For this, I need to make sure that it's actually sitting on the ground, so I'm gonna do that little trick, where I select this value, I hold Command, and I'm just gonna nudge the values until I get to 360 degrees, which means it's flat on the ground. That's play our animation and see what we've got so far. Cool. There's a few little timing issues, I feel it's too slow at the end, so that's an easy fix. I'm just gonna grab these last few keyframes. Hold Option and scale those last few, back a few frames. Cool. Alright, now this animation, you know, that little hang right there, maybe it's a little too long, but overall, this feels pretty good. It gives you a sense that there's weight and that the box has momentum and all of those things. And what's great, is we've literally only had to keyframe one property to get all of this cool, complex motion. So now, let's talk about having the box bounce a little bit, by doing the Y position. I know that in the end, I'm gonna want it to land here. So that's the final Y position. Why don't we start by saying, okay, let's have the box bounce, maybe this is where it lands on the first bounce. I'm gonna put a keyframe there on Y position. Then I'm gonna go back to the first frame and I'm gonna raise up the box. How high do we want it to be when it comes, maybe there. Maybe that's good. Now let's easy ease these keyframes. Let's go into the Graph Editor. Let's talk a little bit. This is a topic that actually, this is one of the first things that I would teach students at Ringling when we're getting into After Effects, how to do a bouncing animation. There's some rules that bounces follow. One of those rules is, as something is falling, if it's starting up here and someone drops it, let's pretend someone dropped it or that this is the apex of a bounce that we can't see back here. It's gonna ease out of that bounce. It's not going to ease into the floor though, right? Gravity makes things accelerate until they hit something. That means, that handle needs to be shaped like this. So that fist fall needs to look like that. Now, the ball's going to bounce a little bit. And the rules of the bounce are essentially this. The height of each bounce is going to decay following a decay curve. You can Google bounce decay curve, I guarantee you'll find like a little drawing of what it's supposed to look like. And then when you're key framing it, and using the Animation Curve Editor, there's some rules you can follow to make it look more natural. One of those rules is each bounce is gonna take less and less time. This bounce, starting at frame zero, hits the ground at frame 11. What that means is that this bounce, if this was a full bounce, would have taken 22 frames. So that means the next bounce has to take less than 22 frames. Why don't we say 10 frames. I'm gonna jump forward 10 frames, put a keyframe here. And now I'm just gonna bend these Bezier handles, like this. And the rule you want to follow is when the box or whatever is bouncing comes into the ground and you can see the angle this Bezier is making, it's gonna bounce off the wall at the same angle, so you don't want to do this, you don't want to do this, you want to basically, a good trick, you put your play head right on that keyframe and then you try to make this symmetrical, okay? And then, you want to do the same thing here. You want to make this angle more or less match this angle here. So now let's do a little RAM preview. So bounces, and that's actually kind of cool. The bounce is actually happening kind of slowly, but we're out just luckily that it's almost like the box bounces and catches itself like a little ballerina. I love when I do things accidentally that are actually pretty cool. I just want to see what happens if I take these keyframes now and scale them just a little bit. There we go, that's great. Now what's happening is that it's landing on the ground slightly before and then catching itself. So I just moved these keyframes a little bit. If you wanted to, you can even add one more bounce, which might be kind of cool. This bounce from here is frame 10 to here is frame 19. So this bounce was nine frames. The next bounce needs to take less than nine frames. You could figure out exactly the right number of frames if you wanted a perfectly, physically accurate bounce. We're just sort of eyeballing it here so we're gonna make it five frames. So we'll go one, two, three, four, five. Put a keyframe there. We'll just have it do a little bounce. You saw what just happened, I pulled this Bezier handle and screwed this thing up. If that happens, it means the Bezier handles on this keyframe are locked together. If you hold Option, now you can break those handles. And make sure that the angles are symmetrical. There we go, and let's see now. Yeah, like that, it's fantastic. It's funny, like, this looks totally different than the demo that I showed you guys at the beginning of this video. It's kind of nice, it's kind of quirky. Again, I wanna just call out that all we've keyframed is X position, Y position on this null, and all of that rotation and all that stuff is happening for free, which is great. Let's turn on some motion blur. We're gonna get a nice, little cute animation. For some reason, I've been into really cute little shapes and eyeballs and stuff like this lately. So that's that. That shows you how once you have this rig, you can really, really easily animate this stuff. If you look at the demo that I did, there's a little bit more sort of fancy composing going on. This giant box is animated the exact same way. The only extra thing was I used an effect called CC Bend It. And that effect just bends layers. I wanted that one 'cause it's so big to fill, a little bit jiggly. And so I just used that to bend it a little bit. That's a pretty simple trick. Now, let's get into, and I'm gonna use this opportunity to say, if you do not care for expressions, hopefully you'll grow out of that. We're gonna get pretty deep in the woods now. Now this rig, it's not that complicated. The code for it is a little bit long, but it's not as math heavy as I thought it was gonna be. Originally when I set out to do this, this is what I did. I took a box and I put a little guide at the bottom of it and I just rotated it just to see what happens. Obviously, what you notice, is that the box as it rotates, it breaks the ground plane. And so I knew I need to lift that box up somehow based on the rotation. When it's rotated, you know, zero degrees or 90 degrees, it needs to not move. As it rotates, it needs to go up and down. At first I thought maybe I could write an easy expression where as the rotation goes up to 45. 'Cause 45 degrees, that's where the box is gonna have to lift up the most. I thought maybe I could just write an expression where you know, the Y position of the box is based on the rotation of the box. The problem is that there's not a really simple relationship between how high the box needs to be and how much it's rotate. If it's rotated 10 degrees, it still needs to lift up, but as it's rotated 20 degrees, it doesn't need to lift up nearly as much. There's not a one to one linear relationship between rotation and height. The next thing I tried was very painful and I tried figuring out some trigonometry and I don't know, that probably says a lot more about me than about the way you should go about doing this, but I was trying to use trigonometry to figure out, can you figure out based on the rotation how much taller this cube is getting. I got close with it, but maybe I'm not good enough at trigonometry and I'm sure that there's a way to do it with cosines and sines and tangents and all that. Then I remembered, and this is where just knowing what's possible with expressions can be amazing. I remembered that there's some expressions in After Effects, that will let you, for example, figure out where on screen this point of this layer is. No matter where this cube is rotated, it can tell me where this corner is, right? So as I rotate it, I can have a value that tells me exactly where that corner is. What I could do then, is put an expression on the cube to figure out the top left, top right, bottom right, bottom left, to figure out where those corners are on screen at all times. Figure out which of those corners is the lowest, and then, figure out the difference between where that corner is and where the center of the box is. I don't know if that made any sense, but we're gonna start making this expression and hopefully it'll make sense as we go. Let's start, I hit F1. I brought up the Help for After Effects, which is funny, because I actually did that when I was trying to figure this out. So let's make a null, new Null Object. We are gonna call this the B Rotate Null. And I'm just gonna parent the box to it. The reason I'm doing that is because, whenever I make a rig, I try to think ahead and say, you know, what? This box is not always going to be the box that I'm gonna want. Sometimes I'm gonna want a bigger box or a smaller box or a red box. I'd rather rotate a null and then have the box just parented to it. I rotate the null, there you go. The next null I'm gonna make. Let me just duplicate this. I'm gonna call this B Y Adjust. This null, what I need this to do, and I'm gonna parent the rotate null to it. This null, I'm going to need to separate the dimensions and have the Y position adjust based on the rotation of this null here. If I rotate this, I want this null to automatically rise up like this, so that the bottom of the box, wherever that happens to be, lines up right on that line. That make sense? There we go. Let's rotate that back to zero and let's set that back to 540. And now we're gonna start talking about expressions. Here's what we need to do. First thing we need to do is figure out how big this layer is, this little box layer. Because what I need to do is tell After Effects to sort of track the top left corner, right corner, bottom right, bottom left. And I can't do that if I don't know how big the box is. Well, I was very smart when I made this box, and I made it 200 pixels by 200 pixels. Very easy numbers. What I can do is, I'm gonna put an expression on the Y position. So let's hold Option, click Stopwatch and let's get rolling. And we're gonna define some variables first. The first thing we need to know is what is the length of one side of the box. What are the dimensions of the box. I made a variable called Box D for dimensions. And I'm gonna say that equals 200. If I know that one side is 200 pixels, what are the coordinates of each of these corners? The way After Effects works, is the anchor point of my layer is the zero, zero points of my layer. And you can see the anchor points right in the middle. As we move to the left, our X value is gonna turn negative. And as we go right, it's gonna turn positive. For Y values, if we go up, it's gonna turn negative and if we go down, it's gonna turn positive. What that means is that this corner here is negative 100, negative 100. And then this corner is positive 100, negative 100. That's how you can figure out where the corners are. Because when the anchor point is right in the middle and we want to go back half the length of the box, I'm then gonna say, D equals Box D divided by two. That D, that is now a variable that tells me how far to move to find these corners. Now, I'm going to define the actual coordinates of the corners. I'm just gonna say top left, TL equals. And what I want to do is use an expression called toWorld, and I'll explain why in a minute. But the first thing I need to do, is say that I'm looking at the layer B Rotate. Because B Rotate that null, that is what's going to actually rotate, not the Box one layer, but the Rotation null is going to rotate. As it rotates, let me just hit Enter for a minute. As this rotates, right? The corner of that null, which just happens to correspond perfectly to the corner of my cube, that is going to move through space. I'm looking at the layer, B Rotate. And I'm going to use an expression called toWorld. And what toWorld does is it translates a coordinate on a layer. For example, this bottom right corner is gonna be 100, 100, on that layer. And as it rotates, it's gonna move through space. The coordinate of that point doesn't change on the layer itself, but it does change as to where it exists in After Effects' world. toWorld converts that point into a world coordinate for me. It's the layer, period, toWorld, and then you open parenthesis, and then you tell it what coordinate to convert. The first coordinate I want it to convert, is the top left corner. So the top left corner remember, is negative 100, negative 100. I don't want to just type in those coordinates, I want it to get the coordinates from this variable here. So if you remember, D is our box dimension divided by two. So, D actually equals 100 right now. If I type in, and you have to do this in brackets, 'cause we're gonna put in two numbers. If you said, negative D, comma, negative D, close the brackets, close the parenthesis, semicolon, there you go. That's how you have to structure this. Again, it's the layer, toWorld, and then the coordinate on that layer you want to convert into world coordinates. Now let's do the top right. And I'm just gonna copy and paste this so I don't have to type in every time. We paste it, we change the variable name to Top Right. So now, the top right coordinate is 100, negative 100. That first number is positive. And then we're gonna do the bottom left coordinate, so that's gonna be negative 100, 100. So now it's negative, positive. And then finally, bottom right, is gonna be positive, positive. And what makes it even great, what makes it even more confusing and awesome is that when you get into Cinema 4D, it doesn't work that way. It actually, the X and the Y values, they're reversed, I believe. That might see, now I just said it, now I'm self doubting, so someone correct me if I just made that up. Now what we've got is we've got these four variables, TL, TR, BL, BR. And those coordinates are literally world coordinates now. Which is fantastic. The next step is to figure out which of those coordinates is the lowest one. Let me just show you here. If we have, for example, let's say we rotate this like this, okay? The bottom right corner is the lowest one. If we keep rotating it, though, now the top right corner is the lowest one. We need to know which coordinate is the lowest one. And so what we're gonna do, we're gonna make some new variables here. What I basically want to do, each of these variables, top left, top right, bottom left, bottom right, these contain two numbers. They contain what's called an array and it's an X position and a Y position. And I don't really care what the X position is, I just care what the Y position is. So let's pull out just the Y position here. What we can do is, we can do it two ways. I could just keep adding to this expression and tweak it a little bit, but to make it less confusing, I'll just do it as a separate line. Why don't we say, the top left Y position equals that top left variable, and then in brackets, a one. Now, why a one? Well, when you have an array with two numbers, right? This variable TL right now, if you were to look at actually what the value of it is, it would look like this. It would be negative 50, comma, negative 50, right? X then Y, and I don't care about X, I just want Y. This value here has a number and this value here has a number, sort of like and index and it starts at zero. If I want the X value, I would make this zero. If I want the Y value, I would make it one. So that's what I'm doing. There we go. Now I'll just copy and paste this. Three more times and I'll just change the name. So this is gonna be TR Y position, BL Y position, and BR Y position. And then I'll just change these variables so that we're getting the right ones. Now, I have these four variables here, which only contain one number, they Y position of the corner. Now, let's figure out, which one of these is the lowest on screen. So here is what you can do, there's a bunch of ways to do it. You could write out a bunch of if, then statements, that sort of check. If this one is lower than this one, then let's use that. And then check the next one. If this one is lower than this one. There's a little shortcut. There's a command called Max and there's another one called Minimum. And it basically lets you compare two numbers and it would just tell you which one is the higher or lower based on what you want to know. What I'm gonna say is the lowest Y equals, so I'm making a new variable. And to find that lowest Y, I'm gonna use a command called Math dot max. And when you use this Math command, you have to capitalize Math, just one of these weird confusing things. Most things are lower case, that one's upper case. And then dot max. The math command, which actually, if you click on this little arrow here, it's in the Java Script math section here. And you can see a whole bunch of things you can use. And so we are using this one, Math dot max. And you give it two values and it tells you which one is the highest, or the maximum. Now, it would be counter intuitive, we want to know which one is the lowest on screen. Remember, in After Effects, the lower you go on screen, the higher the value of Y gets. And when you go up on screen, Y gets negative, so the lower the value is. That's why we're using max. And I'm just gonna check between the first two variables. TL Y position, and TR Y position. Now, the lowest Y variable is going to contain whichever these numbers is the highest, meaning the lowest on screen. So now, we need to check the other variables. I'm just gonna do the same thing again. Lowest Y equals, and this is a cool trick you can do when an expression is. I now want to take whatever the variable currently is, lowest Y, so I can actually use the variable to examine itself. It's like being John Malkovich or something. And now I'm gonna add the next variable, the bottom left Y position. And then I'll do it one more time. Lowest Y equals Math dot max, look at the lowest Y and then examine the bottom right Y position. And as I'm doing this, I realize I didn't name these variables correctly. These should be bottom right Y position, there we go. Cool. I hope you guys can understand what's going on here. I'm just literally just kind of iterating through each one of these variables to compare all four of them and figure out in the end, which one is the lowest on screen. And I probably should have named this differently. I'm looking for the lowest on screen, but actually, the highest number. Lowest Y actually contains the highest value, but it's the lowest position on screen. Now, after all this work, we have a variable that tells me where on screen the lowest point of that cube is. No matter how I rotate it. The next thing I could do is I can take that value, right? And let's talk through this a little bit. And actually, what just happened is. Let's see what happens if I rotate this now. You can see that some stuff is starting to happen. I haven't really set this up correctly yet, but this is what I want you to think about. The B Rotate null is right in the middle of our layer. And what I really want to find out is what is the difference between the bottom of our layer when it's on the floor and the bottom once it's rotated. So what I'm gonna do is I'm gonna make one more null, and I'm gonna call this Box Control, Box C-t-r-l. And I'm gonna just temporarily parent this to my box and position it at 100 comma 200, there you go. So now it's at the very bottom of the box. Then I'm gonna unparent it, and now I'm gonna parent the box. Sorry, I'm gonna parent the B Rotate null. Nope, see I'm telling you guys lies. Box is parented -- I knew I was gonna stumble through this, I knew it. The box is parented to the rotate null, the rotate null is parented to the Y adjuster. And the Y adjuster, I now want to parent to the box control. So now I've got this nice parenting chain. And that's gonna screw some stuff up, but don't worry. And I want the box control to end up right in the middle, right on this floor right here. And let's go to the Y adjust and just turn this off for a minute. Let's think about this. If my box control, and now everything's all messed up, but don't worry about it yet. If what I want to find out is, my box control null is here, okay? I know where it is and I'm also gonna know where the lowest point of my box is. if the box is rotated, let me turn off this expression for a minute, so I can demonstrate this. If my box is rotated like this, I want to measure the distance between my box control null and whatever the lowest point of that box is. Does that make sense? Because then, I can adjust it up by that amount. That's the key to this whole setup here. What I need to do now is go into this expression and I need to add something up at the top here. I need to find out the Y position of my box control null. I'm gonna say control Y position equals, and I'm gonna pick whip this layer and I'm going to use the toWorld command, just like I did here. So that way, if I make this into a 3D layer, or if I more a camera around it, it should still work. So toWorld, parenthesis, and the coordinate I want to put in there is zero comma zero. Because I want to find out where the anchor point of that null is. So there you go. Now, I have two values. I have the control points Y value, which is here, and then I have the lowest point of the cube's Y value, which is here. And what I want to do is subtract one from the other. And honestly I can't remember which one to subtract, so let's just try this way. Let's just try subtracting control Y position minus lowest Y. Let's see what that does. Alright, so we are -- I know what's going on here. See this little warning. Let me try and troubleshoot this with you guys. It's telling me, error at line zero, so I know that it's something going on. Actually, I don't think it's line zero, but let's take a look at this. The Y position of layer two, blah, blah, blah, must be of dimension of one not two. What's going on here? I set this variable incorrectly to control Y position equals the box control layer, toWorld, and the problem is that this toWorld is actually gonna give me an X and a Y. And all I want is the Y. Remember, to get the Y you just add bracket one, there we go. So now, as I rotate this, it's going. There you go, it's working. Dear god. This is actually the -- This is sort of how I acted once I finally figured this out. I couldn't believe that it worked. Let me try and just walk through it one more time because I know that this is probably gobbly gook in your head right now. I have a null, the box control null. Let me actually move this. Let's see here, where's my box control, there we go. I just adjusted the Y position of the BY adjust null so that I could put that box control null right on the bottom. If I rotate this cube now, right, it always stays on the floor. Remember the reason that's happening is because I'm tracking the four corners of it and wherever those four corners are, I'm figuring out which corner is the lowest. Right now it's this corner. But here, it's this corner. And whichever corner's the lowest, I'm figuring out how far below my control null is going and then I'm subtracting that amount to bring it back up to level with the floor. Boy, I really hope you guys could understand this because I know that if you've never used expressions, it probably isn't making a lot of sense and you may have to watch this video a bunch of times to really comprehend this. What I'd love you guys to do is actually go through the painful process of typing in the expressions. For some reason, typing them in helps solidify the concepts in your mind. Now it's working. Now I've got this rotation null that is going to, you know, give me this automatically, super simple. The next step is, how do I make sure that as I move my control null around, it rotates the correct amount. Because, you know, what you could just try doing is say, let's put a position key frame here an another one and move this. And then, we'll just put key frames on rotation. And we'll just have it rotate 90 degrees. If you're lucky, it'll work. But you can see even in this example, it looks like it's kind of gliding across the ground, it's not stuck to the ground. And it's gonna be very hard to manually get that to work right, especially if you're trying to do kind of more complex movements like this and have it land and then stop for a minute and fall back. I mean, that's gonna be really tricky. I wanted the rotation to happen automatically based on where this thing is. What I figured was that each side of this cube is 200 pixels, so if it's gonna rotate 90 degrees, it's gonna move 200 pixels. All I needed to do was make an expression that would rotate this 90 degrees for every 200 pixels I move this. How do I know that I've moved it 200 pixels? First I need a starting point to measure from. I made another null, one more null here, and I called this box start position. I'm gonna put this null level with the ground here. I'm gonna look at the Y position of box control, it's 640, so I'm gonna put this at 640. This box control null, or this starting position null, all it's gonna do is it's gonna give me a reference point where I can measure the distance between this and my control null and that will control the rotation of the box. And this is a pretty simple expression. I'm gonna put an expression on rotation for the B Rotate null. And what I want to do is compare two points. So the start position equals this null dot, and again, I'm gonna use this toWorld command just in case. This will work, but as soon as you make things 3D and you start moving a camera around, if you don't have that toWorld, your values aren't gonna be correct. I'm gonna say, toWorld, parenthesis, brackets, zero, zero, zero. Sorry, just zero, zero. I'm just looking at the anchor point of this. And then I'm going to -- I'm just gonna add the bracket zero to this. Because now, all I'm concerned with is the X position, right? The distance between this and this, but only on X. And I didn't include Y, because I knew if this box was bouncing up and down, I didn't want that to throw off the rotation. I only want the rotation to be based on horizontal movement. So that's why that bracket zero's there. So then, the same thing for the end position equals. End position equals, we're looking at the control null, right here. So we're looking at this dot toWorld, parenthesis, bracket, zero, zero, close, bracket close, parenthesis, and then add that, bracket zero to the end and now I've got start position and the end position. One thing that used to trip me up all the time is that when you use the toWorld command, or expression, you don't use it with the position property of the layer. You don't do this toWorld. That won't work. What you have to make sure you do is you actually need to pick whip and select the layer itself and then use toWorld. If you're having trouble, make sure you're doing that. And then all I need to do is figure out how far this thing has moved. I have this start position, I have the end position, so I'll just say, start position minus end position, so that's now the difference, right? The distance that's been moved. I'm gonna put that in parenthesis and then I'm going to multiply it by 90. Let's see here, I'm missing a step, I know what it is. Let's think about this for a minute. If this thing moves, if our control null moves 200 pixels, that means that it should rotate 90 degrees. What I actually want to find out is how many times this thing has moved 200 pixels away. And then multiply that number by 90. I actually need to get the difference between the start and the end, divide by the length of one side of the box, which we know is 200, and then multiply the result of that by 90. There we go. Now, if I move this box control null, it's kind of interesting. It's rotating, it's just rotating the wrong way, so let me multiply it by negative 90 instead. Now let's move it, and there you go. And now you've got this great little control scheme. That ladies and gentlemen, that's the rig, that's how it works. I added a couple of other little helpers. Good rule of thumb, when you're making an expression, anytime you have a number like this, this 200, that's hard coded into this expression. If, for example, I decided, instead of box one, I wanted to use box two, which is a much bigger box. Now I have to go in and change this expression. And I also have to go and change this expression because it's hard coded in here too. And that doesn't take that much time. It's certainly would be a pain if you had a whole bunch of boxes, so what I did was on this box control null, I added a nice little expression slider control. And I just called this box side length. And that way, I can tie this number to any expressions that need to use that number. So box one, let me replace box one with box two again, and I'll show you how to adjust this. We know that box two has a length of 200 for each side, so now what I'll do, is I'll make sure I can see this slider. I just hit E to bring out the effects on my null. And then I open this so I can see it. Now let's double tap U to bring up our expressions. Instead of hard coding 200 in there, I'm gonna pick whip to that slider. Now whatever that slider is set to is actually the number that will get used. In this expression, that's all I have to change. Now on the rotation expression, I just need to do the same thing. Instead of 200, I can just pick whip to this. And there you go. And now the beauty is, if I swap out a different box, right now it's not gonna work right, but if I change the box side length to whatever the correct size is, which box two is 800 by 800. If we switch this to 800 now, and now I move this, this box will now rotate correctly. Now you've got a very versatile rig, which is very important. You could probably, I don't know, if you guys are like me, you can probably think of 10 other things that you can add controls for. This in essence is really all you need to do to start animating these boxes. This was an interesting one. We kind of hit some animation principles in the beginning and then we really went deep with expressions and making a box rig. I hope that there was kind of something for everyone in this tutorial. I hope, you know, if you're a beginner and you're just getting the hang of animation, I hope that the first part was really helpful. And if you're more advanced and you're really digging rigging and expressions and want to learn more about that, then hopefully the second part of the video was helpful. So thank you so much and I'll see you guys next time. Thank you so much for watching that. I hope you learned not only something about animation, but also something about problem solving and After Effects and how to approach tackling an expression rig. I know a lot of you maybe haven't done that yet, but just knowing what's possible can sometimes open up a lot of opportunities in After Effects. If you have any questions or thoughts about this lesson, let us know and we'd love to hear from you if you use this technique on our project. Give us a shout on Twitter at School of Motion and show us your work. And I you've learned something valuable from this video, please share it around. It really helps us spread the word about School of Motion and we definitely appreciate it. Don't forget to sign up for a free student account so you can access the project files from the lesson that you just watched, plus a whole bunch of other neat stuff. Thanks again and I'll see you on the next one.
Info
Channel: School of Motion
Views: 47,074
Rating: undefined out of 5
Keywords: motion graphics, tutorial, Cube, Roll, After Effects, Video Tutorial, Expressions, Graph Editor, Animation
Id: PGvpFRd5BME
Channel Id: undefined
Length: 51min 13sec (3073 seconds)
Published: Mon Jun 26 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.