Collide.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <math.h>
  19. #include "matrix3d.h"
  20. #include "vector3.h"
  21. #include "col.h"
  22. #include "cvec.h"
  23. #include "mpu.h"
  24. void benchmark_transformations(void);
  25. //---------------------------------------------------------------------------
  26. #include <fstream.h>
  27. #include <stdlib.h>
  28. #define FRAND (-1.0f+2.0f*rand()/float(RAND_MAX))
  29. ofstream ostr("data1.txt");
  30. void Rotate3D (float line[3], float angle, float rotate[3][3])
  31. {
  32. int row, col, mid;
  33. float I[3][3], A[3][3], A2[3][3];
  34. float sn, omcs;
  35. /* identity matrix */
  36. I[0][0] = 1; I[0][1] = 0; I[0][2] = 0;
  37. I[1][0] = 0; I[1][1] = 1; I[1][2] = 0;
  38. I[2][0] = 0; I[2][1] = 0; I[2][2] = 1;
  39. /* infinitesimal rotation about line */
  40. A[0][0] = 0; A[0][1] = +line[2]; A[0][2] = -line[1];
  41. A[1][0] = -line[2]; A[1][1] = 0; A[1][2] = +line[0];
  42. A[2][0] = +line[1]; A[2][1] = -line[0]; A[2][2] = 0;
  43. /* A2 = A*A */
  44. for (row = 0; row < 3; row++)
  45. for (col = 0; col < 3; col++) {
  46. A2[row][col] = 0;
  47. for (mid = 0; mid < 3; mid++)
  48. A2[row][col] += A[row][mid]*A[mid][col];
  49. }
  50. sn = float(sin(angle));
  51. omcs = float(1.0-cos(angle));
  52. /* rotation is I+sin(angle)*A+[1-cos(angle)]*A*A */
  53. for (row = 0; row < 3; row++)
  54. for (col = 0; col < 3; col++)
  55. rotate[row][col] = I[row][col]+sn*A[row][col]+omcs*A2[row][col];
  56. }
  57. void main ()
  58. {
  59. Box box0, box1;
  60. // create box0
  61. box0.center[0] = 0.0f;
  62. box0.center[1] = 0.0f;
  63. box0.center[2] = 0.0f;
  64. box0.basis[0][0] = 1.0f;
  65. box0.basis[0][1] = 0.0f;
  66. box0.basis[0][2] = 0.0f;
  67. box0.basis[1][0] = 0.0f;
  68. box0.basis[1][1] = 1.0f;
  69. box0.basis[1][2] = 0.0f;
  70. box0.basis[2][0] = 0.0f;
  71. box0.basis[2][1] = 0.0f;
  72. box0.basis[2][2] = 1.0f;
  73. box0.extent[0] = 4.0f;
  74. box0.extent[1] = 4.0f;
  75. box0.extent[2] = 4.0f;
  76. box0.velocity[0] = 0.0f;
  77. box0.velocity[1] = 0.0f;
  78. box0.velocity[2] = 0.0f;
  79. // create box1
  80. box1.center[0] = 0.0f;
  81. box1.center[1] = -10.0f;
  82. box1.center[2] = 20.0f;
  83. float line[3] = { 0.0f, 0.707f, 0.707f };
  84. float length = float(sqrt(line[0]*line[0]+line[1]*line[1]+line[2]*line[2]));
  85. line[0] /= length;
  86. line[1] /= length;
  87. line[2] /= length;
  88. float angle = (float)DEG_TO_RAD(45.0f);
  89. Rotate3D(line,angle,box1.basis);
  90. /*
  91. box1.basis[0][0] = 1.0f;
  92. box1.basis[0][1] = 0.0f;
  93. box1.basis[0][2] = 0.0f;
  94. box1.basis[1][0] = 0.0f;
  95. box1.basis[1][1] = 1.0f;
  96. box1.basis[1][2] = 0.0f;
  97. box1.basis[2][0] = 0.0f;
  98. box1.basis[2][1] = 0.0f;
  99. box1.basis[2][2] = 1.0f;
  100. */
  101. box1.extent[0] = 10.0f;
  102. box1.extent[1] = 4.0f;
  103. box1.extent[2] = 4.0f;
  104. box1.velocity[0] = 0.0f;
  105. box1.velocity[1] = 0.0f;
  106. box1.velocity[2] = 0.0f;
  107. BoxClass mybox0(box0);
  108. BoxClass mybox1(box1);
  109. unsigned long high;
  110. unsigned long cycles0;
  111. unsigned long cycles1;
  112. unsigned long cycles2;
  113. while (box1.center[2] > -20.0f) {
  114. cycles0 = Get_CPU_Clock(high);
  115. IntersectType type = BoxesIntersect(1.0f,box0,box1);
  116. cycles0 = Get_CPU_Clock(high) - cycles0;
  117. cycles1 = Get_CPU_Clock(high);
  118. IntersectType mytype = Boxes_Intersect(mybox0,mybox1,1.0f);
  119. cycles1 = Get_CPU_Clock(high) - cycles1;
  120. cycles2 = Get_CPU_Clock(high);
  121. IntersectType mytype2 = Boxes_Intersect(mybox0,mybox1);
  122. cycles2 = Get_CPU_Clock(high) - cycles2;
  123. cout << cycles0 << " "<< cycles1 << " " << cycles2 << " " << type << " " << mytype<< " " << mytype2 << endl;
  124. box1.center[2] -= 1.0f;
  125. mybox1.Center[2] -= 1.0f;
  126. }
  127. // if (type == itIntersects) {
  128. // ostr << "type = " << type << endl;
  129. // }
  130. benchmark_transformations();
  131. #if 0
  132. float line[3] = { FRAND, FRAND, FRAND };
  133. float length = float(sqrt(line[0]*line[0]+line[1]*line[1]+line[2]*line[2]));
  134. line[0] /= length;
  135. line[1] /= length;
  136. line[2] /= length;
  137. float angle = FRAND;
  138. Rotate3D(line,angle,box0.basis);
  139. box0.extent[0] = 1.0f;
  140. box0.extent[1] = 1.0f;
  141. box0.extent[2] = 1.0f;
  142. box0.velocity[0] = 0.0f;
  143. box0.velocity[1] = 0.0f;
  144. box0.velocity[2] = 0.0f;
  145. for (int i = 0; i < 100; i++)
  146. {
  147. box1.center[0] = 2.0f;
  148. box1.center[1] = 0.0f;
  149. box1.center[2] = 0.0f;
  150. line[0] = FRAND;
  151. line[1] = FRAND;
  152. line[2] = FRAND;
  153. length = float(sqrt(line[0]*line[0]+line[1]*line[1]+line[2]*line[2]));
  154. line[0] /= length;
  155. line[1] /= length;
  156. line[2] /= length;
  157. angle = FRAND;
  158. Rotate3D(line,angle,box1.basis);
  159. box1.extent[0] = 1.0f;
  160. box1.extent[1] = 1.0f;
  161. box1.extent[2] = 1.0f;
  162. box1.velocity[0] = 0.0f;
  163. box1.velocity[1] = 0.0f;
  164. box1.velocity[2] = 0.0f;
  165. IntersectType type = BoxesIntersect(1.0f,box0,box1);
  166. ostr << "i = " << i << ' ' << "type = " << type << endl;
  167. }
  168. #endif
  169. /*
  170. ** box0
  171. */
  172. box0.center[0] = 6.1978f;
  173. box0.center[1] = 2.6640f;
  174. box0.center[2] = 0.840f;
  175. box0.extent[0] = 0.1341f;
  176. box0.extent[1] = 0.320672f;
  177. box0.extent[2] = 0.840f;
  178. box0.velocity[0] = box0.velocity[1] = box0.velocity[2] = 0.0f;
  179. box0.basis[0][0] = 0.857709f;
  180. box0.basis[0][1] = -0.514136f;
  181. box0.basis[0][2] = 0.0f;
  182. box0.basis[1][0] = 0.514136f;
  183. box0.basis[1][1] = 0.857709f;
  184. box0.basis[1][2] = 0.0f;
  185. box0.basis[2][0] = 0.0f;
  186. box0.basis[2][1] = 0.0f;
  187. box0.basis[2][2] = 1.0f;
  188. /*
  189. ** box1
  190. */
  191. box1.center[0] = 10.0f;
  192. box1.center[1] = 4.0f;
  193. box1.center[2] = 2.8f;
  194. box1.extent[0] = 4.5f;
  195. box1.extent[1] = 1.966f;
  196. box1.extent[2] = 2.868f;
  197. box1.velocity[0] = box1.velocity[1] = box1.velocity[2] = 0.0f;
  198. box1.basis[0][0] = 1.0f;
  199. box1.basis[0][1] = 0.0f;
  200. box1.basis[0][2] = 0.0f;
  201. box1.basis[1][0] = 0.0f;
  202. box1.basis[1][1] = 1.0f;
  203. box1.basis[1][2] = 0.0f;
  204. box1.basis[2][0] = 0.0f;
  205. box1.basis[2][1] = 0.0f;
  206. box1.basis[2][2] = 1.0f;
  207. IntersectType type = BoxesIntersect(1.0f,box0,box1);
  208. }
  209. void benchmark_transformations(void)
  210. {
  211. unsigned long high;
  212. unsigned long cycles0;
  213. unsigned long cycles1;
  214. /*
  215. ** Testing speed of the matrix library...
  216. */
  217. Vector v = { 4.0f, -2.5f, 100.4f };
  218. float mat[3][3];
  219. float line[3] = { 0.0f, 0.0f, 1.0f };
  220. float length = float(sqrt(line[0]*line[0]+line[1]*line[1]+line[2]*line[2]));
  221. line[0] /= length;
  222. line[1] /= length;
  223. line[2] /= length;
  224. float angle = (float)DEG_TO_RAD(45.0f);
  225. Rotate3D(line,angle,mat);
  226. Vector3 myv(4.0,-2.5,100.4);
  227. Matrix3 mymat;
  228. mymat[0][0] = mat[0][0];
  229. mymat[0][1] = mat[0][1];
  230. mymat[0][2] = mat[0][2];
  231. mymat[1][0] = mat[1][0];
  232. mymat[1][1] = mat[1][1];
  233. mymat[1][2] = mat[1][2];
  234. mymat[2][0] = mat[2][0];
  235. mymat[2][1] = mat[2][1];
  236. mymat[2][2] = mat[2][2];
  237. Vector res;
  238. Vector3 myres;
  239. cycles0 = Get_CPU_Clock(high);
  240. MultiplyVM (v,mat,res);
  241. cycles0 = Get_CPU_Clock(high) - cycles0;
  242. cycles1 = Get_CPU_Clock(high);
  243. myres = mymat * myv;
  244. cycles1 = Get_CPU_Clock(high) - cycles1;
  245. cout << "c cycles = " << cycles0 << endl;
  246. cout << "c++ cycles = " << cycles1 << endl;
  247. }