home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / util.c < prev    next >
C/C++ Source or Header  |  1999-09-27  |  4KB  |  256 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25.  
  26. void RotateAbout (vp, v, n, theta)
  27. double vp[3], v[3], n[3], theta;
  28. /*
  29.  *  Rotate the vector v about the vector n by theta radians, leaving
  30.  *  the rotated vector in vp.
  31.  */
  32. {
  33.     double X, Y, Z, x, y, z, sintheta, costheta, t1, t2, t3, t4, t5;
  34.     double t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17;
  35.     double t18, t19, a, b, c, d, e, f, g, h, i;
  36.     
  37.     X = v[0];
  38.     Y = v[1];
  39.     Z = v[2];
  40.  
  41.     x = n[0];
  42.     y = n[1];
  43.     z = n[2];
  44.  
  45.     sintheta = sin (theta);
  46.     costheta = cos (theta);
  47.  
  48.     t1 = x * x;
  49.     t2 = y * y;
  50.     t3 = z * z;
  51.  
  52.     t4 = 1.0 - t1;
  53.     t5 = 1.0 - t2;
  54.     t6 = 1.0 - t3;
  55.  
  56.     t7 = 1.0 - costheta;
  57.  
  58.     t8 = x * y;
  59.     t9 = x * z;
  60.     t10 = y * z;
  61.  
  62.     t11 = x * sintheta;
  63.     t12 = y * sintheta;
  64.     t13 = z * sintheta;
  65.  
  66.     t14 = t4 * costheta;
  67.     t15 = t5 * costheta;
  68.     t16 = t6 * costheta;
  69.  
  70.     t17 = t8 * t7;
  71.     t18 = t9 * t7;
  72.     t19 = t10 * t7;
  73.  
  74.     a = t1 + t14;
  75.     b = t17 + t13;
  76.     c = t18 - t12;
  77.  
  78.     d = t17 - t13;
  79.     e = t2 + t15;
  80.     f = t19 + t11;
  81.  
  82.     g = t18 + t12;
  83.     h = t19 - t11;
  84.     i = t3 + t16;
  85.  
  86.     vp[0] = X * a + Y * b + Z * c;
  87.     vp[1] = X * d + Y * e + Z * f;
  88.     vp[2] = X * g + Y * h + Z * i;
  89. }
  90.  
  91. Normalize (v)
  92. double v[3];
  93. {
  94.     double Mag(), vt[3];
  95.     Vdiv (vt, v, Mag(v));
  96.     Vset (v, vt);
  97. }
  98.  
  99. double Dotp (a, b)
  100. double a[3], b[3];
  101. {
  102.     return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
  103. }
  104.  
  105. double Mag (a)
  106. double a[3];
  107. {
  108.     return ((double) sqrt ((double)(a[0]*a[0] + a[1]*a[1] + a[2]*a[2])));
  109. }
  110.  
  111. double Mag2 (a)
  112. double a[3];
  113. {
  114.     return (a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
  115. }
  116.  
  117. Vadd (a, b, c)
  118. double a[3], b[3], c[3];
  119. {
  120.     a[0] = b[0] + c[0];
  121.     a[1] = b[1] + c[1];
  122.     a[2] = b[2] + c[2];
  123. }
  124.  
  125. Vsub (a, b, c)
  126. double a[3], b[3], c[3];
  127. {
  128.     a[0] = b[0] - c[0];
  129.     a[1] = b[1] - c[1];
  130.     a[2] = b[2] - c[2];
  131. }
  132.  
  133. Crossp (c, a, b)
  134. double a[3], b[3], c[3];
  135. {
  136.     c[0] = a[1]*b[2] - a[2]*b[1];
  137.     c[1] = a[2]*b[0] - a[0]*b[2];
  138.     c[2] = a[0]*b[1] - a[1]*b[0];
  139. }
  140.  
  141. Vdiv (a, b, s)
  142. double a[3], b[3], s;
  143. {
  144.     a[0] = b[0] / s;
  145.     a[1] = b[1] / s;
  146.     a[2] = b[2] / s;
  147. }
  148.  
  149. Vmul (a, b, s)
  150. double a[3], b[3], s;
  151. {
  152.     a[0] = b[0] * s;
  153.     a[1] = b[1] * s;
  154.     a[2] = b[2] * s;
  155. }
  156.  
  157. Vset (a, b)
  158. double a[3], b[3];
  159. {
  160.     a[0] = b[0];
  161.     a[1] = b[1];
  162.     a[2] = b[2];
  163. }
  164.  
  165. double Dist2 (x1, y1, z1, x2, y2, z2)
  166. double x1, y1, z1, x2, y2, z2;
  167. /*
  168.  *  Squared distance between two points
  169.  */
  170. {
  171.     double dx, dy, dz;
  172.  
  173.     dx = x1 - x2;
  174.     dy = y1 - y2;
  175.     dz = z1 - z2;
  176.  
  177.     return (dx*dx + dy*dy + dz*dz);
  178. }
  179.  
  180. double rnd (r)
  181. double r;
  182. /*
  183.  *  Random double on [0..r]
  184.  */
  185. {
  186.     int i;
  187.     double f;
  188.  
  189.     i = (rand() & 0xffff);
  190.     f = r * (((double)(i)) / 65536.0);
  191.  
  192.     return (f);
  193. }
  194.  
  195. OutOfMemory()
  196. {
  197.     Log ("OutOfMemory: Out of memory!  Panicking!!");
  198.     FinishSound();
  199.     CloseLog();
  200.     exit (0);
  201. }
  202.  
  203. Perp (v1, v)
  204. double v1[3], v[3];
  205. /*
  206.  *  Find a vector v1 perpendicular to non-zero vector v
  207.  *
  208.  *  Based on the idea that the dot product of perpendicular vectors is zero
  209.  */
  210. {
  211.     if ( (v[0] != 0.0) && (v[1] != 0.0) )
  212.     {
  213.         v1[0] = -1.0 / v[0];
  214.         v1[1] =  1.0 / v[1];
  215.         v1[2] = 0.0;
  216.     }
  217.     else if ( (v[1] != 0.0) && (v[2] != 0.0) )
  218.     {
  219.         v1[0] = 0.0;
  220.         v1[1] = -1.0 / v[1];
  221.         v1[2] =  1.0 / v[2];
  222.     }
  223.     else if ( (v[0] != 0.0) && (v[2] != 0.0) )
  224.     {
  225.         v1[0] = -1.0 / v[0];
  226.         v1[1] = 0.0;
  227.         v1[2] =  1.0 / v[2];
  228.     }
  229.     else if ( (v[0] == 0.0) && (v[1] == 0.0) && (v[2] != 0.0) )
  230.     {
  231.         v1[0] = 1.0;
  232.         v1[1] = 0.0;
  233.         v1[2] = 0.0;
  234.     }
  235.     else if ( (v[0] != 0.0) && (v[1] == 0.0) && (v[2] == 0.0) )
  236.     {
  237.         v1[0] = 0.0;
  238.         v1[1] = 1.0;
  239.         v1[2] = 0.0;
  240.     }
  241.     else if ( (v[0] == 0.0) && (v[1] != 0.0) && (v[2] == 0.0) )
  242.     {
  243.         v1[0] = 1.0;
  244.         v1[1] = 0.0;
  245.         v1[2] = 0.0;
  246.     }
  247.     else
  248.     {
  249.         /* Should never happen */
  250.         v1[0] = 1.0;
  251.         v1[1] = 0.0;
  252.         v1[2] = 0.0;
  253.     }
  254. }
  255.  
  256.