math_vector.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. //#include "../game/q_shared.h"
  19. #include "math_vector.h"
  20. #include <assert.h>
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <time.h>
  27. #include <ctype.h>
  28. #define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
  29. #define LERP_DELTA 1e-6
  30. idVec3_t vec_zero( 0.0f, 0.0f, 0.0f );
  31. Bounds boundsZero;
  32. float idVec3_t::toYaw( void ) {
  33. float yaw;
  34. if ( ( y == 0 ) && ( x == 0 ) ) {
  35. yaw = 0;
  36. } else {
  37. yaw = atan2( y, x ) * 180 / M_PI;
  38. if ( yaw < 0 ) {
  39. yaw += 360;
  40. }
  41. }
  42. return yaw;
  43. }
  44. float idVec3_t::toPitch( void ) {
  45. float forward;
  46. float pitch;
  47. if ( ( x == 0 ) && ( y == 0 ) ) {
  48. if ( z > 0 ) {
  49. pitch = 90;
  50. } else {
  51. pitch = 270;
  52. }
  53. } else {
  54. forward = ( float )idSqrt( x * x + y * y );
  55. pitch = atan2( z, forward ) * 180 / M_PI;
  56. if ( pitch < 0 ) {
  57. pitch += 360;
  58. }
  59. }
  60. return pitch;
  61. }
  62. /*
  63. angles_t idVec3_t::toAngles( void ) {
  64. float forward;
  65. float yaw;
  66. float pitch;
  67. if ( ( x == 0 ) && ( y == 0 ) ) {
  68. yaw = 0;
  69. if ( z > 0 ) {
  70. pitch = 90;
  71. } else {
  72. pitch = 270;
  73. }
  74. } else {
  75. yaw = atan2( y, x ) * 180 / M_PI;
  76. if ( yaw < 0 ) {
  77. yaw += 360;
  78. }
  79. forward = ( float )idSqrt( x * x + y * y );
  80. pitch = atan2( z, forward ) * 180 / M_PI;
  81. if ( pitch < 0 ) {
  82. pitch += 360;
  83. }
  84. }
  85. return angles_t( -pitch, yaw, 0 );
  86. }
  87. */
  88. idVec3_t LerpVector( idVec3_t &w1, idVec3_t &w2, const float t ) {
  89. float omega, cosom, sinom, scale0, scale1;
  90. cosom = w1 * w2;
  91. if ( ( 1.0 - cosom ) > LERP_DELTA ) {
  92. omega = acos( cosom );
  93. sinom = sin( omega );
  94. scale0 = sin( ( 1.0 - t ) * omega ) / sinom;
  95. scale1 = sin( t * omega ) / sinom;
  96. } else {
  97. scale0 = 1.0 - t;
  98. scale1 = t;
  99. }
  100. return ( w1 * scale0 + w2 * scale1 );
  101. }
  102. /*
  103. =============
  104. idVec3_t::string
  105. This is just a convenience function
  106. for printing vectors
  107. =============
  108. */
  109. char *idVec3_t::string( void ) {
  110. static int index = 0;
  111. static char str[ 8 ][ 36 ];
  112. char *s;
  113. // use an array so that multiple toString's won't collide
  114. s = str[ index ];
  115. index = (index + 1)&7;
  116. sprintf( s, "%.2f %.2f %.2f", x, y, z );
  117. return s;
  118. }