math-extra.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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, float t)
  54. {
  55. float out = val1;
  56. float dist;
  57. bool over;
  58. if (close_float(val1, val2, EPSILON))
  59. return val2;
  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, const struct vec3 *v2, float torque, float min_adjust,
  78. float t)
  79. {
  80. struct vec3 line, dir;
  81. float orig_dist, torque_dist, adjust_dist;
  82. if (vec3_close(v1, v2, EPSILON)) {
  83. vec3_copy(dst, v2);
  84. return;
  85. }
  86. vec3_sub(&line, v2, v1);
  87. orig_dist = vec3_len(&line);
  88. vec3_mulf(&dir, &line, 1.0f / orig_dist);
  89. torque_dist = orig_dist * torque; /* use distance to determine speed */
  90. if (torque_dist < min_adjust) /* prevent from going too slow */
  91. torque_dist = min_adjust;
  92. adjust_dist = torque_dist * t;
  93. if (adjust_dist <= (orig_dist - LARGE_EPSILON)) {
  94. vec3_mulf(dst, &dir, adjust_dist);
  95. vec3_add(dst, dst, v1); /* add torque */
  96. } else {
  97. vec3_copy(dst, v2); /* clamp if overshoot */
  98. }
  99. }
  100. float rand_float(int positive_only)
  101. {
  102. if (positive_only)
  103. return (float)((double)rand() / (double)RAND_MAX);
  104. else
  105. return (float)(((double)rand() / (double)RAND_MAX * 2.0) - 1.0);
  106. }