-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefFunction.h
More file actions
141 lines (117 loc) · 3.7 KB
/
DefFunction.h
File metadata and controls
141 lines (117 loc) · 3.7 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#define _USE_MATH_DEFINES // to use cmath headfile
#include <cmath>
#define FPS 30.0f // frame per sec
#define EPSILON 0.0001f // mis-accuraccy
#define DISTANCE_P 750.0f // default dis between (projection of cam on xy plane) & act
#define DEFAULT_HEIGHT 105.0f // default cam height
#define DEFAULT_DEGREE 4.7f // default cam look down degree
#define MAX_HEIGHT 250.0f
#define ACT_DEGREEperFRAME 0.25f // act turned degree per frame
#define CAMUPDEGREE 1.0f // cam raise angle if hit terrain
#define POS_APPROACH_MIN 0.5
#define MAX_APPROACH 50
// used in function set_act_dir
#define UP 0
#define DOWN 1
#define RIGHT 2
#define LEFT 3
// used in function mix_dir
#define UP_RIGHT 0
#define UP_LEFT 1
#define DOWN_RIGHT 2
#define DOWN_LEFT 3
#define DEFAULT 1
#define ELSE 0
float dis = sqrt(DISTANCE_P*DISTANCE_P + DEFAULT_HEIGHT*DEFAULT_HEIGHT);
float height = DEFAULT_HEIGHT;
float dis_p = DISTANCE_P;
float THETA = ACT_DEGREEperFRAME;
float PHI = CAMUPDEGREE;
float cam_angle = DEFAULT_DEGREE;
float cam_height = DEFAULT_HEIGHT;
float degree2rad ( float degree )
{
return float( degree * 2 * M_PI / 360 );
}
float distance ( float *a, float *b )
{
float x = a[0] - b[0],
y = a[1] - b[1],
z = a[2] - b[2];
return sqrt ( x * x + y * y + z * z );
}
void set_act_dir ( float *cam_fDir, float *act_fDir, int direction = UP )
{
float d = sqrt ( 1 - cam_fDir[2] * cam_fDir[2] );
act_fDir[0] = cam_fDir[0] / d;
act_fDir[1] = cam_fDir[1] / d;
act_fDir[2] = 0;
if ( direction == DOWN )
act_fDir[0] *= -1, act_fDir[1] *= -1;
else if ( direction == RIGHT ) {
float f = act_fDir[0];
act_fDir[0] = act_fDir[1];
act_fDir[1] = -f;
}
else if ( direction == LEFT ) {
float f = act_fDir[0];
act_fDir[0] = -act_fDir[1];
act_fDir[1] = f;
}
}
void set_cam_dir ( float *fDir, float *uDir, int control = ELSE )
{
if ( control == DEFAULT ) {
float d = sqrt ( fDir[0] * fDir[0] + fDir[1] * fDir[1] );
float Dir[3] = { fDir[0] / d, fDir[1] / d, 0 };
fDir[2] = -sin ( degree2rad ( cam_angle ) );
uDir[2] = cos ( degree2rad ( cam_angle ) );
float fc = sqrt ( 1 - fDir[2] * fDir[2] ),
uc = sqrt ( 1 - uDir[2] * uDir[2] );
uDir[0] = Dir[0] * uc;
uDir[1] = Dir[1] * uc;
fDir[0] = Dir[0] * fc;
fDir[1] = Dir[1] * fc;
}
else {
float d = sqrt ( fDir[0] * fDir[0] + fDir[1] * fDir[1] + fDir[2] * fDir[2] );
fDir[0] /= d;
fDir[1] /= d;
fDir[2] /= d;
d = sqrt ( fDir[2]*fDir[2] / (fDir[0]*fDir[0] + fDir[1]*fDir[1]) );
uDir[0] = fDir[0] * d;
uDir[1] = fDir[1] * d;
uDir[2] = sqrt ( fDir[0]*fDir[0] + fDir[1]*fDir[1] );
}
}
void set_cam_pos ( float *cam_pos, float *act_pos, float *cam_fDir )
{
float d = sqrt ( 1 - cam_fDir[2] * cam_fDir[2] );
cam_pos[0] = act_pos[0] - dis_p * cam_fDir[0] / d;
cam_pos[1] = act_pos[1] - dis_p * cam_fDir[1] / d;
cam_pos[2] = height;
}
void mix_dir ( float *cam_fDir, float *act_fDir, int dir )
{
float Dir1[3], Dir2[3];
if ( dir == UP_RIGHT ) {
Dir1[0] = cam_fDir[0], Dir1[1] = cam_fDir[1];
Dir2[0] = cam_fDir[1], Dir2[1] = -cam_fDir[0];
}
else if ( dir == UP_LEFT ) {
Dir1[0] = cam_fDir[0], Dir1[1] = cam_fDir[1];
Dir2[0] = -cam_fDir[1], Dir2[1] = cam_fDir[0];
}
else if ( dir == DOWN_RIGHT ) {
Dir1[0] = -cam_fDir[0], Dir1[1] = -cam_fDir[1];
Dir2[0] = cam_fDir[1], Dir2[1] = -cam_fDir[0];
}
else {
Dir1[0] = -cam_fDir[0], Dir1[1] = -cam_fDir[1];
Dir2[0] = -cam_fDir[1], Dir2[1] = cam_fDir[0];
}
const float d = float( sqrt ( 0.5 ) );
act_fDir[0] = ( Dir1[0] + Dir2[0] ) * d;
act_fDir[1] = ( Dir1[1] + Dir2[1] ) * d;
act_fDir[2] = 0;
}