-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.c
178 lines (133 loc) · 5.21 KB
/
projectile.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Created by tim on 11/5/20.
//
#include "projectile.h"
float effabs(float x){
return sqrtf(x * x);
}
float effacos(float x) {
float negate = (float)(x < 0);
x = effabs(x);
float ret = -0.0187293;
ret = ret * x;
ret = ret + 0.0742610;
ret = ret * x;
ret = ret - 0.2121144;
ret = ret * x;
ret = ret + 1.5707288;
ret = ret * sqrtf(1.0-x);
ret = ret - 2 * negate * ret;
return negate * 3.14159265358979 + ret;
}
Projectile instantiate_projectile(Vec3d playerPos, Vec3d playerForward, float distance, float radius) {
long identity[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
float playerMagnitude = sqrtf(playerPos.x * playerPos.x + playerPos.y * playerPos.y + playerPos.z * playerPos.z);
Vec3d playerPosNorm = {
playerPos.x / playerMagnitude,
playerPos.y / playerMagnitude,
playerPos.z / playerMagnitude
};
Vec3d startPos = {
playerPos.x + playerPosNorm.x * distance,
playerPos.y + playerPosNorm.y * distance,
playerPos.z + playerPosNorm.z * distance
};
Projectile projOut;
projOut.forward = playerForward;
projOut.look_forward = projOut.forward;
projOut.pos = startPos;
projOut.radius = radius;
projOut.initial_pos = projOut.pos;
projOut.isEliminated = 0;
projOut.speed = PROJECTILE_SPEED;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
projOut.transform.m[i][j] = identity[i][j];
projOut.translation.m[i][j] = identity[i][j];
}
}
return projOut;
}
void initialize_projectile_array(Projectile * array){
for(int i = 0; i < MAX_PROJECTILES; i++){
array[i].isEliminated = 1;
}
}
void fire_projectile(Vec3d playerPos, Vec3d playerForward, Projectile * array, int * projectile_count){
if(*projectile_count < MAX_PROJECTILES){
*projectile_count = *projectile_count + 1;
int i = 0;
while(!array[i].isEliminated){
i++;
}
array[i] = instantiate_projectile(playerPos, playerForward, DEFAULT_DISTANCE, DEFAULT_RADIUS);
}
}
void cull_projectiles(Projectile * array, int * projectile_count){
for(int i = 0; i < MAX_PROJECTILES; i++){
if(!array[i].isEliminated){
Vec3d pos = array[i].pos;
Vec3d init_pos = array[i].initial_pos;
*projectile_count = *projectile_count - 1;
Vec3d diff = {
pos.x - init_pos.x,
pos.y - init_pos.y,
pos.z - init_pos.z,
};
float distance_travelled = sqrtf(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z);
if(distance_travelled > MAX_TRAVEL_DISTANCE){
array[i].isEliminated = 1;
}
}
}
}
void rotate_proj_to_player(Projectile * projectile, Vec3d playerForward){
Vec3d axis;
float angle;
Mtx rotation_matrix;
Vec3d projectileForward = projectile->look_forward;
Vec3d crossProduct ={
projectileForward.y * playerForward.z - projectileForward.z * playerForward.y,
projectileForward.z * playerForward.x - projectileForward.x * playerForward.z,
projectileForward.x * playerForward.y - projectileForward.y * playerForward.x
};
float crossMag = sqrtf(crossProduct.x * crossProduct.x + crossProduct.y * crossProduct.y +
crossProduct.z * crossProduct.z);
float projMag = sqrtf(projectileForward.x * projectileForward.x + projectileForward.y * projectileForward.y
+ projectileForward.z * projectileForward.z);
float playerMag = sqrtf(playerForward.x * playerForward.x + playerForward.y * playerForward.y
+ playerForward.z * playerForward.z);
float dotProd = projectileForward.x * playerForward.x + projectileForward.y * playerForward.y +
projectileForward.z * playerForward.z;
if(projMag != 0 && crossMag != 0){
axis.x = crossProduct.x / crossMag;
axis.y = crossProduct.y / crossMag;
axis.z = crossProduct.z / crossMag;
angle = effacos(dotProd / (projMag * playerMag));
guRotate(&(projectile->transform), angle, axis.x, axis.y, axis.z);
}
}
void move_projectiles(Projectile * projectiles){
for(int i = 0; i < MAX_PROJECTILES; i++){
if(!projectiles[i].isEliminated) {
projectiles[i].pos.x += projectiles[i].forward.x * projectiles[i].speed * .066;
projectiles[i].pos.y += projectiles[i].forward.y * projectiles[i].speed * .066;
projectiles[i].pos.z += projectiles[i].forward.z * projectiles[i].speed * .066;
guTranslate(&(projectiles[i].translation), projectiles[i].pos.x, projectiles[i].pos.y, projectiles[i].pos.z);
}
}
}
void handle_projectiles(Projectile * projectiles, Vec3d playerForward, Vec3d playerPos, int * projectile_count){
cull_projectiles(projectiles, projectile_count);
/*for(int i = 0; i < MAX_PROJECTILES; i++){
if(!projectiles[i].isEliminated){
//rotate_proj_to_player(&projectiles[i], playerForward);
}
}*/
move_projectiles(projectiles);
}