There are three position coordinate spaces that After Effects uses to calculate
2D and 3D Positions in your composition.
They are referred to as Layer Space, Composition Space, and World Space.
Let's break down each of those spaces.
The Layer Space is a two dimensional array that's relative to the layer you are working with.
Layer Space refers to a layer's Anchor Point in relation to its top left corner.
The Composition Space is a two dimensional Position array that's relative to the dimensions
of the composition you are working in.
It works in similar ways to Layer Space.
The main difference is that Composition Space refers to a layer's Position
in relation to the Composition's dimensions.
Composition Space is measured from the top left to the bottom left corner.
The World Space is a separate level of space within a composition that layers can interact with
using the Z Position.
It's a three dimensional array that has no direct relation to the composition size.
Have you ever needed to calculate the exact pixel length between two positions
in 2D or 3D space?
The length method is a very easy way to do this.
It starts with the word length, followed by a set of parentheses.
Inside the parentheses, length accepts two arguments representing both Position points
and then calculates the distance between the two arrays.
In this composition, you have a Comet flying through space towards the Planet.
You'll also notice an Impact Distance text layer and a Numerical Readout underneath.
Let's use the length method to calculate the distance between these two objects.
First, open the Source Text property on the Impact Readout by hitting U + U on your keyboard.
Now, add an expression by Alt + Clicking (or Option + Clicking on a Mac) the stopwatch,
and then we're going to use the Expression Language Menu.
I'm going to hit tilde on the keyboard to fullscreen this.
Open up the Expression Language Menu and go to the Vector Math category.
In here, you'll see length with two arguments.
So if you click that, it's going to fill it in here.
I'm going to hit tilde to collapse this back.
So now we're going to need two Position points to calculate the distance between.
So, one's going to be the Comet so I'm going to hit P on that one,
and the other's going to be Planet, so I'm going to hit P on that one as well.
So now that both of these are visible, I can come back to the original expression,
highlight point1, grab the Pick Whip tool, and come down to Comet's Position and let go.
And same with point2, except I'm going to pick whip the Planet instead.
And now, to finish this, just to make it look clean, I'm going to add .toFixed,
with a capital F, open parenthesis, 1, and then close parenthesis
just to clean up the decimal points.
So now, if you RAM Preview the scene, the Impact Distance number here updates
as the Comet gets closer to the Planet.
But you'll notice that the Comet doesn't actually hit 0 because
it's actually calculating the distance between this Comet here and the center of the Planet.
So, to fix this, you can just come to the end of Planet transform.position,
and add a minus, and then a custom array here.
Now this might take some trial and error but I determined that it was [160, 160] offset,
which puts it at the surface of the Planet here.
And then click away from the expression and now you'll see that the Impact Distance
almost immediately updated to 0.
And now if you RAM Preview the scene again, you get a nice countdown to, pow, impact.
Have you ever had a scenario where you needed an effect with a 2D Source Point such as
Trapcode Shine to work with parented objects?
What about linking a 2D effect to a 3D layer's position?
The toComp method can output 2D composition coordinates from both parented positions
and positions in 3D space.
The toComp method starts with the word toComp, capital C, followed by a set of parentheses
and one argument.
The argument must be a two or three dimensional array to act as an offset for the output.
Two very common input arguments for the toComp method are [0,0,0] as a custom array,
and transform.anchorPoint
Now, we're going to take a look back at one of the previous lessons' use
of the toComp method.
In this same composition, if you collapse this down and hide all the old layers,
and then turn on the Rocket layers here, you'll see we have a Rocket Null, a Rocket layer,
and a Rocket Particles layer.
The Rocket Particles layer is using CC Particle Systems II for the particle emission.
And if you go down to the Producer, and Position property, you'll see that's exactly where
these particles are being emitted in the center of the composition.
Now, two problems we have here.
One, this is a 2D effect but we're trying to link it to a 3D layer.
And two, this is also parented to the Rocket Null which also doesn't help our case.
Now this is exactly what toComp was built for.
So first things first, let's add an expression to the Producer Position property.
So go to Producer, Position, add a new expression, and it's going to open it up here on the timeline.
I'm just going to shorten this by double tapping E just so we can see this effect.
Now, we're working with the Rocket layer so I'm going to grab the Pick Whip tool
and come up to the Rocket layer and just let go on the actual layer's name.
This is going to give us the thisComp.layer("Rocket") reference,
and before that I'm just going to go ahead and attach it to a variable,
so roc and then an equal sign, and then I'm going to end this line with a semi colon.
Now on the next line, I'm going to reference the variable I just created, so roc,
followed by a period operator, and then the toComp method which is going to start
toComp with a capital C, open parenthesis, and then inside the parenthesis
you need to add a reference to the Rocket layer's Anchor Point.
So we already have the Rocket layer's reference so we're going to type roc,
and then another period operator.
And then you want to target the Anchor Point, so transform, period operator, anchorPoint
with a capital P, and then close parenthesis.
Anchor Point is used here instead of [0,0,0] to account for the Anchor Point's offset
in Layer Space.
Without this, the calculated point would be somewhere up here on the top left of the layer,
instead of at its true Anchor Point.
So this toComp expression is taking the Rocket layer and converting it to Composition Space,
and then offsetting it based on the layer's Anchor Point,
which gives you the actual true Position.
So this expression is done.
If you click off the expression, it's going to update the Position.
And now, if you RAM Preview your scene,
you'll see that the particles trail right behind the Rocket.
For this to work properly, make sure that the layer you are using is scaled to 100 percent,
centered to the composition, and is the same size as the composition you're working in.
What if you have a situation where you need the true world coordinates of a 3D layer
parented to another 3D null in a scene?
This is where toWorld saves the day.
So let's take the same scene from earlier and apply the toWorld method.
So you can hide all the Rocket stuff and turn on the two Moon layers.
Let's say you wanted to have the Camera layer follow the rotating parented Moon layer
while looking at it from one angle.
Easy to parent it, right?
Well, if you parent the layer, it will rotate around the scene with the Moon,
because it's attached to a rotating null.
And if you Pick Whip the Camera to the Moon, you'll see the Position doesn't change
due to the parent child relationship.
So, where do we start?
Well the same setup for toComp can be used for this example as well.
So in this case, we're going to use the Camera's Position, so hit P to open that up,
and then add a new expression.
So this is going to be a very similar setup to before,
I'm going to start with the moon variable, and then equal sign.
And then I'm going to grab the Pick Whip tool, come down to the Moon layer, and let go.
And this is going to give us our layer reference.
And I'm going to end that line with a semi colon.
Then on the next line, I'm going to use moon to start the reference, period operator,
and then toWorld with a capital W.
And then open parenthesis, and then we need to grab the Moon's Anchor Point.
So moon, period operator, transform, period operator, anchorPoint, with a capital P,
and then close parenthesis.
So this is all there is to it.
If you click outside of the expression, you'll notice the Camera just jumped
and we don't really see anything in our viewport.
Now this is because the Camera is sharing the exact same Position as the Moon layer,
which is causing the Moon to be invisible.
To fix this, we need to offset the toWorld result to bring the Moon back into view
and to pull the camera back a bit.
And we can do this with an array.
So if you click back into the expression, after the toWorld closing parenthesis,
you can add a Subtraction operator, so minus, and then we're going to create a custom array,
so open square bracket, and then 0 comma, 0 comma, 0 for X, Y, and Z axis.
And if you come down to the Moon layer, you'll notice there's already a Moon Zoom slider
with a value of 600.
So with that visible in the Effect Controls panel, you can come back to the original expression
and highlight the 0 for the Z axis, grab the Pick Whip tool and replace it with the Slider reference.
So now, clicking off the expression, boom.
There's the Moon, it's right back in view.
And if you RAM Preview the scene, you'll notice the Camera is now following the Moon
from a fixed angle.
And it rotates beautifully through the 3D scene without any parenting.
In these two examples, you've used all three Layer, Composition, and World spaces
and have converted Position values between the three spaces with toComp and toWorld methods.
You can find Position conversion syntax in the Expression Language Menu
under the Layer category and the Space Transforms sub category.
We've arrived at the end of Lesson 304: Calculating 2D & 3D Position Coordinates
If you enjoy this expressions course, consider purchasing the paid content.
It includes in-depth documentation, extra tutorial content, high definition videos,
and all the project files used in the training.
Your purchase will help to create more free courses like this in the future.