Home Contents Forum Links Tip Jar
Animation Math in Lingo       

Vectors

A vector extends from the origin to some point. A vector can be used to specify a direction and a size (which is its length), so it is a useful term in describing these with one word.

A vector is specified by its endpoint, [x,y]. When working in more than 2 dimensions, a vector is specified by a value for each dimension. In 3 dimensions, a vector is specified by [x,y,z].

A vector that has a length of 1 is called a unit vector. To change the length of a vector to 1 is called normalizing the vector. It is done by dividing each component of the vector by the vector's length.

Some Vector Operations
Addition
[x1, y1] + [x2, y2] = [x1+x2, y1+y2]

Subtraction
[x1, y1] - [x2, y2] = [x1-x2, y1-y2]

Multiplication
[x, y] * scalar = [x * scalar, y * scalar]

Division
[x, y] / scalar = [x / scalar, y / scalar]

Length of [x,y]
sqrt(x2 + y2)

Normalizing [x,y]
normal = [x / length, y / length]

By definition the tail of a vector is at the origin. However, vectors are often drawn at other places to indicate how the vector is being used. For example, in the diagram above vectors A and B are drawn head-to-tail to graphically demonstrate vector addition.

Dot Product
The dot product B.dot(A) returns the distance from the origin to a line dropped from A perpendicular to B.

If the angle between the vectors is greater than 90° the dot product will be negative. If A is normalized the dot product will be between -1 and 1. Dot product is used in the tutorial in calculating light intensity on a surface in 3D rendering, and also to determine if a surface faces the camera for back-face culling.

Cross Product
The cross product A.cross(B) will return a vector that is perpendicular to both A and B.

The direction this vector points is determined by the "right-hand rule", which says that if you put your right hand edge-wise along A and sweep your fingers to B then A.cross(B) will be in the direction your outstretched thumb points.
A.cross(B) and B.cross(A) are vectors that point in opposite directions.

Cross product is used in the tutorial to find the "normal" to a surface in 3D. The normal is a unit vector that points out perpendicularly from the surface. It is found by taking the cross product of two of the surface edges, then normalizing. It's used in the tutorial for lighting and back-face culling, and also in the 3D Camera demo to get the direction to pop the cube when a cube face is clicked.

Vectors in Lingo
A Lingo vector data type and vector functions were added with Shockwave 3D. The functions simplify many operations done in animation. For example, finding the length of a vector, normalizing vectors, and cross and dot products.

Lingo's vector data type is strictly 3-dimensional, but it can be used for 2 dimensions by giving the z-coordinate a value of 0.

Lingo's point() data type can also be used as a two-dimensional vector. While it doesn't have the more advanced vector functions, it supports vector arithmetic.

It is also possible to use linear and property lists as vectors, and do vector arithmetic with them. For example [#x:1, #y:2] + [#x:3, #y:4] = [#x:4, #y:6]. And [#x:1, #y:2] * 5 = [#x:5, #y:10].

Finally, you can write your own vector data type using a script. This would allow you to write vector functions that aren't provided in Lingo.

Vectors in the Tutorial
Many places in the tutorial, vectors are being used without using the word "vector". For example, velocity and acceleration in the tutorial are specified by "x and y components" which is a vector:

Most of the demos in this tutorial are programmed without using Lingo's vector functions so they can show all the math behind the animation. Versions that use vectors are included as extras to show how vector types can be used.

 
 


Copyright © 2003 JM Harward 
 jmckell~at~jmckell~dot~com
All lingo provided on this site may be used freely for educational purposes. Not for redistribution as uncompiled code. Instructional materials may not be reproduced without permission.