2 * Elite - The New Kind.
4 * Reverse engineered from the BBC disk version of Elite.
5 * Additional material by C.J.Pinder.
7 * The original Elite code is (C) I.Bell & D.Braben 1984.
8 * This version re-engineered in C by C.J.Pinder 1999-2001.
10 * email: <christian@newkind.co.uk>
16 * The original Elite code did all the vector calculations using 8-bit integers.
18 * Writing all the routines in C to use 8 bit ints would have been fairly pointless.
19 * I have, therefore, written a new set of routines which use floating point math.
30 static Matrix start_matrix =
40 * Multiply first matrix by second matrix.
41 * Put result into first matrix.
45 void mult_matrix (struct vector *first, struct vector *second)
50 for (i = 0; i < 3; i++)
53 rv[i].x = (first[0].x * second[i].x) +
54 (first[1].x * second[i].y) +
55 (first[2].x * second[i].z);
57 rv[i].y = (first[0].y * second[i].x) +
58 (first[1].y * second[i].y) +
59 (first[2].y * second[i].z);
61 rv[i].z = (first[0].z * second[i].x) +
62 (first[1].z * second[i].y) +
63 (first[2].z * second[i].z);
66 for (i = 0; i < 3; i++)
73 void mult_vector (struct vector *vec, struct vector *mat)
79 x = (vec->x * mat[0].x) +
83 y = (vec->x * mat[1].x) +
87 z = (vec->x * mat[2].x) +
98 * Calculate the dot product of two vectors sharing a common point.
99 * Returns the cosine of the angle between the two vectors.
103 double vector_dot_product (struct vector *first, struct vector *second)
105 return (first->x * second->x) + (first->y * second->y) + (first->z * second->z);
111 * Convert a vector into a vector of unit (1) length.
114 struct vector unit_vector (struct vector *vec)
124 uni = sqrt (lx * lx + ly * ly + lz * lz);
137 void set_init_matrix (struct vector *mat)
141 for (i = 0; i < 3; i++)
142 mat[i] = start_matrix[i];
147 void tidy_matrix (struct vector *mat)
149 mat[2] = unit_vector (&mat[2]);
151 if ((mat[2].x > -1) && (mat[2].x < 1))
153 if ((mat[2].y > -1) && (mat[2].y < 1))
155 mat[1].z = -(mat[2].x * mat[1].x + mat[2].y * mat[1].y) / mat[2].z;
159 mat[1].y = -(mat[2].x * mat[1].x + mat[2].z * mat[1].z) / mat[2].y;
164 mat[1].x = -(mat[2].y * mat[1].y + mat[2].z * mat[1].z) / mat[2].x;
167 mat[1] = unit_vector (&mat[1]);
170 /* xyzzy... nothing happens. :-)*/
172 mat[0].x = mat[1].y * mat[2].z - mat[1].z * mat[2].y;
173 mat[0].y = mat[1].z * mat[2].x - mat[1].x * mat[2].z;
174 mat[0].z = mat[1].x * mat[2].y - mat[1].y * mat[2].x;