Jo Engine  9
Jo Sega Saturn Engine
math.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2017, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
35 #ifndef __JO_MATH_H__
36 # define __JO_MATH_H__
37 
38 #ifdef JO_COMPILE_WITH_SINUS_TABLE
39 
43 extern int JoSinLookupTable[360];
44 
45 #endif
46 
50 extern int JoCosLookupTable[360];
51 
56 # define JO_ZERO(X) X ^= X
57 
58 # define JO_FIXED_0 (0)
59 
60 # define JO_FIXED_1 (65536)
61 
62 # define JO_FIXED_120 (7864320)
63 
64 # define JO_FIXED_150 (9830400)
65 
67 # define JO_FLOAT_EPSILON (0.00001f)
68 
70 # define JO_PI (3.1415927)
71 
73 # define JO_PI_2 (1.5707963)
74 
79 # define JO_MULT_BY_2(X) ((X) << 1)
80 
84 # define JO_MULT_BY_4(X) ((X) << 2)
85 
89 # define JO_MULT_BY_8(X) ((X) << 3)
90 
94 # define JO_MULT_BY_16(X) ((X) << 4)
95 
99 # define JO_MULT_BY_32(X) ((X) << 5)
100 
105 # define JO_MULT_BY_256(X) ((X) << 8)
106 
111 # define JO_MULT_BY_1024(X) ((X) << 10)
112 
117 # define JO_MULT_BY_32768(X) ((X) << 15)
118 
123 # define JO_MULT_BY_65536(X) ((X) << 16)
124 
129 # define JO_DIV_BY_2(X) ((X) >> 1)
130 
134 # define JO_DIV_BY_4(X) ((X) >> 2)
135 
139 # define JO_DIV_BY_8(X) ((X) >> 3)
140 
144 # define JO_DIV_BY_16(X) ((X) >> 4)
145 
149 # define JO_DIV_BY_32(X) ((X) >> 5)
150 
154 # define JO_DIV_BY_1024(X) ((X) >> 10)
155 
160 # define JO_DIV_BY_32768(X) ((X) >> 15)
161 
166 # define JO_DIV_BY_65536(X) ((X) >> 16)
167 
171 # define JO_ABS(X) ((X) < 0 ? -(X) : (X))
172 
176 # define JO_FABS(X) ((X) < 0.0f ? -(X) : (X))
177 
182 # define JO_MIN(A, B) (((A) < (B)) ? (A) : (B))
183 
188 # define JO_MAX(A, B) (((A) > (B)) ? (A) : (B))
189 
193 # define JO_CHANGE_SIGN(X) (-(X))
194 
199 # define JO_FLOAT_NEARLY_EQUALS(A, B) ((A) <= ((B) + JO_FLOAT_EPSILON) && (A) >= ((B) - JO_FLOAT_EPSILON))
200 
204 # define JO_IS_FLOAT_NULL(A) ((A) <= JO_FLOAT_EPSILON && (A) >= (-JO_FLOAT_EPSILON))
205 
209 # define JO_RAD_TO_DEG(A) (180.0 * (A) / JO_PI)
210 
214 # define JO_DEG_TO_RAD(A) (JO_PI * (A) / 180.0)
215 
219 # define JO_IS_ODD(A) ((A) & 1)
220 
225 # define JO_SWAP(A, B) { (A) = (A) ^ (B); (B) = (A) ^ (B); (A) = (A) ^ (B); }
226 
231 # define JO_PERCENT_USED(TOTAL, FREE) (int)(100.0f / (float)(TOTAL) * (float)((TOTAL) - (FREE)))
232 
236 # define JO_SQUARE(A) ((A) * (A))
237 
243 static __jo_force_inline int jo_float2fixed(const float x)
244 {
245  jo_IEEE754 ieee754;
246 
247  ieee754.f = x;
248  if (!ieee754.field.exponent)
249  return 0;
250  if (ieee754.field.sign)
251  return -((ieee754.field.mantissa | 0x800000) >> (135 - ieee754.field.exponent));
252  return ((ieee754.field.mantissa | 0x800000) >> (135 - ieee754.field.exponent));
253 }
254 
260 {
261  return (JO_ABS(f) < 0.00000001f);
262 }
263 
275 static __jo_force_inline bool jo_square_intersect(const int x1, const int y1, const int w1, const int h1,
276  const int x2, const int y2, const int w2, const int h2)
277 {
278  return ((x1 < x2 + w2) && (x2 < x1 + w1)) && ((y1 < y2 + h2) && (y2 < y1 + h1));
279 }
280 
285 int jo_random(int max);
286 
292 static __jo_force_inline int jo_random_using_multiple(int max, int multiple)
293 {
294  return (jo_random(max) / multiple) * multiple;
295 }
296 
301 unsigned int jo_sqrt(unsigned int value);
302 
308 static __jo_force_inline float jo_sqrtf(float value)
309 {
310  unsigned int i = *(unsigned int*)(void *)&value;
311  i += 127 << 23;
312  i >>= 1;
313  return *(float*)(void *)&i;
314 }
315 
316 /*
317 ** COSINUS COMPUTATION
318 */
319 
324 static __jo_force_inline int jo_cos(const int angle)
325 {
326  return (JoCosLookupTable[JO_ABS(angle) % 360]);
327 }
328 
334 static __jo_force_inline float jo_cosf(const int angle)
335 {
336  return (jo_cos(angle) / 32768.0f);
337 }
338 
344 static __jo_force_inline int jo_cos_rad(const float angle)
345 {
346  return jo_cos((int)(JO_RAD_TO_DEG(angle)));
347 }
348 
354 static __jo_force_inline float jo_cos_radf(const float angle)
355 {
356  return jo_cos_rad(angle) / 32768.0f;
357 }
358 
364 static __jo_force_inline int jo_cos_mult(const int value, const int angle)
365 {
366  return JO_DIV_BY_32768(value * jo_cos(angle));
367 }
368 
374 static __jo_force_inline float jo_cosf_mult(const float value, const int angle)
375 {
376  return value * jo_cosf(angle);
377 }
378 
379 /*
380 ** SINUS COMPUTATION
381 */
382 
387 static __jo_force_inline int jo_sin(const int angle)
388 {
389 #ifdef JO_COMPILE_WITH_SINUS_TABLE
390  return (angle >= 0 ? JoSinLookupTable[JO_ABS(angle) % 360] : -JoSinLookupTable[JO_ABS(angle) % 360]);
391 #else
392  return (jo_cos(angle - 90));
393 #endif
394 }
395 
401 static __jo_force_inline float jo_sinf(const int angle)
402 {
403  return (jo_sin(angle) / 32768.0f);
404 }
405 
411 static __jo_force_inline int jo_sin_rad(const float angle)
412 {
413  return jo_sin((int)(JO_RAD_TO_DEG(angle)));
414 }
415 
421 static __jo_force_inline float jo_sin_radf(const float angle)
422 {
423  return (jo_sin_rad(angle) / 32768.0f);
424 }
425 
431 static __jo_force_inline int jo_sin_mult(const int value, const int angle)
432 {
433  return JO_DIV_BY_32768(value * jo_sin(angle));
434 }
435 
441 static __jo_force_inline float jo_sinf_mult(const float value, const int angle)
442 {
443  return value * jo_sinf(angle);
444 }
445 
446 /*
447 ** TAN
448 */
449 
454 static __jo_force_inline int jo_tan(const int angle)
455 {
456  return (jo_sin(angle) / jo_cos(angle));
457 }
458 
464 static __jo_force_inline float jo_tanf(const float angle)
465 {
466  return (jo_sinf(angle) / jo_cosf(angle));
467 }
468 
474 static __jo_force_inline int jo_tan_rad(const float angle)
475 {
476  return (jo_sin_rad(angle) / jo_cos_rad(angle));
477 }
478 
484 static __jo_force_inline float jo_tan_radf(const float angle)
485 {
486  return (jo_sin_radf(angle) / jo_cos_radf(angle));
487 }
488 
489 /*
490 ** ACOS
491 */
492 
497 static __jo_force_inline float jo_acos_radf(const float angle)
498 {
499  return (3.14159f - 1.57079f * angle);
500 }
501 
502 /*
503 ** ATAN2
504 */
505 
512 float jo_atan2f_rad(const float y, const float x);
513 
520 static __jo_force_inline int jo_atan2f(const float y, const float x)
521 {
522  return (int)JO_RAD_TO_DEG(jo_atan2f_rad(y, x));
523 }
524 
525 /*
526 ** 3D VECTOR
527 */
528 
535 static __jo_force_inline void jo_vectorf_add(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result)
536 {
537  result->x = a->x + b->x;
538  result->y = a->y + b->y;
539  result->z = a->z + b->z;
540 }
541 
548 static __jo_force_inline void jo_vectorf_adds(const jo_vectorf * const a, const float s, jo_vectorf * const result)
549 {
550  result->x = a->x + s;
551  result->y = a->y + s;
552  result->z = a->z + s;
553 }
554 
561 static __jo_force_inline void jo_vectorf_sub(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result)
562 {
563  result->x = a->x - b->x;
564  result->y = a->y - b->y;
565  result->z = a->z - b->z;
566 }
567 
574 static __jo_force_inline void jo_vectorf_subs(const jo_vectorf * const a, const float s, jo_vectorf * const result)
575 {
576  result->x = a->x - s;
577  result->y = a->y - s;
578  result->z = a->z - s;
579 }
580 
587 static __jo_force_inline void jo_vectorf_mul(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result)
588 {
589  result->x = a->x * b->x;
590  result->y = a->y * b->y;
591  result->z = a->z * b->z;
592 }
593 
600 static __jo_force_inline void jo_vectorf_muls(const jo_vectorf * const a, const float s, jo_vectorf * const result)
601 {
602  result->x = a->x * s;
603  result->y = a->y * s;
604  result->z = a->z * s;
605 }
606 
613 static __jo_force_inline void jo_vectorf_div(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result)
614 {
615  result->x = a->x / b->x;
616  result->y = a->y / b->y;
617  result->z = a->z / b->z;
618 }
619 
626 static __jo_force_inline void jo_vectorf_divs(const jo_vectorf * const a, const float s, jo_vectorf * const result)
627 {
628  result->x = a->x / s;
629  result->y = a->y / s;
630  result->z = a->z / s;
631 }
632 
637 static __jo_force_inline float jo_vectorf_length(const jo_vectorf * const a)
638 {
639  return (jo_sqrtf(JO_SQUARE(a->x) + JO_SQUARE(a->y) + JO_SQUARE(a->z)));
640 }
641 
647 static __jo_force_inline float jo_vectorf_dot(const jo_vectorf * const a, const jo_vectorf * const b)
648 {
649  return (a->x * b->x + a->y * b->y + a->z * b->z);
650 }
651 
657 static __jo_force_inline void jo_vectorf_normalize(const jo_vectorf * const a, jo_vectorf * const result)
658 {
659  float len = jo_vectorf_length(a);
660  if (len > 0)
661  {
662  result->x = a->x / len;
663  result->y = a->y / len;
664  result->z = a->z / len;
665  }
666  else
667  {
668  result->x = 0;
669  result->y = 0;
670  result->z = 0;
671  }
672 }
673 
679 static __jo_force_inline void jo_vectorf_proj(const jo_vectorf * const v, const jo_vectorf * const onto, jo_vectorf * const result)
680 {
681  jo_vectorf_muls(onto, jo_vectorf_dot(v, onto) / jo_vectorf_dot(onto, onto), result);
682 }
683 
690 static __jo_force_inline void jo_vectorf_cross(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result)
691 {
692  result->x = a->y * b->z - a->z * b->y;
693  result->y = a->z * b->x - a->x * b->z;
694  result->z = a->x * b->y - a->y * b->x;
695 }
696 
702 static __jo_force_inline float jo_vectorf_angle_between_radf(const jo_vectorf * const a, const jo_vectorf * const b)
703 {
705 }
706 
707 /*
708 ** 3D MATRIX 4x4
709 */
710 
715 {
716  register int i;
717 
718  for (i = 1; i < 15; ++i)
719  result->table[i] = 0;
720  result->m00 = 1;
721  result->m11 = 1;
722  result->m22 = 1;
723  result->m33 = 1;
724 }
725 
730 static __jo_force_inline void jo_matrixf_translation(const jo_vectorf * const offset, jo_matrixf * const result)
731 {
732  jo_matrixf_identity(result);
733  result->m30 = offset->x;
734  result->m31 = offset->y;
735  result->m32 = offset->z;
736 }
737 
742 static __jo_force_inline void jo_matrixf_scaling(const jo_vectorf * const scale, jo_matrixf * const result)
743 {
744  jo_matrixf_identity(result);
745  result->m00 = scale->x;
746  result->m11 = scale->y;
747  result->m22 = scale->z;
748 }
749 
754 static __jo_force_inline void jo_matrixf_rotation_x_rad(const float angle_in_rad, jo_matrixf * const result)
755 {
756  jo_matrixf_identity(result);
757  result->m11 = jo_cos_radf(angle_in_rad);
758  result->m12 = jo_sin_radf(angle_in_rad);
759  result->m21 = -result->m12;
760  result->m22 = result->m11;
761 }
762 
767 static __jo_force_inline void jo_matrixf_rotation_y_rad(const float angle_in_rad, jo_matrixf * const result)
768 {
769  jo_matrixf_identity(result);
770  result->m00 = jo_cos_radf(angle_in_rad);
771  result->m20 = jo_sin_radf(angle_in_rad);
772  result->m02 = -result->m20;
773  result->m22 = result->m00;
774 }
775 
780 static __jo_force_inline void jo_matrixf_rotation_z_rad(const float angle_in_rad, jo_matrixf * const result)
781 {
782  jo_matrixf_identity(result);
783  result->m00 = jo_cos_radf(angle_in_rad);
784  result->m01 = jo_sin_radf(angle_in_rad);
785  result->m10 = -result->m01;
786  result->m11 = result->m00;
787 }
788 
794 static __jo_force_inline void jo_matrixf_transpose(const jo_matrixf * const matrix, jo_matrixf * const result)
795 {
796  result->m00 = matrix->m00;
797  result->m10 = matrix->m01;
798  result->m20 = matrix->m02;
799  result->m30 = matrix->m03;
800  result->m01 = matrix->m10;
801  result->m11 = matrix->m11;
802  result->m21 = matrix->m12;
803  result->m31 = matrix->m13;
804  result->m02 = matrix->m20;
805  result->m12 = matrix->m21;
806  result->m22 = matrix->m22;
807  result->m32 = matrix->m23;
808  result->m03 = matrix->m30;
809  result->m13 = matrix->m31;
810  result->m23 = matrix->m32;
811  result->m33 = matrix->m33;
812 }
813 
820 static __jo_force_inline void jo_matrixf_mul(const jo_matrixf * const a, const jo_matrixf * const b, jo_matrixf * const result)
821 {
822  register int i;
823  register int j;
824  register int k;
825  register float sum;
826 
827  for (i = 0; i < 4; ++i)
828  {
829  for (j = 0; j < 4; ++j)
830  {
831  sum = 0;
832  for (k = 0; k < 4; ++k)
833  sum += a->m[k][j] * b->m[i][k];
834  result->m[i][j] = sum;
835  }
836  }
837 }
838 
844 static __jo_force_inline void jo_matrixf_rotation(const float angle_in_rad, const jo_vectorf * const axis, jo_matrixf * const result)
845 {
846  jo_vectorf normalized_axis;
847 
848  jo_vectorf_normalize(axis, &normalized_axis);
849  float c = jo_cos_radf(angle_in_rad);
850  float s = jo_sin_radf(angle_in_rad);
851  float a = 1 - c;
852  jo_matrixf_identity(result);
853  result->m00 = c + normalized_axis.x * normalized_axis.x * a;
854  result->m10 = normalized_axis.x * normalized_axis.y * a - normalized_axis.z * s;
855  result->m20 = normalized_axis.x * normalized_axis.z * a + normalized_axis.y * s;
856  result->m01 = normalized_axis.y * normalized_axis.x * a + normalized_axis.z * s;
857  result->m11 = c + normalized_axis.y * normalized_axis.y * a;
858  result->m21 = normalized_axis.y * normalized_axis.z * a - normalized_axis.x * s;
859  result->m02 = normalized_axis.z * normalized_axis.x * a - normalized_axis.y * s;
860  result->m12 = normalized_axis.z * normalized_axis.y * a + normalized_axis.x * s;
861  result->m22 = c + normalized_axis.z * normalized_axis.z * a;
862 }
863 
873 static __jo_force_inline void jo_matrixf_ortho(const float left, const float right, const float bottom, const float top,
874  const float back, const float front, jo_matrixf * const result)
875 {
876  jo_matrixf_identity(result);
877  result->m00 = 2 / (right - left);
878  result->m11 = 2 / (top - bottom);
879  result->m22 = 2 / (back - front);
880  result->m30 = -(right + left) / (right - left);
881  result->m31 = -(top + bottom) / (top - bottom);
882  result->m32 = -(back + front) / (back - front);
883 }
884 
893 static __jo_force_inline void jo_matrixf_perspective(const float vertical_field_of_view_in_deg, const float aspect_ratio,
894  const float near_view_distance, const float far_view_distance, jo_matrixf * const result)
895 {
896  jo_matrixf_identity(result);
897  result->m11 = 1.0f / jo_tan_radf((vertical_field_of_view_in_deg / 180 * JO_PI) / 2.0f);
898  result->m00 = result->m11 / aspect_ratio;
899  result->m22 = (far_view_distance + near_view_distance) / (near_view_distance - far_view_distance);
900  result->m32 = (2 * far_view_distance * near_view_distance) / (near_view_distance - far_view_distance);
901  result->m23 = -1;
902  result->m33 = 0;
903 }
904 
911 static __jo_force_inline void jo_matrixf_look_at(const jo_vectorf * const from, const jo_vectorf * const to,
912  const jo_vectorf * const up, jo_matrixf * const result)
913 {
914  jo_vectorf tmp;
915  jo_vectorf x;
916  jo_vectorf y;
917  jo_vectorf z;
918 
919  jo_vectorf_sub(to, from, &tmp);
920  jo_vectorf_normalize(&tmp, &tmp);
921  jo_vectorf_muls(&tmp, -1, &z);
922  jo_vectorf_cross(up, &z, &tmp);
923  jo_vectorf_normalize(&tmp, &x);
924  jo_vectorf_cross(&z, &x, &y);
925 
926  jo_matrixf_identity(result);
927  result->m00 = x.x;
928  result->m10 = x.y;
929  result->m20 = x.z;
930  result->m30 = -jo_vectorf_dot(from, &x);
931  result->m01 = y.x;
932  result->m11 = y.y;
933  result->m21 = y.z;
934  result->m31 = -jo_vectorf_dot(from, &y);
935  result->m02 = z.x;
936  result->m12 = z.y;
937  result->m22 = z.z;
938  result->m33 = -jo_vectorf_dot(from, &z);
939 }
940 
945 static __jo_force_inline void jo_matrixf_invert_affine(const jo_matrixf * const matrix, jo_matrixf * const result)
946 {
947  float c00 = matrix->m11 * matrix->m22 - matrix->m12 * matrix->m21;
948  float c10 = -(matrix->m01 * matrix->m22 - matrix->m02 * matrix->m21);
949  float c20 = matrix->m01 * matrix->m12 - matrix->m02 * matrix->m11;
950  float det = matrix->m00 * c00 + matrix->m10 * c10 + matrix->m20 * c20;
951  jo_matrixf_identity(result);
952  if (JO_FABS(det) < JO_FLOAT_EPSILON)
953  return;
954  float c01 = -(matrix->m10 * matrix->m22 - matrix->m12 * matrix->m20);
955  float c11 = matrix->m00 * matrix->m22 - matrix->m02 * matrix->m20;
956  float c21 = -(matrix->m00 * matrix->m12 - matrix->m02 * matrix->m10);
957  float c02 = matrix->m10 * matrix->m21 - matrix->m11 * matrix->m20;
958  float c12 = -(matrix->m00 * matrix->m21 - matrix->m01 * matrix->m20);
959  float c22 = matrix->m00 * matrix->m11 - matrix->m01 * matrix->m10;
960  float i00 = c00 / det;
961  float i10 = c01 / det;
962  float i20 = c02 / det;
963  float i01 = c10 / det;
964  float i11 = c11 / det;
965  float i21 = c12 / det;
966  float i02 = c20 / det;
967  float i12 = c21 / det;
968  float i22 = c22 / det;
969 
970  result->m00 = i00;
971  result->m10 = i10;
972  result->m20 = i20;
973  result->m30 = -(i00 * matrix->m30 + i10 * matrix->m31 + i20 * matrix->m32);
974  result->m01 = i01;
975  result->m11 = i11;
976  result->m21 = i21;
977  result->m31 = -(i01 * matrix->m30 + i11 * matrix->m31 + i21 * matrix->m32);
978  result->m02 = i02;
979  result->m12 = i12;
980  result->m22 = i22;
981  result->m32 = -(i02 * matrix->m30 + i12 * matrix->m31 + i22 * matrix->m32);
982 }
983 
989 static __jo_force_inline void jo_matrixf_mul_pos(const jo_matrixf * const matrix, const jo_vectorf * const position,
990  jo_vectorf * const result)
991 {
992  float w = matrix->m03 * position->x + matrix->m13 * position->y + matrix->m23 * position->z + matrix->m33;
993  result->x = matrix->m00 * position->x + matrix->m10 * position->y + matrix->m20 * position->z + matrix->m30;
994  result->y = matrix->m01 * position->x + matrix->m11 * position->y + matrix->m21 * position->z + matrix->m31;
995  result->z = matrix->m02 * position->x + matrix->m12 * position->y + matrix->m22 * position->z + matrix->m32;
996  if (w != 0 && w != 1)
997  {
998  result->x /= w;
999  result->y /= w;
1000  result->z /= w;
1001  }
1002 }
1003 
1009 static __jo_force_inline void jo_matrixf_mul_dir(const jo_matrixf * const matrix, const jo_vectorf * const direction,
1010  jo_vectorf * const result)
1011 {
1012  float w = matrix->m03 * direction->x + matrix->m13 * direction->y + matrix->m23 * direction->z;
1013  result->x = matrix->m00 * direction->x + matrix->m10 * direction->y + matrix->m20 * direction->z;
1014  result->y = matrix->m01 * direction->x + matrix->m11 * direction->y + matrix->m21 * direction->z;
1015  result->z = matrix->m02 * direction->x + matrix->m12 * direction->y + matrix->m22 * direction->z;
1016  if (w != 0 && w != 1)
1017  {
1018  result->x /= w;
1019  result->y /= w;
1020  result->z /= w;
1021  }
1022 }
1023 
1024 /*
1025 ** MISC
1026 */
1027 
1034 int jo_gcd(int a, int b);
1035 
1043 void jo_planar_rotate(const jo_pos2D * const point, const jo_pos2D * const origin, const int angle, jo_pos2D * const result);
1044 
1050 {
1051  switch (direction)
1052  {
1053  case LEFT:
1054  return (180);
1055  case UP:
1056  return (270);
1057  case DOWN:
1058  return (90);
1059  case UP_LEFT:
1060  return (225);
1061  case UP_RIGHT:
1062  return (315);
1063  case DOWN_LEFT:
1064  return (135);
1065  case DOWN_RIGHT:
1066  return (45);
1067  default:
1068  return (0);
1069  }
1070 }
1071 
1072 #endif /* !__JO_MATH_H__ */
1073 
1074 /*
1075 ** END OF FILE
1076 */
1077 
static __jo_force_inline float jo_cosf(const int angle)
Cosinus computation.
Definition: math.h:334
static __jo_force_inline void jo_vectorf_divs(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Divide value to vector (using floating numbers)
Definition: math.h:626
float x
Definition: types.h:80
static __jo_force_inline float jo_cosf_mult(const float value, const int angle)
Fast cosinus multiplication.
Definition: math.h:374
#define JO_DIV_BY_32768(X)
Devide a variable by 32768.
Definition: math.h:160
float table[16]
Definition: types.h:97
static __jo_force_inline void jo_matrixf_mul_dir(const jo_matrixf *const matrix, const jo_vectorf *const direction, jo_vectorf *const result)
Multiplies a 4x4 matrix with a 3D vector representing a direction in 3D space (using floating numbers...
Definition: math.h:1009
#define JO_RAD_TO_DEG(A)
Convert radians to degrees.
Definition: math.h:209
static __jo_force_inline void jo_vectorf_add(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Add 2 vectors (using floating numbers)
Definition: math.h:535
static __jo_force_inline void jo_matrixf_translation(const jo_vectorf *const offset, jo_matrixf *const result)
Creates translation matrix (using floating numbers)
Definition: math.h:730
4x4 MATRIX for 3D computation using floating numbers
Definition: types.h:94
Definition: types.h:189
int JoCosLookupTable[360]
Cosinus lookup table (internal engine usage) use jo_cos() instead.
void jo_planar_rotate(const jo_pos2D *const point, const jo_pos2D *const origin, const int angle, jo_pos2D *const result)
Rotate a point on the plan with a specific origin.
static __jo_force_inline int jo_tan_rad(const float angle)
Tangent computation.
Definition: math.h:474
static __jo_force_inline int jo_cos(const int angle)
Fast cosinus computation.
Definition: math.h:324
static __jo_force_inline float jo_vectorf_dot(const jo_vectorf *const a, const jo_vectorf *const b)
Get the dot product of 2 vectors (using floating numbers)
Definition: math.h:647
static __jo_force_inline int jo_float2fixed(const float x)
Convert float to jo engine fixed (avoid usage of GCC Soft Float)
Definition: math.h:243
static __jo_force_inline int jo_sin(const int angle)
Fast sinus computation.
Definition: math.h:387
Float IEEE 754 format.
Definition: types.h:157
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: conf.h:154
static __jo_force_inline void jo_matrixf_perspective(const float vertical_field_of_view_in_deg, const float aspect_ratio, const float near_view_distance, const float far_view_distance, jo_matrixf *const result)
Creates a perspective projection matrix for a camera (using floating numbers)
Definition: math.h:893
#define JO_SQUARE(A)
Square computation (x)
Definition: math.h:236
unsigned int jo_sqrt(unsigned int value)
Fast square root.
static __jo_force_inline int jo_atan2f(const float y, const float x)
Fast ATAN2 computation in degree.
Definition: math.h:520
static __jo_force_inline void jo_matrixf_rotation_y_rad(const float angle_in_rad, jo_matrixf *const result)
Creates rotating matrix (Y axis) (using floating numbers)
Definition: math.h:767
#define JO_PI
PI value.
Definition: math.h:70
int jo_random(int max)
Get a random number.
static __jo_force_inline float jo_vectorf_length(const jo_vectorf *const a)
Get the length of a vector (using floating numbers)
Definition: math.h:637
static __jo_force_inline int jo_sin_rad(const float angle)
Sinus computation.
Definition: math.h:411
Definition: types.h:185
static __jo_force_inline void jo_vectorf_muls(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Multiply value to vector (using floating numbers)
Definition: math.h:600
static __jo_force_inline void jo_matrixf_ortho(const float left, const float right, const float bottom, const float top, const float back, const float front, jo_matrixf *const result)
Creates an orthographic projection matrix (using floating numbers)
Definition: math.h:873
static __jo_force_inline float jo_vectorf_angle_between_radf(const jo_vectorf *const a, const jo_vectorf *const b)
Get the angle between 2 vectors (using floating numbers)
Definition: math.h:702
static __jo_force_inline void jo_matrixf_scaling(const jo_vectorf *const scale, jo_matrixf *const result)
Creates scaling matrix (using floating numbers)
Definition: math.h:742
static __jo_force_inline int jo_cos_rad(const float angle)
Cosinus computation.
Definition: math.h:344
int jo_gcd(int a, int b)
Get the greatest common divisor.
float z
Definition: types.h:82
Definition: types.h:183
static __jo_force_inline void jo_vectorf_cross(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Cross 2 vectors (using floating numbers)
Definition: math.h:690
static __jo_force_inline int jo_random_using_multiple(int max, int multiple)
Get a random number with a specific multiple.
Definition: math.h:292
#define JO_FABS(X)
Get the absolute value of X.
Definition: math.h:176
static __jo_force_inline void jo_matrixf_identity(jo_matrixf *const result)
Creates the identity matrix (using floating numbers)
Definition: math.h:714
static __jo_force_inline void jo_vectorf_subs(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Subtract value to vector (using floating numbers)
Definition: math.h:574
float f
Definition: types.h:159
static __jo_force_inline float jo_acos_radf(const float angle)
Fast Arc Cosinus computation.
Definition: math.h:497
2D position
Definition: types.h:42
static __jo_force_inline float jo_sinf(const int angle)
Sinus computation.
Definition: math.h:401
static __jo_force_inline int jo_tan(const int angle)
Fast tangent computation.
Definition: math.h:454
jo_8_directions
8 directions definition
Definition: types.h:181
static __jo_force_inline void jo_matrixf_rotation(const float angle_in_rad, const jo_vectorf *const axis, jo_matrixf *const result)
Creates a matrix to rotate around an axis by a given angle in radiant (using floating numbers) ...
Definition: math.h:844
Definition: types.h:188
static __jo_force_inline void jo_matrixf_mul(const jo_matrixf *const a, const jo_matrixf *const b, jo_matrixf *const result)
Multiply 2 matrix (using floating numbers)
Definition: math.h:820
static __jo_force_inline void jo_matrixf_rotation_x_rad(const float angle_in_rad, jo_matrixf *const result)
Creates rotating matrix (X axis) (using floating numbers)
Definition: math.h:754
static __jo_force_inline void jo_matrixf_mul_pos(const jo_matrixf *const matrix, const jo_vectorf *const position, jo_vectorf *const result)
Multiplies a 4x4 matrix with a 3D vector representing a point in 3D space (using floating numbers) ...
Definition: math.h:989
static __jo_force_inline void jo_matrixf_invert_affine(const jo_matrixf *const matrix, jo_matrixf *const result)
Inverts an affine transformation matrix (using floating numbers)
Definition: math.h:945
Definition: types.h:187
static __jo_force_inline int jo_cos_mult(const int value, const int angle)
Fast cosinus multiplication.
Definition: math.h:364
static __jo_force_inline float jo_sin_radf(const float angle)
Sinus computation.
Definition: math.h:421
static __jo_force_inline bool jo_is_float_equals_zero(float f)
Check if float almost equals 0;.
Definition: math.h:259
float jo_atan2f_rad(const float y, const float x)
Fast ATAN2 computation in radian.
static __jo_force_inline void jo_vectorf_normalize(const jo_vectorf *const a, jo_vectorf *const result)
Normalize a vector (using floating numbers)
Definition: math.h:657
static __jo_force_inline short jo_direction_to_angle(const jo_8_directions direction)
Convert jo_8_directions to angle in degree.
Definition: math.h:1049
static __jo_force_inline void jo_matrixf_rotation_z_rad(const float angle_in_rad, jo_matrixf *const result)
Creates rotating matrix (Z axis) (using floating numbers)
Definition: math.h:780
#define JO_FLOAT_EPSILON
Float minimum positive value.
Definition: math.h:67
Definition: types.h:186
static __jo_force_inline void jo_vectorf_proj(const jo_vectorf *const v, const jo_vectorf *const onto, jo_vectorf *const result)
Compute projection vector (using floating numbers)
Definition: math.h:679
static __jo_force_inline float jo_cos_radf(const float angle)
Cosinus computation.
Definition: math.h:354
static __jo_force_inline void jo_vectorf_mul(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Multiply 2 vectors (using floating numbers)
Definition: math.h:587
static __jo_force_inline void jo_matrixf_transpose(const jo_matrixf *const matrix, jo_matrixf *const result)
Creates transpose matrix (using floating numbers)
Definition: math.h:794
static __jo_force_inline float jo_sqrtf(float value)
Fast Square root using floating number.
Definition: math.h:308
static __jo_force_inline void jo_matrixf_look_at(const jo_vectorf *const from, const jo_vectorf *const to, const jo_vectorf *const up, jo_matrixf *const result)
Builds a transformation matrix for a camera (using floating numbers)
Definition: math.h:911
static __jo_force_inline float jo_tanf(const float angle)
Tangent computation.
Definition: math.h:464
float m[4][4]
Definition: types.h:96
static __jo_force_inline int jo_sin_mult(const int value, const int angle)
Fast sinus multiplication.
Definition: math.h:431
static __jo_force_inline void jo_vectorf_adds(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Add value to vector (using floating numbers)
Definition: math.h:548
static __jo_force_inline void jo_vectorf_div(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Divide 2 vectors (using floating numbers)
Definition: math.h:613
Definition: types.h:190
#define JO_ABS(X)
Get the absolute value of X.
Definition: math.h:171
static __jo_force_inline float jo_sinf_mult(const float value, const int angle)
Fast sinus multiplication.
Definition: math.h:441
struct jo_IEEE754::@18 field
static __jo_force_inline void jo_vectorf_sub(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Subtract 2 vectors (using floating numbers)
Definition: math.h:561
Vector for 3D computation using floating numbers.
Definition: types.h:78
static __jo_force_inline float jo_tan_radf(const float angle)
Tangent computation.
Definition: math.h:484
static __jo_force_inline bool jo_square_intersect(const int x1, const int y1, const int w1, const int h1, const int x2, const int y2, const int w2, const int h2)
Fast method to get if two square intersects (HitBox processing)
Definition: math.h:275
float y
Definition: types.h:81