# Cross Products

I won’t get into much detail here other than to talk about a few example of why you would want to calculate vector cross products and briefly explain what they are. Wikipedia has a great entry on them which will explain all the math behind them.

Given two vectors, for instance, two sides of a triangle, the cross product will give you a vector that points perpendicular to those vectors.  This has many uses in 3D animation. You can calculate surface area (1/2 the magnitude of the cross product of vectors of two sides of a triangle will give you the area of that triangle). You can calculate surface normals, which show which direction the polygons of your surface are pointing.  You can use cross products to align sprites to the camera or cull geometry that is pointing away from the camera.  One thing I used cross products for recently was in a particle setup where I needed to create an aura around a character.  I was able to figure out which surfaces of the geometry were pointing perpendicular to the camera and use that as a basis for particle emission.  So, many uses.

# Area of a triangle in 3D space

It can be very helpful to know how to calculate the area of triangles in 3D space. You can use it to calculate the surface area of an object, which in turn you can use to calculate/set mass or density of objects.  Primarily, I use area to create distribution attributes for particle emission, such that polygons of varying size emit a spatially consistent amount of particles.  For example, in Houdini, if area is assigned as a primitive attribute (you have measured the area of each polygon and stored it) you simply multiply area by emission rate and will get a fairly even distribution of particles emitted from a surface.  Of course, Houdini has a built in operator to calculate area, but if you don’t have such an operator in your 3D program, or you are writing your own code, here’s how to do it.

The main tools needed to calculate an area of a triangle in 3D space are covered in my post about distance.

The basic formula using vectors goes something like this:

Given three vector points in space, A, B, and C, the area of the triangle formed by those points is 1/2 the magnitude (see distance) of the cross product (see cross product) of vector AB and AC.

It can be written in mathematical notation like this:

1/2 * |AB x AC|

It can also be explained diagrammatically like so: So, given three points, A, B, and C, pick one point arbitrarily and calculate two distance vectors between that point and the other two points (distance vectors AB and AC) by doing vector subtraction.  Find the cross product between those vectors.  The cross product outputs a vector.  Now, calculate the magnitude of this vector, and finally divide by 2 (or multiply by 0.5).

# Distance or Magnitude of Vectors

I’ll briefly explain vectors here.  Plenty of information out there on the web, and they are quite easy to understand.  Basically, a vector in computer graphics is an array of three numbers [X,Y,Z] that carry a direction and a magnitude.  The magnitude can mean a few different things, depending on context.  For instance, the magnitude can indicate the speed an object is traveling at, when talking about moving objects.  It can also indicate the distance from one point to another point (the components tell you which direction to travel to reach that point).

There’s a formula for calculating the magnitude of a vector, and I will show a couple ways of implementing this formula.

The basic formula is:

Given vector [X, Y, Z], the magnitude is the square root of the dot-product of the vector with itself.

√(X^2 + Y^2 + Z^2)

I won’t get into what a dot-product is here, but let’s just say that this formula works.

So, how does this give us distance?

Consider two points in space, PointA with coordinates (X, Y, Z) and PointB with coordinates (A, B, C).  To get the distance between these points we get the difference between each component and create a new vector with that information, then plug it into our magnitude formula, which will spit out a scalar number (A number with only one component, as opposed to our vector, which has three).

So, our distance vector is: [A-X, B-Y, C-Z]

The magnitude of this is: √[(A-X)^2 + (B-Y)^2 + (C-Z)^2]

Written as a dot-product, if [A-X, B-Y, C-Z] = Vector V,  the magnitude is: √(V•V)

These two diagrams also illustrate two ways of calculating distance. Since most computer programs and computing languages already have functions to calculate dot products, you’ll see you can do this quicker with less nodes/code.  # New Category – Mathematics

So, I am back on the trail of the Destruction plugin.  Looking to do some things that are a bit math-heavy, like figuring out how to decompose a space into Voronoi/Delaunay cells.  Not so easy to do, especially considering my very modest understanding of the mathematics behind computer graphics.

I thought it would be nice to start a category for mathematics, where I can post formulas and solutions to problems I encounter when trying to write this plugin.  I think it will be an excellent collection of information, and I will do my best to make sure the writing is clear and easy to follow/understand.