-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarget.c
More file actions
137 lines (105 loc) · 2.28 KB
/
Target.c
File metadata and controls
137 lines (105 loc) · 2.28 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
#include "Target.h"
struct Targets
{
Vector3D* pos;
int* destroyed;
int numTargets;
};
Targets* createTargets(void)
{
Targets* targets = malloc(sizeof(Targets));
targets->numTargets = 512;
targets->pos = malloc(targets->numTargets * sizeof(Vector3D));
targets->destroyed = malloc(targets->numTargets * sizeof(int));
srand(getCurrentTime());
int i;
for(i=0; i<targets->numTargets; i++)
{
Vector3D pos;
pos.x = rand()%2000 - 1000.0;
pos.y = rand()%2000 - 1000.0;
if(rand()%2 == 0)
{
pos.z = rand()%2000;
}
else
{
pos.z = 1.0;
}
targets->pos[i] = pos;
targets->destroyed[i] = 0;
}
return targets;
}
static void drawTarget(void)
{
glPushMatrix();
glBegin(GL_QUADS);
glVertex3f(-1.0,-1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, 0.0);
glVertex3f( 1.0,-1.0, 0.0);
glVertex3f(-1.0, 0.0,-1.0);
glVertex3f(-1.0, 0.0, 1.0);
glVertex3f( 1.0, 0.0, 1.0);
glVertex3f( 1.0, 0.0,-1.0);
glVertex3f( 0.0,-1.0,-1.0);
glVertex3f( 0.0,-1.0, 1.0);
glVertex3f( 0.0, 1.0, 1.0);
glVertex3f( 0.0, 1.0,-1.0);
glEnd();
glPopMatrix();
}
void destroyTargets(Targets* targets, Bullet* bullets, int numBullets)
{
int i;
for(i=0; i<targets->numTargets; i++)
{
if(targets->destroyed[i] == 0)
{
Vector3D posTarget = targets->pos[i];
int j;
for(j=0; j<numBullets; j++)
{
Vector3D posBullet = bullets[j].position;
Vector3D velBullet = bullets[j].velocity;
int n = 4;
int k;
for(k=0; k<n; k++)
{
double distx = posTarget.x - posBullet.x + k*velBullet.x/(double)n;
double disty = posTarget.y - posBullet.y + k*velBullet.y/(double)n;
double distz = posTarget.z - posBullet.z + k*velBullet.z/(double)n;
double dist = sqrt(distx*distx + disty*disty + distz*distz);
if(dist < 1.0)
{
targets->destroyed[i] = 1;
}
}
}
}
}
}
void drawTargets(Targets* targets)
{
int i;
for(i=0; i<targets->numTargets; i++)
{
if(targets->destroyed[i] == 0)
{
glPushMatrix();
Vector3D pos = targets->pos[i];
glTranslatef(pos.x, pos.y, pos.z);
if(pos.z < 2.0)
{
glColor3f(0.1, 0.1, 0.7);
}
else
{
glColor3f(1.0, 1.0, 0.8);
}
drawTarget();
glPopMatrix();
}
}
}