# 3D Math

## Constraint

Aim Constraint

*Briefly, the problem is that I cannot figure out what the aimConstraint's

offset attribute is actually doing mathematically*. If you look at the Maya

documentation, it says:

*Offset*

Specifies an offset position (translate X, Y, and Z) for the constrained

> object relative to the target point. Note that the target point is the

> position of the target object’s rotate pivot, or the average position of the

> rotate pivots of the target objects. Default values are all 0.

This is of course rubbish, and the the offset value is in fact some kind of

Euler angle offset. On an orientConstraint, for instance, the offset

attribute is simply a post-rotation of Euler angles using the rotateOrder of

the object being constrained. As such, a constrained object's rotation is

something like: worldSpaceConstraintRotation * offsetRotation

*On an aimConstraint, however, you get this result only when the constrained

object has a rotateOrder of XYZ*. If using any other rotateOrder, you get

some other result, and I haven't managed to reverse-engineer it successfully

to figure out what's going on. I pasted two short scripts on my tao thread

that demonstrate the problem:

http://tech-artists.org/forum/showthread.php?t=1512

Thanks in advance for any help anyone can offer!

(A. Mechtley)

From the big pop it makes as soon as I put in the smallest non-zero value,

I'd see it's simply buggy...

- Ofer

Understanding Aim Constraints (Adam Mechtley)

**Transformations**

### Linear Transformation

In mathematics, a **linear map** (also called a **linear transformation**, **linear function** or **linear operator**) is a function between two vector spaces that preserves the operations of vector addition and scalar multiplication. The expression "linear operator" is commonly used for linear maps from a vector space to itself (endomorphisms). In advanced mathematics, the definition of linear function coincides with the definition of linear map.

(Yes, but what is it in english?)

A linear transformation is a certain kind of transformation that preserves certain things from the original before it was transformed. Rotation, reflection and scale are linear transformations because they preserve the origin of the vectors. I think what this is meaning is, if you were to draw a line (ie., vector) from the origin through to the original vector, after you perform the linear transformation you will still be able to draw the line (ie., vector) from the SAME origin to the new position, and not have to draw it from a different position.

Matrices represent certain kinds

of transformations on vector spaces. Speciﬁcally, they correspond to linear transformations. (A

transformation is just another word for a function. For historical reasons, the word “transformation”

is often preferred when dealing with functions on geometric objects and we will follow that usage

here.) For instance, let us consider a rotation about the origin through some angle. Each vector from the origin is transformed into another vector from the origin as a result of the rotation. You should

be able to convince yourself quite easily that if you rotate the sume of two vectors, you get the same

result as if you ﬁrst rotated the vectors and then added them. Basically, the whole parallelogram

rotates along with the vectors. Similarly, if you scalar multiply a rotated vector by a ﬁxed number

you get the same result as if you ﬁrst scalar multiplied and then rotated. What happens if you

translate a vector by moving it in a certain direction and by a certain amount? This would also

translate the addition parallelogram. However, it is not a linear transformation because it does not

preserve the origin of vectors. (It is an example of what is called an aﬃne transformation , which

is almost, but not quite, linear.) Transformations which preserve addition and scalar multiplication

are called linear transformations . More formally, if F denotes a transformation from vectors to

vectors, so that F(x) denotes the vector to which x is transformed, then a linear transformation is

one which satisﬁes

F(x + y) = F(x) + F(y) F(λx) = λF(x) . (1)

We have just seen that rotations are linear transformations. So also are reﬂections . One easy way to

see this is to note that a reﬂection in 2 dimensions corresponds to a rotation in 3 dimensions through

180 degrees about the axis given by the line of the mirror. Another type of linear transformation

is a dilation , which corresponds to scalar multiplication: Dα(x) = αx. It is easy to check using

the formulae in Figure 7 and (1) that Dα is a linear transformation. Translating a vector, x, in a

certain direction and by a certain amount, is the same as forming the vector sum x + v, where v is

a vector from the origin in the required direction having the required length. You should be able to

check that the transformation given by x → x + v does not satisfy either of the formulae in (1).

(Jeremy Gunawardena)

### Examples of linear transformation matrices

Some special cases of linear transformations of two-dimensional space **R**^{2} are illuminating:

rotation by 90 degrees counterclockwise:

rotation by

*θ*degrees counterclockwise:

reflection against the

*x*axis:

reflection against the

*y*axis:

scaling by 2 in all directions:

projection onto the

*y*axis:

### Affine Transformations

An affine transformation is any transformation that preserves collinearity (i.e., all points lying on a line initially still lie on a line after transformation) and ratios of distances (e.g., the midpoint of a line segment remains the midpoint after transformation). In this sense, affine indicates a special class of projective transformations that do not move any objects from the affine space to the plane at infinity or conversely. An affine transformation is also called an affinity.

Geometric contraction, expansion, dilation, reflection, rotation, shear, similarity transformations, spiral similarities, and translation are all affine transformations, as are their combinations. In general, an affine transformation is a composition of rotations, translations, dilations, and shears.

While an affine transformation preserves proportions on lines, it does not necessarily preserve angles or lengths. Any triangle can be transformed into any other by an affine transformation, so all triangles are affine and, in this sense, affine is a generalization of congruent and similar.

(wolfram math world)

## Matrices

Seriously.. what IS the matrix?

Matrix Anatomy (Hamish McKenzie)

Scale & Rotation Matrices (Hamish McKenzie)

How to read a 3x3 Matrix (Andy Nicholas)

How to rotate a 3x3 Matrix (Andy Nicholas)

Matrices offer a concise way of representing linear transformations between vector spaces.

### Identity Matrix

It is made up of 9 numbers arranged like this:

This particular 3×3 matrix is known as the “identity” matrix. We have zero rotation and a scale of 1. It doesn’t do anything to a point in space, no rotation or scale,

(Andy Nicholas)

### Scaling

What would a 2x uniform scale do to the icon? It’d move each point to double it’s original position along each axis. Okay, well that’s easy to write:

### Matrix Decomposition

Factoring out scale from a transformation matrix is called matrix decomposition. Basically decomposition will take a single matrix and return a bunch of other matrices which when multiplied together will give you the matrix you had in the first place. So if you had a matrix that had translation, rotation and scale, if you wanted to halve the RX channel, you’d need to decompose the matrix, perform the operation on the rotation matrix, and then put it all back together.

### (Hamish McKenzie) (more there..)

HINT: you have to load the Maya plugin before you can use it.

http://www.youtube.com/watch?v=OPN2CZe2qjM (shyalb)

This video is a cruel tease, though.

More explanation:

### Rotation Matrices

Lets take the rotation we talked about above (ie r=0, 90, 0) and turn it into a matrix:

| 0, 0, -1, 0 | | 0, 1, 0, 0 | | 1, 0, 0, 0 | | 0, 0, 0, 1 |

This, literally, is the matrix for an object sitting at origin rotated 90 degrees around the Y axis. Now how cool is that? As you can see, the top 3×3 part of the matrix are just the basis vectors we wrote above. And this is generally true for all transformation matrices. The top left 3×3 part of the matrix contains the basis vectors for the transform. This 3×3 part of the matrix is the rotation matrix because it completely describes the rotation. It can be converted to either euler angles, or a quaternion should you need either of them.

Thats the first interesting piece of matrix anatomy, and it will take you a long way. It also means that you can pretty easily construct your own rotation matrices by doing a little bit of vector math. Vectors are far easier to understand than rotations, and when you come to grips with the fact that rotations are indeed just a set of vectors arranged in such a way, then you’re home free.

Now for a few important points. For the matrix to be a proper rotation matrix you need to make sure of a few things:

Your vectors need to be orthogonal to one another

They need to be normalized

If you don’t know what that means do some searching on the web on how tonormalize a vector, and how to determine whether two vectors are orthogonal to one another (hint: their dot product is zero!). So as long as each basis vector is orthogonal to each of the other basis vectors AND each basis vector is normalized, you have yourself a rotation matrix.

(H. McKenzie)

I'd like to add up all the child rotations of a hierarchy (with

different rotation orders) and output them to the euler rotations of

one node with a scripted python plugin.

It would be similar to decomposing a world matrix, however it would

take account of rotations over 180 degrees and different rotation

orders thereby giving a correct value for the total rotation of a

child rather than just a pointless world space orientation.

I can't find the source code in the devkit for the decomposeMatrix

plugin (unsupported?) but I did find:

'Find Euler rotation values of Maya matrix' http://www.akeric.com/blog/?p=1067

http://download.autodesk.com/us/maya/2010help/API/build_rotation_node_8cpp-example.html#_a11

I'm currently looping through a dag path and incrementally adding

rotations with the xform command in an expression

http://download.autodesk.com/us/maya/2011help/CommandsPython/xform.html

Could someone please demonstrate, in scripted plugin code, how to add

rotations together that have different rotation orders like the xform

command does?

(nojunkok)

#rigTip - maya collision system with matrix

https://vimeo.com/49104367 (Marco d'Ambros)

## Shear

A transformation in which all points along a given line remain fixed while other points are shifted parallel to by a distance proportional to their perpendicular distance from . Shearing a plane figure does not change its area. The shear can also be generalized to three dimensions, in which planes are translated instead of lines.

(wolfram math world)

## Shear Mapping

## Tuple

In mathematics and computer science, a **tuple** is an ordered list of elements. In set theory, an **(ordered) ****n****-tuple** is a sequence (or ordered list) of *n* elements, where *n* is a positive integer. There is also one 0-tuple, an empty sequence. An *n*-tuple is defined inductively using the construction of an ordered pair. Tuples are usually written by listing the elements within parentheses '( )' and separated by commas; for example, (2, 7, 4, 1, 7) denotes a 5-tuple. Sometimes other delimiters are used, such as brackets '[ ]' or angle brackets '⟨ ⟩'. Braces '{ }' are almost never used for tuples, as they are the standard notation for sets.

Tuples are often used to describe other mathematical objects. In algebra, for example, a ring is commonly defined as a 3-tuple (*E*,+,×), where *E* is some set, and '+', and '×' are functions from the Cartesian product *E*×*E* to *E* with specific properties. In computer science, tuples are directly implemented as product types in most functional programming languages. More commonly, they are implemented as record types, where the components are labeled instead of being identified by position alone. This approach is also used in relational algebra.

(Wikipedia)

## Vector

A geometric object with both magnitude (length) and direction.

### Basis Vector

A set of vectors that span a space. All span really means is that you can take any vector in your space and represent it as a combination of basis vectors

For example, a set of basis vectors in two dimensions is:

[1 0], [0 1]

Ie, in a 3-dimensional space, any (x,y,z) gnomon represents basis vectors.

Basis vectors are basically a transform’s x, y and z axes after its transformation has been applied. So if you rotate an object by 90 degrees around the y axis, then that object’s basis vectors would be:

x=<0,0,-1> y=<0,1,0> z=<1,0,0>

Try it out – rotate an object in maya 90 degrees in Y, then look at the object’s rotation axes. You’ll see the X axis facing down the negative Z of the world and the Z axis pointing in the direction of the world X axis.

So these are the basis vectors for the transform in world space. Now if you have basis vectors for the transform, you have enough information to figure out the rotation. Ie from the above 3 vectors, you could figure out that the rotation was 90 degrees around Y.

(H. McKenzie)

### Cross Product

The **cross product** (also called the **vector product** or **outer product**) is only meaningful in three or seven dimensions.

(haha, very funny. now what does* that *mean in english?)

The cross product differs from the dot product primarily in that the result of the cross product of two vectors is a vector. The cross product, denoted **a** × **b**, is a vector perpendicular to both **a** and **b** and is defined as

where *θ* is the measure of the angle between **a** and **b**, and **n** is a unit vector perpendicular to both **a** and **b** which completes a right-handed system. The right-handedness constraint is necessary because there exist *two* unit vectors that are perpendicular to both **a** and **b**, namely, **n** and (–**n**).

Ie., you are are adding the values of two vectors together and getting a third vector, where if you drew a line betw. a and b in this picture it would be perpendicular to it.

### Dot Product

The **dot product** of two vectors **a** and **b** (sometimes called the **inner product**, or, since its result is a scalar, the **scalar product**) is denoted by **a** ∙ **b** and is defined as:

where *θ* is the measure of the angle between **a** and **b** (see trigonometric function for an explanation of cosine). Geometrically, this means that **a** and **b** are drawn with a common start point and then the length of **a** is multiplied with the length of that component of **b** that points in the same direction as **a**.

The dot product can also be defined as the sum of the products of the components of each vector as

So in lay terms, you are adding the values of the two vectors together to make a new value. This is a scalar value--one point in space--not another vector.

### How to Normalize a Vector

A **unit vector** is any vector with a length of one; normally unit vectors are used simply to indicate direction. A vector of arbitrary length can be divided by its length to create a unit vector. This is known as **normalizing** a vector. A unit vector is often indicated with a hat as in **â**.

To normalize a vector **a** = [*a*_{1}, *a*_{2}, *a*_{3}], scale the vector by the reciprocal of its length ||**a**||. That is:

The normalization of a vector **a** into a unit vector **â**

### Vector subtraction

Given V and W, V-W is that vector which, when added to W yields V: W + (V-W) = V.

In the sketch, you can put the tails of V and W together and verify that V-W is properly labelled. You can move V and W, stretch them, and change their direction to get a feel for how V-W works.

(mathforum.org)

This allows you to find the 'inner product', ie., the angle between v & w

## Vector Space

A **vector space** is a mathematical structure formed by a collection of vectors: objects that may be added together and multiplied ("scaled") by numbers, called *scalars* in this context.

## Calculating Distance in Euclidean Space

I have two coordinates in space, say,

0,0,0

4,0,0

and so the linear distance between them would be 4.

but what if my coordinates are:

0,0,0

4,1,2

how can i calculate the linear distance now?

Sqrt( (0-4)^2 + (0-1)^2 + (0-2)^2 ) = 4.5826

http://en.wikipedia.org/wiki/Distance#Distance_in_Euclidean_space

what the 'distanceBetween' node does in Maya..

## Polygons

Where is the center of a Polygon? (mayaStation)

## Eulers and Quaternions

Euler rotations and matrices (H. McKenzie)

## More Resources

Charles Looker's Research Page -- best math link page ever

Khan Academy -- great online courses in math

3D Math for Artists - Ryan Trowbridge (thanks to Rigging Dojo) (you need an Area login.. but you already have an Area login, don't you?)