q_math.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. // q_math.c -- stateless support routines that are included in each code module
  2. // leave this at the top for PCH reasons...
  3. #include "common_headers.h"
  4. //#include "q_shared.h"
  5. const vec3_t vec3_origin = {0,0,0};
  6. const vec3_t axisDefault[3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
  7. vec4_t colorTable[CT_MAX] =
  8. {
  9. {0, 0, 0, 0}, // CT_NONE
  10. {0, 0, 0, 1}, // CT_BLACK
  11. {1, 0, 0, 1}, // CT_RED
  12. {0, 1, 0, 1}, // CT_GREEN
  13. {0, 0, 1, 1}, // CT_BLUE
  14. {1, 1, 0, 1}, // CT_YELLOW
  15. {1, 0, 1, 1}, // CT_MAGENTA
  16. {0, 1, 1, 1}, // CT_CYAN
  17. {1, 1, 1, 1}, // CT_WHITE
  18. {0.75f, 0.75f, 0.75f, 1}, // CT_LTGREY
  19. {0.50f, 0.50f, 0.50f, 1}, // CT_MDGREY
  20. {0.25f, 0.25f, 0.25f, 1}, // CT_DKGREY
  21. {0.15f, 0.15f, 0.15f, 1}, // CT_DKGREY2
  22. {0.992f, 0.652f, 0.0f, 1}, // CT_VLTORANGE -- needs values
  23. {0.810f, 0.530f, 0.0f, 1}, // CT_LTORANGE
  24. {0.610f, 0.330f, 0.0f, 1}, // CT_DKORANGE
  25. {0.402f, 0.265f, 0.0f, 1}, // CT_VDKORANGE
  26. {0.503f, 0.375f, 0.996f, 1}, // CT_VLTBLUE1
  27. {0.367f, 0.261f, 0.722f, 1}, // CT_LTBLUE1
  28. {0.199f, 0.0f, 0.398f, 1}, // CT_DKBLUE1
  29. {0.160f, 0.117f, 0.324f, 1}, // CT_VDKBLUE1
  30. {0.300f, 0.628f, 0.816f, 1}, // CT_VLTBLUE2 -- needs values
  31. {0.300f, 0.628f, 0.816f, 1}, // CT_LTBLUE2
  32. {0.191f, 0.289f, 0.457f, 1}, // CT_DKBLUE2
  33. {0.125f, 0.250f, 0.324f, 1}, // CT_VDKBLUE2
  34. {0.796f, 0.398f, 0.199f, 1}, // CT_VLTBROWN1 -- needs values
  35. {0.796f, 0.398f, 0.199f, 1}, // CT_LTBROWN1
  36. {0.558f, 0.207f, 0.027f, 1}, // CT_DKBROWN1
  37. {0.328f, 0.125f, 0.035f, 1}, // CT_VDKBROWN1
  38. {0.996f, 0.796f, 0.398f, 1}, // CT_VLTGOLD1 -- needs values
  39. {0.996f, 0.796f, 0.398f, 1}, // CT_LTGOLD1
  40. {0.605f, 0.441f, 0.113f, 1}, // CT_DKGOLD1
  41. {0.386f, 0.308f, 0.148f, 1}, // CT_VDKGOLD1
  42. {0.648f, 0.562f, 0.784f, 1}, // CT_VLTPURPLE1 -- needs values
  43. {0.648f, 0.562f, 0.784f, 1}, // CT_LTPURPLE1
  44. {0.437f, 0.335f, 0.597f, 1}, // CT_DKPURPLE1
  45. {0.308f, 0.269f, 0.375f, 1}, // CT_VDKPURPLE1
  46. {0.816f, 0.531f, 0.710f, 1}, // CT_VLTPURPLE2 -- needs values
  47. {0.816f, 0.531f, 0.710f, 1}, // CT_LTPURPLE2
  48. {0.566f, 0.269f, 0.457f, 1}, // CT_DKPURPLE2
  49. {0.343f, 0.226f, 0.316f, 1}, // CT_VDKPURPLE2
  50. {0.929f, 0.597f, 0.929f, 1}, // CT_VLTPURPLE3
  51. {0.570f, 0.371f, 0.570f, 1}, // CT_LTPURPLE3
  52. {0.355f, 0.199f, 0.355f, 1}, // CT_DKPURPLE3
  53. {0.285f, 0.136f, 0.230f, 1}, // CT_VDKPURPLE3
  54. {0.953f, 0.378f, 0.250f, 1}, // CT_VLTRED1
  55. {0.953f, 0.378f, 0.250f, 1}, // CT_LTRED1
  56. {0.593f, 0.121f, 0.109f, 1}, // CT_DKRED1
  57. {0.429f, 0.171f, 0.113f, 1}, // CT_VDKRED1
  58. {.25f, 0, 0, 1}, // CT_VDKRED
  59. {.70f, 0, 0, 1}, // CT_DKRED
  60. {0.717f, 0.902f, 1.0f, 1}, // CT_VLTAQUA
  61. {0.574f, 0.722f, 0.804f, 1}, // CT_LTAQUA
  62. {0.287f, 0.361f, 0.402f, 1}, // CT_DKAQUA
  63. {0.143f, 0.180f, 0.201f, 1}, // CT_VDKAQUA
  64. {0.871f, 0.386f, 0.375f, 1}, // CT_LTPINK
  65. {0.435f, 0.193f, 0.187f, 1}, // CT_DKPINK
  66. { 0, .5f, .5f, 1}, // CT_LTCYAN
  67. { 0, .25f, .25f, 1}, // CT_DKCYAN
  68. { .179f, .51f, .92f, 1}, // CT_LTBLUE3
  69. { .199f, .71f, .92f, 1}, // CT_LTBLUE3
  70. { .5f, .05f, .4f, 1}, // CT_DKBLUE3
  71. { 0.0f, .613f, .097f, 1}, // CT_HUD_GREEN
  72. { 0.835f, .015f, .015f, 1}, // CT_HUD_RED
  73. { .567f, .685f, 1.0f, .75f}, // CT_ICON_BLUE
  74. { .515f, .406f, .507f, 1}, // CT_NO_AMMO_RED
  75. { 1.0f, .658f, .062f, 1}, // CT_HUD_ORANGE
  76. { 0.549f, .854f, 1.0f, 1.0f}, // CT_TITLE
  77. };
  78. vec4_t g_color_table[8] =
  79. {
  80. {0.0, 0.0, 0.0, 1.0},
  81. {1.0, 0.0, 0.0, 1.0},
  82. {0.0, 1.0, 0.0, 1.0},
  83. {1.0, 1.0, 0.0, 1.0},
  84. {0.0, 0.0, 1.0, 1.0},
  85. {0.0, 1.0, 1.0, 1.0},
  86. {1.0, 0.0, 1.0, 1.0},
  87. {1.0, 1.0, 1.0, 1.0},
  88. };
  89. #pragma warning(disable : 4305) // truncation from const double to float
  90. vec3_t bytedirs[NUMVERTEXNORMALS] =
  91. {
  92. {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188},
  93. {-0.295242, 0.000000, 0.955423}, {-0.309017, 0.500000, 0.809017},
  94. {-0.162460, 0.262866, 0.951056}, {0.000000, 0.000000, 1.000000},
  95. {0.000000, 0.850651, 0.525731}, {-0.147621, 0.716567, 0.681718},
  96. {0.147621, 0.716567, 0.681718}, {0.000000, 0.525731, 0.850651},
  97. {0.309017, 0.500000, 0.809017}, {0.525731, 0.000000, 0.850651},
  98. {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188},
  99. {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567},
  100. {-0.809017, 0.309017, 0.500000},{-0.587785, 0.425325, 0.688191},
  101. {-0.850651, 0.525731, 0.000000},{-0.864188, 0.442863, 0.238856},
  102. {-0.716567, 0.681718, 0.147621},{-0.688191, 0.587785, 0.425325},
  103. {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863},
  104. {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621},
  105. {-0.500000, 0.809017, -0.309017}, {-0.525731, 0.850651, 0.000000},
  106. {0.000000, 0.850651, -0.525731}, {-0.238856, 0.864188, -0.442863},
  107. {0.000000, 0.955423, -0.295242}, {-0.262866, 0.951056, -0.162460},
  108. {0.000000, 1.000000, 0.000000}, {0.000000, 0.955423, 0.295242},
  109. {-0.262866, 0.951056, 0.162460}, {0.238856, 0.864188, 0.442863},
  110. {0.262866, 0.951056, 0.162460}, {0.500000, 0.809017, 0.309017},
  111. {0.238856, 0.864188, -0.442863},{0.262866, 0.951056, -0.162460},
  112. {0.500000, 0.809017, -0.309017},{0.850651, 0.525731, 0.000000},
  113. {0.716567, 0.681718, 0.147621}, {0.716567, 0.681718, -0.147621},
  114. {0.525731, 0.850651, 0.000000}, {0.425325, 0.688191, 0.587785},
  115. {0.864188, 0.442863, 0.238856}, {0.688191, 0.587785, 0.425325},
  116. {0.809017, 0.309017, 0.500000}, {0.681718, 0.147621, 0.716567},
  117. {0.587785, 0.425325, 0.688191}, {0.955423, 0.295242, 0.000000},
  118. {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866},
  119. {0.850651, -0.525731, 0.000000},{0.955423, -0.295242, 0.000000},
  120. {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866},
  121. {0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
  122. {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856},
  123. {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866},
  124. {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567},
  125. {0.681718, -0.147621, -0.716567},{0.850651, 0.000000, -0.525731},
  126. {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856},
  127. {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718},
  128. {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785},
  129. {0.442863, 0.238856, -0.864188}, {0.587785, 0.425325, -0.688191},
  130. {0.688191, 0.587785, -0.425325}, {-0.147621, 0.716567, -0.681718},
  131. {-0.309017, 0.500000, -0.809017}, {0.000000, 0.525731, -0.850651},
  132. {-0.525731, 0.000000, -0.850651}, {-0.442863, 0.238856, -0.864188},
  133. {-0.295242, 0.000000, -0.955423}, {-0.162460, 0.262866, -0.951056},
  134. {0.000000, 0.000000, -1.000000}, {0.295242, 0.000000, -0.955423},
  135. {0.162460, 0.262866, -0.951056}, {-0.442863, -0.238856, -0.864188},
  136. {-0.309017, -0.500000, -0.809017}, {-0.162460, -0.262866, -0.951056},
  137. {0.000000, -0.850651, -0.525731}, {-0.147621, -0.716567, -0.681718},
  138. {0.147621, -0.716567, -0.681718}, {0.000000, -0.525731, -0.850651},
  139. {0.309017, -0.500000, -0.809017}, {0.442863, -0.238856, -0.864188},
  140. {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863},
  141. {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785},
  142. {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325},
  143. {0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
  144. {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460},
  145. {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242},
  146. {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460},
  147. {0.500000, -0.809017, 0.309017}, {0.716567, -0.681718, 0.147621},
  148. {0.525731, -0.850651, 0.000000}, {-0.238856, -0.864188, -0.442863},
  149. {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460},
  150. {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621},
  151. {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000},
  152. {-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
  153. {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856},
  154. {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325},
  155. {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188},
  156. {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017},
  157. {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785},
  158. {-0.162460, -0.262866, 0.951056}, {0.442863, -0.238856, 0.864188},
  159. {0.162460, -0.262866, 0.951056}, {0.309017, -0.500000, 0.809017},
  160. {0.147621, -0.716567, 0.681718}, {0.000000, -0.525731, 0.850651},
  161. {0.425325, -0.688191, 0.587785}, {0.587785, -0.425325, 0.688191},
  162. {0.688191, -0.587785, 0.425325}, {-0.955423, 0.295242, 0.000000},
  163. {-0.951056, 0.162460, 0.262866}, {-1.000000, 0.000000, 0.000000},
  164. {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000},
  165. {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856},
  166. {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000},
  167. {-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
  168. {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567},
  169. {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731},
  170. {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191},
  171. {-0.425325, 0.688191, -0.587785}, {-0.425325, -0.688191, -0.587785},
  172. {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325}
  173. };
  174. #pragma warning(default : 4305) // truncation from const double to float
  175. //==============================================================
  176. //=======================================================
  177. /*
  178. erandom
  179. This function produces a random number with a exponential
  180. distribution and the specified mean value.
  181. */
  182. float erandom( float mean ) {
  183. float r;
  184. do {
  185. r = random();
  186. } while ( r == 0.0 );
  187. return -mean * log( r );
  188. }
  189. signed char ClampChar( int i ) {
  190. if ( i < -128 ) {
  191. return -128;
  192. }
  193. if ( i > 127 ) {
  194. return 127;
  195. }
  196. return i;
  197. }
  198. signed short ClampShort( int i ) {
  199. if ( i < (short)0x8000 ) {
  200. return (short)0x8000;
  201. }
  202. if ( i > 0x7fff ) {
  203. return 0x7fff;
  204. }
  205. return i;
  206. }
  207. // this isn't a real cheap function to call!
  208. int DirToByte( vec3_t dir ) {
  209. int i, best;
  210. float d, bestd;
  211. if ( !dir ) {
  212. return 0;
  213. }
  214. bestd = 0;
  215. best = 0;
  216. for (i=0 ; i<NUMVERTEXNORMALS ; i++)
  217. {
  218. d = DotProduct (dir, bytedirs[i]);
  219. if (d > bestd)
  220. {
  221. bestd = d;
  222. best = i;
  223. }
  224. }
  225. return best;
  226. }
  227. void ByteToDir( int b, vec3_t dir ) {
  228. if ( b < 0 || b >= NUMVERTEXNORMALS ) {
  229. VectorCopy( vec3_origin, dir );
  230. return;
  231. }
  232. VectorCopy (bytedirs[b], dir);
  233. }
  234. unsigned ColorBytes3 (float r, float g, float b) {
  235. unsigned i;
  236. ( (byte *)&i )[0] = r * 255;
  237. ( (byte *)&i )[1] = g * 255;
  238. ( (byte *)&i )[2] = b * 255;
  239. return i;
  240. }
  241. unsigned ColorBytes4 (float r, float g, float b, float a) {
  242. unsigned i;
  243. ( (byte *)&i )[0] = r * 255;
  244. ( (byte *)&i )[1] = g * 255;
  245. ( (byte *)&i )[2] = b * 255;
  246. ( (byte *)&i )[3] = a * 255;
  247. return i;
  248. }
  249. float NormalizeColor( const vec3_t in, vec3_t out ) {
  250. float max;
  251. max = in[0];
  252. if ( in[1] > max ) {
  253. max = in[1];
  254. }
  255. if ( in[2] > max ) {
  256. max = in[2];
  257. }
  258. if ( !max ) {
  259. VectorClear( out );
  260. } else {
  261. out[0] = in[0] / max;
  262. out[1] = in[1] / max;
  263. out[2] = in[2] / max;
  264. }
  265. return max;
  266. }
  267. void VectorAdvance( const vec3_t veca, const float scale, const vec3_t vecb, vec3_t vecc)
  268. {
  269. vecc[0] = veca[0] + (scale * (vecb[0] - veca[0]));
  270. vecc[1] = veca[1] + (scale * (vecb[1] - veca[1]));
  271. vecc[2] = veca[2] + (scale * (vecb[2] - veca[2]));
  272. }
  273. //============================================================================
  274. /*
  275. =====================
  276. PlaneFromPoints
  277. Returns false if the triangle is degenrate.
  278. The normal will point out of the clock for clockwise ordered points
  279. =====================
  280. */
  281. qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
  282. vec3_t d1, d2;
  283. VectorSubtract( b, a, d1 );
  284. VectorSubtract( c, a, d2 );
  285. CrossProduct( d2, d1, plane );
  286. if ( VectorNormalize( plane ) == 0 ) {
  287. return qfalse;
  288. }
  289. plane[3] = DotProduct( a, plane );
  290. return qtrue;
  291. }
  292. #ifdef _XBOX
  293. qboolean PlaneFromPoints( vec4_t plane, const short a[3], const short b[3], const short c[3] ) {
  294. vec3_t d1, d2;
  295. VectorSubtract( b, a, d1 );
  296. VectorSubtract( c, a, d2 );
  297. CrossProduct( d2, d1, plane );
  298. if ( VectorNormalize( plane ) == 0 ) {
  299. return qfalse;
  300. }
  301. plane[3] = DotProduct( a, plane );
  302. return qtrue;
  303. }
  304. #endif
  305. /*
  306. ===============
  307. RotatePointAroundVector
  308. This is not implemented very well...
  309. ===============
  310. */
  311. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
  312. float degrees ) {
  313. float m[3][3];
  314. float im[3][3];
  315. float zrot[3][3];
  316. float tmpmat[3][3];
  317. float rot[3][3];
  318. int i;
  319. vec3_t vr, vup, vf;
  320. float rad;
  321. vf[0] = dir[0];
  322. vf[1] = dir[1];
  323. vf[2] = dir[2];
  324. PerpendicularVector( vr, dir );
  325. CrossProduct( vr, vf, vup );
  326. m[0][0] = vr[0];
  327. m[1][0] = vr[1];
  328. m[2][0] = vr[2];
  329. m[0][1] = vup[0];
  330. m[1][1] = vup[1];
  331. m[2][1] = vup[2];
  332. m[0][2] = vf[0];
  333. m[1][2] = vf[1];
  334. m[2][2] = vf[2];
  335. memcpy( im, m, sizeof( im ) );
  336. im[0][1] = m[1][0];
  337. im[0][2] = m[2][0];
  338. im[1][0] = m[0][1];
  339. im[1][2] = m[2][1];
  340. im[2][0] = m[0][2];
  341. im[2][1] = m[1][2];
  342. memset( zrot, 0, sizeof( zrot ) );
  343. zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
  344. rad = DEG2RAD( degrees );
  345. zrot[0][0] = cos( rad );
  346. zrot[0][1] = sin( rad );
  347. zrot[1][0] = -sin( rad );
  348. zrot[1][1] = cos( rad );
  349. MatrixMultiply( m, zrot, tmpmat );
  350. MatrixMultiply( tmpmat, im, rot );
  351. for ( i = 0; i < 3; i++ ) {
  352. dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
  353. }
  354. }
  355. /*
  356. ===============
  357. RotateAroundDirection
  358. ===============
  359. */
  360. void RotateAroundDirection( vec3_t axis[3], float yaw ) {
  361. // create an arbitrary axis[1]
  362. PerpendicularVector( axis[1], axis[0] );
  363. // rotate it around axis[0] by yaw
  364. if ( yaw ) {
  365. vec3_t temp;
  366. VectorCopy( axis[1], temp );
  367. RotatePointAroundVector( axis[1], axis[0], temp, yaw );
  368. }
  369. // cross to get axis[2]
  370. CrossProduct( axis[0], axis[1], axis[2] );
  371. }
  372. void vectoangles( const vec3_t value1, vec3_t angles ) {
  373. float forward;
  374. float yaw, pitch;
  375. if ( value1[1] == 0 && value1[0] == 0 ) {
  376. yaw = 0;
  377. if ( value1[2] > 0 ) {
  378. pitch = 90;
  379. }
  380. else {
  381. pitch = 270;
  382. }
  383. }
  384. else {
  385. if ( value1[0] ) {
  386. yaw = ( atan2 ( value1[1], value1[0] ) * 180 / M_PI );
  387. }
  388. else if ( value1[1] > 0 ) {
  389. yaw = 90;
  390. }
  391. else {
  392. yaw = 270;
  393. }
  394. if ( yaw < 0 ) {
  395. yaw += 360;
  396. }
  397. forward = sqrt ( value1[0]*value1[0] + value1[1]*value1[1] );
  398. pitch = ( atan2(value1[2], forward) * 180 / M_PI );
  399. if ( pitch < 0 ) {
  400. pitch += 360;
  401. }
  402. }
  403. angles[PITCH] = -pitch;
  404. angles[YAW] = yaw;
  405. angles[ROLL] = 0;
  406. }
  407. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
  408. {
  409. float d;
  410. vec3_t n;
  411. float inv_denom;
  412. inv_denom = 1.0F / DotProduct( normal, normal );
  413. d = DotProduct( normal, p ) * inv_denom;
  414. n[0] = normal[0] * inv_denom;
  415. n[1] = normal[1] * inv_denom;
  416. n[2] = normal[2] * inv_denom;
  417. dst[0] = p[0] - d * n[0];
  418. dst[1] = p[1] - d * n[1];
  419. dst[2] = p[2] - d * n[2];
  420. }
  421. /*
  422. ================
  423. MakeNormalVectors
  424. Given a normalized forward vector, create two
  425. other perpendicular vectors
  426. ================
  427. */
  428. void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up) {
  429. float d;
  430. // this rotate and negate guarantees a vector
  431. // not colinear with the original
  432. right[1] = -forward[0];
  433. right[2] = forward[1];
  434. right[0] = forward[2];
  435. d = DotProduct (right, forward);
  436. VectorMA (right, -d, forward, right);
  437. VectorNormalize (right);
  438. CrossProduct (right, forward, up);
  439. }
  440. //============================================================================
  441. /*
  442. ** float q_rsqrt( float number )
  443. */
  444. float Q_rsqrt( float number )
  445. {
  446. long i;
  447. float x2, y;
  448. const float threehalfs = 1.5F;
  449. x2 = number * 0.5F;
  450. y = number;
  451. i = * ( long * ) &y; // evil floating point bit level hacking
  452. i = 0x5f3759df - ( i >> 1 ); // what the fuck?
  453. y = * ( float * ) &i;
  454. y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  455. // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
  456. return y;
  457. }
  458. float Q_fabs( float f ) {
  459. int tmp = * ( int * ) &f;
  460. tmp &= 0x7FFFFFFF;
  461. return * ( float * ) &tmp;
  462. }
  463. //============================================================
  464. //float AngleMod(float a) {
  465. // a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
  466. // return a;
  467. //}
  468. //============================================================
  469. /*
  470. =================
  471. SetPlaneSignbits
  472. =================
  473. */
  474. void SetPlaneSignbits (cplane_t *out) {
  475. int bits, j;
  476. // for fast box on planeside test
  477. bits = 0;
  478. for (j=0 ; j<3 ; j++) {
  479. if (out->normal[j] < 0) {
  480. bits |= 1<<j;
  481. }
  482. }
  483. out->signbits = bits;
  484. }
  485. /*
  486. ==================
  487. BoxOnPlaneSide
  488. Returns 1, 2, or 1 + 2
  489. // this is the slow, general version
  490. int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  491. {
  492. int i;
  493. float dist1, dist2;
  494. int sides;
  495. vec3_t corners[2];
  496. for (i=0 ; i<3 ; i++)
  497. {
  498. if (p->normal[i] < 0)
  499. {
  500. corners[0][i] = emins[i];
  501. corners[1][i] = emaxs[i];
  502. }
  503. else
  504. {
  505. corners[1][i] = emins[i];
  506. corners[0][i] = emaxs[i];
  507. }
  508. }
  509. dist1 = DotProduct (p->normal, corners[0]) - p->dist;
  510. dist2 = DotProduct (p->normal, corners[1]) - p->dist;
  511. sides = 0;
  512. if (dist1 >= 0)
  513. sides = 1;
  514. if (dist2 < 0)
  515. sides |= 2;
  516. return sides;
  517. }
  518. ==================
  519. */
  520. #if !(defined __linux__ && defined __i386__) || defined __LCC__
  521. #if !id386
  522. int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  523. {
  524. float dist1, dist2;
  525. int sides;
  526. // fast axial cases
  527. if (p->type < 3)
  528. {
  529. if (p->dist <= emins[p->type])
  530. return 1;
  531. if (p->dist >= emaxs[p->type])
  532. return 2;
  533. return 3;
  534. }
  535. // general case
  536. switch (p->signbits)
  537. {
  538. case 0:
  539. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  540. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  541. break;
  542. case 1:
  543. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  544. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  545. break;
  546. case 2:
  547. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  548. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  549. break;
  550. case 3:
  551. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  552. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  553. break;
  554. case 4:
  555. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  556. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  557. break;
  558. case 5:
  559. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  560. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  561. break;
  562. case 6:
  563. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  564. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  565. break;
  566. case 7:
  567. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  568. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  569. break;
  570. default:
  571. dist1 = dist2 = 0; // shut up compiler
  572. break;
  573. }
  574. sides = 0;
  575. if (dist1 >= p->dist)
  576. sides = 1;
  577. if (dist2 < p->dist)
  578. sides |= 2;
  579. return sides;
  580. }
  581. #else
  582. #pragma warning( disable: 4035 )
  583. __declspec( naked ) int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  584. {
  585. static int bops_initialized;
  586. static int Ljmptab[8];
  587. __asm {
  588. push ebx
  589. cmp bops_initialized, 1
  590. je initialized
  591. mov bops_initialized, 1
  592. mov Ljmptab[0*4], offset Lcase0
  593. mov Ljmptab[1*4], offset Lcase1
  594. mov Ljmptab[2*4], offset Lcase2
  595. mov Ljmptab[3*4], offset Lcase3
  596. mov Ljmptab[4*4], offset Lcase4
  597. mov Ljmptab[5*4], offset Lcase5
  598. mov Ljmptab[6*4], offset Lcase6
  599. mov Ljmptab[7*4], offset Lcase7
  600. initialized:
  601. mov edx,dword ptr[4+12+esp]
  602. mov ecx,dword ptr[4+4+esp]
  603. xor eax,eax
  604. mov ebx,dword ptr[4+8+esp]
  605. mov al,byte ptr[17+edx]
  606. cmp al,8
  607. jge Lerror
  608. fld dword ptr[0+edx]
  609. fld st(0)
  610. jmp dword ptr[Ljmptab+eax*4]
  611. Lcase0:
  612. fmul dword ptr[ebx]
  613. fld dword ptr[0+4+edx]
  614. fxch st(2)
  615. fmul dword ptr[ecx]
  616. fxch st(2)
  617. fld st(0)
  618. fmul dword ptr[4+ebx]
  619. fld dword ptr[0+8+edx]
  620. fxch st(2)
  621. fmul dword ptr[4+ecx]
  622. fxch st(2)
  623. fld st(0)
  624. fmul dword ptr[8+ebx]
  625. fxch st(5)
  626. faddp st(3),st(0)
  627. fmul dword ptr[8+ecx]
  628. fxch st(1)
  629. faddp st(3),st(0)
  630. fxch st(3)
  631. faddp st(2),st(0)
  632. jmp LSetSides
  633. Lcase1:
  634. fmul dword ptr[ecx]
  635. fld dword ptr[0+4+edx]
  636. fxch st(2)
  637. fmul dword ptr[ebx]
  638. fxch st(2)
  639. fld st(0)
  640. fmul dword ptr[4+ebx]
  641. fld dword ptr[0+8+edx]
  642. fxch st(2)
  643. fmul dword ptr[4+ecx]
  644. fxch st(2)
  645. fld st(0)
  646. fmul dword ptr[8+ebx]
  647. fxch st(5)
  648. faddp st(3),st(0)
  649. fmul dword ptr[8+ecx]
  650. fxch st(1)
  651. faddp st(3),st(0)
  652. fxch st(3)
  653. faddp st(2),st(0)
  654. jmp LSetSides
  655. Lcase2:
  656. fmul dword ptr[ebx]
  657. fld dword ptr[0+4+edx]
  658. fxch st(2)
  659. fmul dword ptr[ecx]
  660. fxch st(2)
  661. fld st(0)
  662. fmul dword ptr[4+ecx]
  663. fld dword ptr[0+8+edx]
  664. fxch st(2)
  665. fmul dword ptr[4+ebx]
  666. fxch st(2)
  667. fld st(0)
  668. fmul dword ptr[8+ebx]
  669. fxch st(5)
  670. faddp st(3),st(0)
  671. fmul dword ptr[8+ecx]
  672. fxch st(1)
  673. faddp st(3),st(0)
  674. fxch st(3)
  675. faddp st(2),st(0)
  676. jmp LSetSides
  677. Lcase3:
  678. fmul dword ptr[ecx]
  679. fld dword ptr[0+4+edx]
  680. fxch st(2)
  681. fmul dword ptr[ebx]
  682. fxch st(2)
  683. fld st(0)
  684. fmul dword ptr[4+ecx]
  685. fld dword ptr[0+8+edx]
  686. fxch st(2)
  687. fmul dword ptr[4+ebx]
  688. fxch st(2)
  689. fld st(0)
  690. fmul dword ptr[8+ebx]
  691. fxch st(5)
  692. faddp st(3),st(0)
  693. fmul dword ptr[8+ecx]
  694. fxch st(1)
  695. faddp st(3),st(0)
  696. fxch st(3)
  697. faddp st(2),st(0)
  698. jmp LSetSides
  699. Lcase4:
  700. fmul dword ptr[ebx]
  701. fld dword ptr[0+4+edx]
  702. fxch st(2)
  703. fmul dword ptr[ecx]
  704. fxch st(2)
  705. fld st(0)
  706. fmul dword ptr[4+ebx]
  707. fld dword ptr[0+8+edx]
  708. fxch st(2)
  709. fmul dword ptr[4+ecx]
  710. fxch st(2)
  711. fld st(0)
  712. fmul dword ptr[8+ecx]
  713. fxch st(5)
  714. faddp st(3),st(0)
  715. fmul dword ptr[8+ebx]
  716. fxch st(1)
  717. faddp st(3),st(0)
  718. fxch st(3)
  719. faddp st(2),st(0)
  720. jmp LSetSides
  721. Lcase5:
  722. fmul dword ptr[ecx]
  723. fld dword ptr[0+4+edx]
  724. fxch st(2)
  725. fmul dword ptr[ebx]
  726. fxch st(2)
  727. fld st(0)
  728. fmul dword ptr[4+ebx]
  729. fld dword ptr[0+8+edx]
  730. fxch st(2)
  731. fmul dword ptr[4+ecx]
  732. fxch st(2)
  733. fld st(0)
  734. fmul dword ptr[8+ecx]
  735. fxch st(5)
  736. faddp st(3),st(0)
  737. fmul dword ptr[8+ebx]
  738. fxch st(1)
  739. faddp st(3),st(0)
  740. fxch st(3)
  741. faddp st(2),st(0)
  742. jmp LSetSides
  743. Lcase6:
  744. fmul dword ptr[ebx]
  745. fld dword ptr[0+4+edx]
  746. fxch st(2)
  747. fmul dword ptr[ecx]
  748. fxch st(2)
  749. fld st(0)
  750. fmul dword ptr[4+ecx]
  751. fld dword ptr[0+8+edx]
  752. fxch st(2)
  753. fmul dword ptr[4+ebx]
  754. fxch st(2)
  755. fld st(0)
  756. fmul dword ptr[8+ecx]
  757. fxch st(5)
  758. faddp st(3),st(0)
  759. fmul dword ptr[8+ebx]
  760. fxch st(1)
  761. faddp st(3),st(0)
  762. fxch st(3)
  763. faddp st(2),st(0)
  764. jmp LSetSides
  765. Lcase7:
  766. fmul dword ptr[ecx]
  767. fld dword ptr[0+4+edx]
  768. fxch st(2)
  769. fmul dword ptr[ebx]
  770. fxch st(2)
  771. fld st(0)
  772. fmul dword ptr[4+ecx]
  773. fld dword ptr[0+8+edx]
  774. fxch st(2)
  775. fmul dword ptr[4+ebx]
  776. fxch st(2)
  777. fld st(0)
  778. fmul dword ptr[8+ecx]
  779. fxch st(5)
  780. faddp st(3),st(0)
  781. fmul dword ptr[8+ebx]
  782. fxch st(1)
  783. faddp st(3),st(0)
  784. fxch st(3)
  785. faddp st(2),st(0)
  786. LSetSides:
  787. faddp st(2),st(0)
  788. fcomp dword ptr[12+edx]
  789. xor ecx,ecx
  790. fnstsw ax
  791. fcomp dword ptr[12+edx]
  792. and ah,1
  793. xor ah,1
  794. add cl,ah
  795. fnstsw ax
  796. and ah,1
  797. add ah,ah
  798. add cl,ah
  799. pop ebx
  800. mov eax,ecx
  801. ret
  802. Lerror:
  803. int 3
  804. }
  805. }
  806. #pragma warning( default: 4035 )
  807. #endif
  808. #endif
  809. /*
  810. =================
  811. RadiusFromBounds
  812. =================
  813. */
  814. float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
  815. int i;
  816. vec3_t corner;
  817. float a, b;
  818. for (i=0 ; i<3 ; i++) {
  819. a = Q_fabs( mins[i] );
  820. b = Q_fabs( maxs[i] );
  821. corner[i] = a > b ? a : b;
  822. }
  823. return VectorLength (corner);
  824. }
  825. void ClearBounds( vec3_t mins, vec3_t maxs ) {
  826. mins[0] = mins[1] = mins[2] = WORLD_SIZE; //99999; // I used WORLD_SIZE instead of MAX_WORLD_COORD...
  827. maxs[0] = maxs[1] = maxs[2] = -WORLD_SIZE; //-99999; // ... so it would definately be beyond furthese legal.
  828. }
  829. vec_t DistanceHorizontal( const vec3_t p1, const vec3_t p2 ) {
  830. vec3_t v;
  831. VectorSubtract( p2, p1, v );
  832. return sqrt( v[0]*v[0] + v[1]*v[1] ); //Leave off the z component
  833. }
  834. vec_t DistanceHorizontalSquared( const vec3_t p1, const vec3_t p2 ) {
  835. vec3_t v;
  836. VectorSubtract( p2, p1, v );
  837. return v[0]*v[0] + v[1]*v[1]; //Leave off the z component
  838. }
  839. int Q_log2( int val ) {
  840. int answer;
  841. answer = 0;
  842. while ( ( val>>=1 ) != 0 ) {
  843. answer++;
  844. }
  845. return answer;
  846. }
  847. /*
  848. =================
  849. PlaneTypeForNormal
  850. =================
  851. */
  852. int PlaneTypeForNormal (vec3_t normal) {
  853. if ( normal[0] == 1.0 )
  854. return PLANE_X;
  855. if ( normal[1] == 1.0 )
  856. return PLANE_Y;
  857. if ( normal[2] == 1.0 )
  858. return PLANE_Z;
  859. return PLANE_NON_AXIAL;
  860. }
  861. /*
  862. ================
  863. MatrixMultiply
  864. ================
  865. */
  866. void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
  867. out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  868. in1[0][2] * in2[2][0];
  869. out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  870. in1[0][2] * in2[2][1];
  871. out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  872. in1[0][2] * in2[2][2];
  873. out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  874. in1[1][2] * in2[2][0];
  875. out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  876. in1[1][2] * in2[2][1];
  877. out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  878. in1[1][2] * in2[2][2];
  879. out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  880. in1[2][2] * in2[2][0];
  881. out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  882. in1[2][2] * in2[2][1];
  883. out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  884. in1[2][2] * in2[2][2];
  885. }
  886. void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) {
  887. float angle;
  888. static float sr, sp, sy, cr, cp, cy;
  889. // static to help MS compiler fp bugs
  890. angle = angles[YAW] * (M_PI*2 / 360.0);
  891. sy = sin(angle);
  892. cy = cos(angle);
  893. angle = angles[PITCH] * (M_PI*2 / 360.0);
  894. sp = sin(angle);
  895. cp = cos(angle);
  896. if (forward)
  897. {
  898. forward[0] = cp*cy;
  899. forward[1] = cp*sy;
  900. forward[2] = -sp;
  901. }
  902. if (right || up)
  903. {
  904. angle = angles[ROLL] * (M_PI*2 / 360.0);
  905. sr = sin(angle);
  906. cr = cos(angle);
  907. if (right)
  908. {
  909. right[0] = (-sr*sp*cy + cr*sy);
  910. right[1] = (-sr*sp*sy + -cr*cy);
  911. right[2] = -sr*cp;
  912. }
  913. if (up)
  914. {
  915. up[0] = (cr*sp*cy + sr*sy);
  916. up[1] = (cr*sp*sy + -sr*cy);
  917. up[2] = cr*cp;
  918. }
  919. }
  920. }
  921. /*
  922. ** assumes "src" is normalized
  923. */
  924. void PerpendicularVector( vec3_t dst, const vec3_t src )
  925. {
  926. int pos;
  927. int i;
  928. float minelem = 1.0F;
  929. vec3_t tempvec;
  930. /*
  931. ** find the smallest magnitude axially aligned vector
  932. ** bias towards using z instead of x or y
  933. */
  934. for ( pos = 0, i = 2; i >= 0; i-- )
  935. {
  936. if ( Q_fabs( src[i] ) < minelem )
  937. {
  938. pos = i;
  939. minelem = Q_fabs( src[i] );
  940. }
  941. }
  942. tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
  943. tempvec[pos] = 1.0F;
  944. /*
  945. ** project the point onto the plane defined by src
  946. */
  947. ProjectPointOnPlane( dst, tempvec, src );
  948. /*
  949. ** normalize the result
  950. */
  951. VectorNormalize( dst );
  952. }
  953. /*
  954. -------------------------
  955. DotProductNormalize
  956. -------------------------
  957. */
  958. float DotProductNormalize( const vec3_t inVec1, const vec3_t inVec2 )
  959. {
  960. vec3_t v1, v2;
  961. VectorNormalize2( inVec1, v1 );
  962. VectorNormalize2( inVec2, v2 );
  963. return DotProduct(v1, v2);
  964. }
  965. /*
  966. -------------------------
  967. G_FindClosestPointOnLineSegment
  968. -------------------------
  969. */
  970. qboolean G_FindClosestPointOnLineSegment( const vec3_t start, const vec3_t end, const vec3_t from, vec3_t result )
  971. {
  972. vec3_t vecStart2From, vecStart2End, vecEnd2Start, vecEnd2From;
  973. float distEnd2From, distEnd2Result, theta, cos_theta;
  974. //Find the perpendicular vector to vec from start to end
  975. VectorSubtract( from, start, vecStart2From);
  976. VectorSubtract( end, start, vecStart2End);
  977. float dot = DotProductNormalize( vecStart2From, vecStart2End );
  978. if ( dot <= 0 )
  979. {
  980. //The perpendicular would be beyond or through the start point
  981. VectorCopy( start, result );
  982. return qfalse;
  983. }
  984. if ( dot == 1 )
  985. {
  986. //parallel, closer of 2 points will be the target
  987. if( (VectorLengthSquared( vecStart2From )) < (VectorLengthSquared( vecStart2End )) )
  988. {
  989. VectorCopy( from, result );
  990. }
  991. else
  992. {
  993. VectorCopy( end, result );
  994. }
  995. return qfalse;
  996. }
  997. //Try other end
  998. VectorSubtract( from, end, vecEnd2From);
  999. VectorSubtract( start, end, vecEnd2Start);
  1000. dot = DotProductNormalize( vecEnd2From, vecEnd2Start );
  1001. if ( dot <= 0 )
  1002. {//The perpendicular would be beyond or through the start point
  1003. VectorCopy( end, result );
  1004. return qfalse;
  1005. }
  1006. if ( dot == 1 )
  1007. {//parallel, closer of 2 points will be the target
  1008. if( (VectorLengthSquared( vecEnd2From )) < (VectorLengthSquared( vecEnd2Start )))
  1009. {
  1010. VectorCopy( from, result );
  1011. }
  1012. else
  1013. {
  1014. VectorCopy( end, result );
  1015. }
  1016. return qfalse;
  1017. }
  1018. // /|
  1019. // c / |
  1020. // / |a
  1021. // theta /)__|
  1022. // b
  1023. //cos(theta) = b / c
  1024. //solve for b
  1025. //b = cos(theta) * c
  1026. //angle between vecs end2from and end2start, should be between 0 and 90
  1027. theta = 90 * (1 - dot);//theta
  1028. //Get length of side from End2Result using sine of theta
  1029. distEnd2From = VectorLength( vecEnd2From );//c
  1030. cos_theta = cos(DEG2RAD(theta));//cos(theta)
  1031. distEnd2Result = cos_theta * distEnd2From;//b
  1032. //Extrapolate to find result
  1033. VectorNormalize( vecEnd2Start );
  1034. VectorMA( end, distEnd2Result, vecEnd2Start, result );
  1035. //perpendicular intersection is between the 2 endpoints
  1036. return qtrue;
  1037. }
  1038. float G_PointDistFromLineSegment( const vec3_t start, const vec3_t end, const vec3_t from )
  1039. {
  1040. vec3_t vecStart2From, vecStart2End, vecEnd2Start, vecEnd2From, intersection;
  1041. float distEnd2From, distStart2From, distEnd2Result, theta, cos_theta;
  1042. //Find the perpendicular vector to vec from start to end
  1043. VectorSubtract( from, start, vecStart2From);
  1044. VectorSubtract( end, start, vecStart2End);
  1045. VectorSubtract( from, end, vecEnd2From);
  1046. VectorSubtract( start, end, vecEnd2Start);
  1047. float dot = DotProductNormalize( vecStart2From, vecStart2End );
  1048. distStart2From = Distance( start, from );
  1049. distEnd2From = Distance( end, from );
  1050. if ( dot <= 0 )
  1051. {
  1052. //The perpendicular would be beyond or through the start point
  1053. return distStart2From;
  1054. }
  1055. if ( dot == 1 )
  1056. {
  1057. //parallel, closer of 2 points will be the target
  1058. return ((distStart2From<distEnd2From)?distStart2From:distEnd2From);
  1059. }
  1060. //Try other end
  1061. dot = DotProductNormalize( vecEnd2From, vecEnd2Start );
  1062. if ( dot <= 0 )
  1063. {//The perpendicular would be beyond or through the end point
  1064. return distEnd2From;
  1065. }
  1066. if ( dot == 1 )
  1067. {//parallel, closer of 2 points will be the target
  1068. return ((distStart2From<distEnd2From)?distStart2From:distEnd2From);
  1069. }
  1070. // /|
  1071. // c / |
  1072. // / |a
  1073. // theta /)__|
  1074. // b
  1075. //cos(theta) = b / c
  1076. //solve for b
  1077. //b = cos(theta) * c
  1078. //angle between vecs end2from and end2start, should be between 0 and 90
  1079. theta = 90 * (1 - dot);//theta
  1080. //Get length of side from End2Result using sine of theta
  1081. cos_theta = cos(DEG2RAD(theta));//cos(theta)
  1082. distEnd2Result = cos_theta * distEnd2From;//b
  1083. //Extrapolate to find result
  1084. VectorNormalize( vecEnd2Start );
  1085. VectorMA( end, distEnd2Result, vecEnd2Start, intersection );
  1086. //perpendicular intersection is between the 2 endpoints, return dist to it from from
  1087. return Distance( intersection, from );
  1088. }