forked from cyxx/f2bgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathfuncs.cpp
94 lines (86 loc) · 2.04 KB
/
mathfuncs.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Fade To Black engine rewrite
* Copyright (C) 2006-2012 Gregory Montoir ([email protected])
*/
#include <math.h>
#include "mathfuncs.h"
int32_t g_cos[1024], g_sin[1024];
int32_t g_atan[256];
void Vec_xz::rotate(int angle, int shift, int dist) {
angle &= 1023;
const int cosa = g_cos[angle] * dist;
const int sina = g_sin[angle] * dist;
const int xr = fixedMul(cosa, x, shift) + fixedMul(sina, z, shift);
const int zr = fixedMul(cosa, z, shift) - fixedMul(sina, x, shift);
x = xr;
z = zr;
}
int getAngleDiff(int angle1, int angle2) {
if (angle1 != angle2) {
int mul = 1;
if (angle1 < angle2) {
SWAP(angle1, angle2);
mul = -1;
}
int ret = angle1 - angle2;
if (ret > 512) {
ret = 1024 - ret;
mul = -mul;
}
return mul * ret;
}
return 0;
}
int fixedSqrt(int x, int shift) {
assert(x >= 0);
return (int)(sqrt(x) + .5);
}
int getSquareDistance(int x1, int z1, int x2, int z2, int shift) {
const int dx = (x2 >> shift) - (x1 >> shift);
const int dz = (z2 >> shift) - (z1 >> shift);
return dx * dx + dz * dz;
}
int getAngleFromPos(int X, int Z) {
static const int maxRy = 256;
int A;
if (X == 0) {
if (Z > 0) {
A = 0;
} else {
A = 512;
}
} else if (X > 0) {
if (Z > X) {
A = 1 + g_atan[fixedMul(X, maxRy << 15, 15) / Z];
} else if (Z == X) {
A = 128;
} else if (Z > 0) {
A = 256 - 1 - g_atan[fixedMul(Z, maxRy << 15, 15) / X];
} else if (Z == 0) {
A = 256;
} else if (Z > -X) {
A = 256 + 1 + g_atan[-fixedMul(Z, maxRy << 15, 15) / X];
} else if (Z == -X) {
A = 384;
} else {
A = 512 - 1 - g_atan[-fixedMul(X, maxRy << 15, 15) / Z];
}
} else {
if (Z < X) {
A = 512 + 1 + g_atan[fixedMul(X, maxRy << 15, 15) / Z];
} else if (Z == X) {
A = 640;
} else if (Z < 0) {
A = 768 - 1 - g_atan[fixedMul(Z, maxRy << 15, 15) / X];
} else if (Z == 0) {
A = 768;
} else if (Z < -X) {
A = 768 + 1 + g_atan[-fixedMul(Z, maxRy << 15, 15) / X];
} else if (Z == -X) {
A = 896;
} else {
A = 1024 - 1 - g_atan[-fixedMul(X, maxRy << 15, 15) / Z];
}
}
return A;
}