math-extra.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <stdlib.h>
  15. #include "vec2.h"
  16. #include "vec3.h"
  17. #include "math-defs.h"
  18. #include "math-extra.h"
  19. void polar_to_cart(struct vec3 *dst, const struct vec3 *v)
  20. {
  21. struct vec3 cart;
  22. float sinx = cosf(v->x);
  23. float sinx_z = v->z * sinx;
  24. cart.x = sinx_z * sinf(v->y);
  25. cart.z = sinx_z * cosf(v->y);
  26. cart.y = v->z * sinf(v->x);
  27. vec3_copy(dst, &cart);
  28. }
  29. void cart_to_polar(struct vec3 *dst, const struct vec3 *v)
  30. {
  31. struct vec3 polar;
  32. polar.z = vec3_len(v);
  33. if (close_float(polar.z, 0.0f, EPSILON)) {
  34. vec3_zero(&polar);
  35. } else {
  36. polar.x = asinf(v->y / polar.z);
  37. polar.y = atan2f(v->x, v->z);
  38. }
  39. vec3_copy(dst, &polar);
  40. }
  41. void norm_to_polar(struct vec2 *dst, const struct vec3 *norm)
  42. {
  43. dst->x = atan2f(norm->x, norm->z);
  44. dst->y = asinf(norm->y);
  45. }
  46. void polar_to_norm(struct vec3 *dst, const struct vec2 *polar)
  47. {
  48. float sinx = sinf(polar->x);
  49. dst->x = sinx * cosf(polar->y);
  50. dst->y = sinx * sinf(polar->y);
  51. dst->z = cosf(polar->x);
  52. }
  53. float calc_torquef(float val1, float val2, float torque, float min_adjust,
  54. float t)
  55. {
  56. float out = val1;
  57. float dist;
  58. bool over;
  59. if (close_float(val1, val2, EPSILON))
  60. return val2;
  61. dist = (val2 - val1) * torque;
  62. over = dist > 0.0f;
  63. if (over) {
  64. if (dist < min_adjust) /* prevents from going too slow */
  65. dist = min_adjust;
  66. out += dist * t; /* add torque */
  67. if (out > val2) /* clamp if overshoot */
  68. out = val2;
  69. } else {
  70. if (dist > -min_adjust)
  71. dist = -min_adjust;
  72. out += dist * t;
  73. if (out < val2)
  74. out = val2;
  75. }
  76. return out;
  77. }
  78. void calc_torque(struct vec3 *dst, const struct vec3 *v1, const struct vec3 *v2,
  79. float torque, float min_adjust, float t)
  80. {
  81. struct vec3 line, dir;
  82. float orig_dist, torque_dist, adjust_dist;
  83. if (vec3_close(v1, v2, EPSILON)) {
  84. vec3_copy(dst, v2);
  85. return;
  86. }
  87. vec3_sub(&line, v2, v1);
  88. orig_dist = vec3_len(&line);
  89. vec3_mulf(&dir, &line, 1.0f / orig_dist);
  90. torque_dist = orig_dist * torque; /* use distance to determine speed */
  91. if (torque_dist < min_adjust) /* prevent from going too slow */
  92. torque_dist = min_adjust;
  93. adjust_dist = torque_dist * t;
  94. if (adjust_dist <= (orig_dist - LARGE_EPSILON)) {
  95. vec3_mulf(dst, &dir, adjust_dist);
  96. vec3_add(dst, dst, v1); /* add torque */
  97. } else {
  98. vec3_copy(dst, v2); /* clamp if overshoot */
  99. }
  100. }
  101. float rand_float(int positive_only)
  102. {
  103. if (positive_only)
  104. return (float)((double)rand() / (double)RAND_MAX);
  105. else
  106. return (float)(((double)rand() / (double)RAND_MAX * 2.0) - 1.0);
  107. }