Thursday, 3 June 2021

The Powah of Local Spaces

Yeah. How ya like this? You wanna know how this works?

 
All that talk of vectors is going to come in handy here.
Maya has a native 3d coordinate space commonly called 'the world'.

When we put objects under parents and then move those parents it will look like the object itself is at a different location than its translation values indicate. That's because when you parent an object to another object, you put it in a local space separate from the world.
The object's position relative to its parent is its local position.
Meanwhile the object's position relative to the world is its world position.
Move an object 4 units from the origin in the x axis, then do the same to its parent - the object will be 4 units from its parent, but 8 units from the world origin. That's a local position of (4, 0, 0) and a world position of (8, 0, 0).

Maya has ways of extracting an object's world position even when there are parents between it and the world. But... another time with that.
To understand how to think about local position, lets look at some more vectors.
Here is a vector (3, 0.5) we'll call A

Here's another vector (-5, 3) we'll call B

When we talk about vectors, it's assumed we're measuring them from the origin unless otherwise stated.

Here's the thing. We can add vectors together to get a new vector that is the combination of their directions and magnitudes.

This is A + B:

Addition is a commutative operation, meaning the order doesn't matter. A+B and B+A yield the same result:

Graphically this makes sense, but mathematically we can figure out what the sum of two vectors is by saying the vector A+B's x vector is the sum of A's and B's x vectors (3 + -5) and its y vector is the sum of A's and B's y vectors (0.5, +3).
3 - 5 = -2
0.5 + 3 = 3.5

And indeed, the vector A+B is (-2, 3.5)

Let's try this in Maya.
Create a cube at the origin. Call it cube A.
Make a second cube at the origin. Call it cube B.
Parent cube B to cube A.
We'll ignore the Z axis and mimic the vectors we just used. Input into cube A's translation attributes 3 in the X field and 0.5 in the Y field.
Input into cube B's translation attributes -5 in the X field and 3 in the Y field.

Cube B now has it's own translate values but on top of that its position is effected by the position of its parent, cube A.
Cube B's world position is the sum of its translate values and its parent's translate values.
Unparent Cube B and now that it's under the world again, it's local and world positions will match again, and you'll see its translate attributes are just like vector A+B from our equation.

Rotation works much the same. If you rotate an object by -30 degrees, and then rotate its parent by -20 degrees, the object will have rotated -50 degrees relative to the world.

Scale is a little more interesting. In a good way. There's some interesting things we can do by scaling an object's parent.
First off, the relationship between an object's scale and its parents scale is multiplicative, not additive. put a cube under a parent group, set the cube's scale to 2, then set the parent group's scale to 2. The cube's local scale may be 2, but its world scale is now 4, because 2 * 2 = 4.

But what about that weird squashed eyeball I opened this post with?
You see, you should think about transforming a parent like transforming the entire coordinate space of its children. What happens if I parent a cube, then rotate that parent by 45 degrees, and then start playing with that cube's X translate?


Ordinarily, we'd expect to have to move it in two axes at once to get that diagonal movement. But understand, from the cube's perspective, it's moving horizontally. It's just that it's moving horizontally in its local space, which has been tilted 45 degrees.

When you translate an object, you translate the space in which its children reside. Same goes for rotation and scale. Here are two cubes. The on top is parented to the world, while the cube bellow that is parented to a group that has been scaled down to 0.5 (the cube has been scaled up to 2, so it appears the same size as the cube on top)


It looks like the top cube has been translated twice as much as the bottom cube, but actually they're both being translated to 10 in the X axis.
Because the cube on the bottom is in a local space that's been scaled down by to 0.5, 10 units for is half as much as 10 units for the cube above.
10 * 0.5 = 5.
So 10 units in space scaled down to 0.5, winds up being 5 units in world space.
But now... we don't have to scale in all three axes at once, do we? What if we scaled an object's parent in say, just the one axis. It and any child objects would appear squashed, yes, but remember, the child object's entire local space is squashed!
Watch what happens when we squash this cube's parent group and then rotate the cube in a squashed local space:


One way this effect is useful is if a character has cartoony, non-spherical eyes. However warped, eyes still need to rotate without changing their shape.


No comments:

Post a Comment