math-extra.c 3.2 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 3 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-extra.h"
  18. void polar_to_cart(struct vec3 *dst, const struct vec3 *v)
  19. {
  20. struct vec3 cart;
  21. float sinx = cosf(v->x);
  22. float sinx_z = v->z * sinx;
  23. cart.x = sinx_z * sinf(v->y);
  24. cart.z = sinx_z * cosf(v->y);
  25. cart.y = v->z * sinf(v->x);
  26. vec3_copy(dst, &cart);
  27. }
  28. void cart_to_polar(struct vec3 *dst, const struct vec3 *v)
  29. {
  30. struct vec3 polar;
  31. polar.z = vec3_len(v);
  32. if (close_float(polar.z, 0.0f, EPSILON)) {
  33. vec3_zero(&polar);
  34. } else {
  35. polar.x = asinf(v->y / polar.z);
  36. polar.y = atan2f(v->x, v->z);
  37. }
  38. vec3_copy(dst, &polar);
  39. }
  40. void norm_to_polar(struct vec2 *dst, const struct vec3 *norm)
  41. {
  42. dst->x = atan2f(norm->x, norm->z);
  43. dst->y = asinf(norm->y);
  44. }
  45. void polar_to_norm(struct vec3 *dst, const struct vec2 *polar)
  46. {
  47. float sinx = sinf(polar->x);
  48. dst->x = sinx * cosf(polar->y);
  49. dst->y = sinx * sinf(polar->y);
  50. dst->z = cosf(polar->x);
  51. }
  52. float calc_torquef(float val1, float val2, float torque, float min_adjust,
  53. float t)
  54. {
  55. float out = val1;
  56. float dist;
  57. bool over;
  58. if (val1 == val2)
  59. return val1;
  60. dist = (val2-val1)*torque;
  61. over = dist > 0.0f;
  62. if (over) {
  63. if (dist < min_adjust) /* prevents from going too slow */
  64. dist = min_adjust;
  65. out += dist*t; /* add torque */
  66. if (out > val2) /* clamp if overshoot */
  67. out = val2;
  68. } else {
  69. if (dist > -min_adjust)
  70. dist = -min_adjust;
  71. out += dist*t;
  72. if (out < val2)
  73. out = val2;
  74. }
  75. return out;
  76. }
  77. void calc_torque(struct vec3 *dst, const struct vec3 *v1,
  78. const struct vec3 *v2, float torque, float min_adjust,
  79. 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, v1);
  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. adjust_dist = torque_dist*t;
  92. if (torque_dist < min_adjust) /* prevent from going too slow */
  93. torque_dist = min_adjust;
  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. }