-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_functions.c
72 lines (59 loc) · 1.57 KB
/
math_functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "math_functions.h"
void linalg_getFaceNormal(float *norm, float * pointa, float * pointb,
float * pointc)
{
float vect[2][3];
int a,b;
float point[3][3];
// copies points into point[][]
for (a=0;a<3;++a)
{
point[0][a]=pointa[a];
point[1][a]=pointb[a];
point[2][a]=pointc[a];
}
// Calculate vectors from point[0] to point[1]
for (a=0;a<2;++a)
{
//Calculate vectors from point[0] to point[2]
for (b=0;b<3;++b)
{
vect[a][b]=point[2-a][b]-point[0][b];
}
}
// Calculates vector at 90 deg to 2 vectors
linalg_crossProduct(norm,vect[0],vect[1]);
linalg_normalize(norm);
}
void linalg_normalize(float * vect)
{
float length;
int a;
length = sqrt(pow(vect[0],2) + pow(vect[1],2) + pow(vect[2],2));
for (a=0;a<3;++a)
{
vect[a]/=length;
}
}
void linalg_crossProduct(float *c,float a[3], float b[3])
{
c[0]=a[1]*b[2] - b[1]*a[2];
c[1]=a[2]*b[0] - b[2]*a[0];
c[2]=a[0]*b[1] - b[0]*a[1];
}
float linalg_calcPitch(PhysicsVector * from, PhysicsVector * to)
{
return atan2f(to->x - from->x, to->z - from->z) + PI;
}
float linalg_calcYaw(PhysicsVector * from, PhysicsVector * to)
{
return atan2f(to->x - from->x, to->y - from->y);
}
float linalg_calcPitchDeg(PhysicsVector * from, PhysicsVector * to)
{
return linalg_calcPitch(from, to) / PI * 180;
}
float linalg_calcYawDeg(PhysicsVector * from, PhysicsVector * to)
{
return linalg_calcYaw(from, to) / PI * 180;
}