View Full Version : Vector Maths / Trig Cheat Sheet

10 October 2006, 11:57 PM
Hi all!

I thought it might be of some help to us floundering mayafish if we could start a thread dedicated to providing some formulae, tricks and shortcuts for calculating various 3D trigonometric conditions! I know that it can be frustrating relying on google for this kind of information, especially for people like me who aren't mathematical experts, and who often don't quite now what it is we're looking for!

I hope there are others out there who would find a Vector Mathematics / Trigonometry Cheat Sheet of some use! To all the human supercomputers out there, please jump on board and share some of your repository of number art! Feel free to post tips and tricks, or provide easier solutions to problems listed here-in (corrections, too if required!).

I don't know about anyone else, but I'd also appreciate the 'long' solutions alongside the Maya inbuilt functions, if possible!

P.S. If a thread of this nature already exists, please forgive me and point me in the right direction ;)

My first contribution:

Calculate the center point <x,y,z> of n coordinates.

// Pseudo-code: Calculate the center point <x,y,z> of n coordinates.
// < x, y, z > = < ((x[1] + x[2]... + x[n]) / n), ((y[1] + y[2]... + y[n]) / n), ((z[1] + z[2]... + z[n]) / n) >

// Calculate the center of polygon triangle "polyTri"

// Populate arrays pntA, pntB and pntC with vertex x,y,z coords
$pntA = `xform -q -ws -t polyTri.vtx[0]`;
$pntB = `xform -q -ws -t polyTri.vtx[1]`;
$pntC = `xform -q -ws -t polyTri.vtx[2]`;

// Calculate the center of the three points
$cntX = ($pntA[0] + $pntB[0] + $pntC[0]) / 3;
$cntY = ($pntA[1] + $pntB[1] + $pntC[1]) / 3;
$cntZ = ($pntA[2] + $pntB[2] + $pntC[2]) / 3;

// Create locator at center point
spaceLocator -p $cntX $cntY $cntZ;

10 October 2006, 12:11 AM
Calculate distance between points < x[1], y[1], z[1] > and < x[2], y[2], z[2] >

// Pseudo-code: Calculate distance between points <x[1], y[1], z[1]> and <x[2], y[2], z[2]>
// Distance = squareRoot( (x[2] - x[1])^2 + (y[2] - y[1])^2 + (z[2] - z[1])^2 )

// Calculate the distance between two locators: loc1 and loc2

// Populate $pntA and $pntB with world space coords of loc1 and loc2
$pntA = `xform -q -ws -t loc1`;
$pntB = `xform -q -ws -t loc2`;

// Calculate distance between locators
$distance = sqrt( (pow ($pntB[0]-$pntA[0]) 2) + (pow ($pntB[1]-$pntA[1]) 2) + (pow ($pntB[2]-$pntA[2]) 2) );

// Display distance
print ("The distance between loc1 and loc2 is: "+$distance);

BTW feel free to post more complicated problems / solutions! Being that I'm as mathematically minded as a brick, my contributions will be simple!

Excuse my pseudo-code, too :blush:

10 October 2006, 01:25 AM
I have been thinking about looking into vector math for a while now, as my schools math course does not cover it. But I have not found any good resource for it. Do you know of any good ones?

Thanks for what you have posted so far. Nice and easy to read and follow.

10 October 2006, 01:33 AM
I don't really, sorry :( Most of what I've found online is either extremely complicated, badly documented/commented, or very situational and specific to one problem.

I would be interested in finding some good "Idiot's Guide to 3D Mathematics" links!

10 October 2006, 11:01 AM
Normalise a vector <x,y,z>

// Pseudo-code: Normalise the vector <x,y,z>
// vectorLength = squareRoot ( x^2 + y^2 + z^2)
// x /= vectorLength
// y /= vectorLength
// z /= vectorLength

// Normalise the vector <$vx, $vy, $vz>

$vecLngth = sqrt ( (pow $vx 2) + (pow $vy 2) + (pow $vz 2) );
$vx /= $vecLngth;
$vy /= $vecLngth;
$vz /= $vecLngth;

10 October 2006, 11:10 AM
Seems I screwed something up with my tags and now there's some strange varying indents in my code boxes... ~shrug~

Robert Bateman
10 October 2006, 01:27 PM
Normalise a vector <x,y,z>

(note: $vl could be zero if the vector has zero length. Happens occasionally when say trying to determine the distance moved between frames and it hasn't moved....)

// Normalise the vector <$vx, $vy, $vz>
$vl = (pow $vx 2) + (pow $vy 2) + (pow $vz 2);
$vecLngth = sqrt ( $vl );
$vx /= $vecLngth;
$vy /= $vecLngth;
$vz /= $vecLngth;

10 October 2006, 09:48 PM
Thanks for the correction! :D

10 October 2006, 12:56 AM
H3ro, or anyone else who wants to get into vector math, here's a beginning primer that I found useful...

10 October 2006, 09:10 AM
I had these ones in my bookmark:

Basic 3d Math:

Vector Math for 3D Computer Graphics:

Geometry: (


CGTalk Moderation
10 October 2006, 09:10 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.