-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asteroid.cpp
336 lines (262 loc) · 6.72 KB
/
Asteroid.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "StdAfx.h"
#include "Asteroid.h"
#include "LuaUtilities.h"
#include "RLRenderer.h"
using RustyLib::Graphics::Renderer;
//Define some vars
Vector3 Asteroid::colour;
float Asteroid::MIN_VELOCITY;
float Asteroid::MAX_VELOCITY;
float Asteroid::MIN_ROT_VELOCITY;
float Asteroid::MAX_ROT_VELOCITY;
float Asteroid::MIN_SIZE;
float Asteroid::MAX_SIZE;
Asteroid::Asteroid(void)
: GameObject()
{
}
Asteroid::~Asteroid(void)
{
}
Error Asteroid::ClassInitialise(void )
{
//Read out my config details
LuaUtils::LoadFile("config/asteroid_config.ini");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
colour = LuaUtils::GetVector3("colour");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
MIN_VELOCITY = LuaUtils::GetFloat("MIN_VELOCITY");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
MAX_VELOCITY = LuaUtils::GetFloat("MAX_VELOCITY");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
MIN_ROT_VELOCITY = LuaUtils::GetFloat("MIN_ROT_VELOCITY");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
MAX_ROT_VELOCITY = LuaUtils::GetFloat("MAX_ROT_VELOCITY");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
MIN_SIZE = LuaUtils::GetFloat("MIN_SIZE");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
MAX_SIZE = LuaUtils::GetFloat("MAX_SIZE");
if(LuaUtils::GetErrorCode()){return Error::RL_ERROR;}
//Clear up LuaStack
LuaUtils::ClearLuaStack();
return Error::RL_NO_ERROR;
}
Error Asteroid::Initialise(void )
{
size = MAX_SIZE;
if(HullGenerate())
{
return Error::RL_ERROR;
}
//make the bsphere from the hull
b_sphere.CreateFrom(m_hull, num_verts);
SetVelocities();
return Error::RL_NO_ERROR;
}
Error Asteroid::Initialise(float size)
{
if(size > MAX_SIZE)
{
this->size = MAX_SIZE;
}
else if(size < MIN_SIZE)
{
size = MIN_SIZE;
}
else
{
this->size = size;
}
if(HullGenerate())
{
return Error::RL_ERROR;
}
//Make the bsphere from the hull
b_sphere.CreateFrom(m_hull, num_verts);
SetVelocities();
return Error::RL_NO_ERROR;
}
void Asteroid::SetVelocities(void )
{
srand(timeGetTime());
//Set up a velocity
float angle = rand() % 360;
float velocity_scalar = (rand() % (int)MAX_VELOCITY) + MIN_VELOCITY;
velocity_scalar /= 100;
using RustyLib::Matrix44;
Matrix44 *rot = Matrix44::CreateRotationZ(angle);
velocity = *rot * Vector3::INV_Y_AXIS();
velocity *= velocity_scalar;
delete rot;
//Set the rotational velocity
rot_velocity = (rand() % (int)MAX_ROT_VELOCITY) + MIN_ROT_VELOCITY;
rot_velocity /= 1000;//we can only use integers with rand because
//the values required are so low they sometimes truncate to zero
//which throws with %, so we divide by 10 after to get that low value
}
Error Asteroid::Exit(void )
{
return Error::RL_NO_ERROR;
}
void Asteroid::Update(const float time_step)
{
rotation += rot_velocity * time_step;
GameObject::Update(time_step);
b_sphere.SetPosition(position);
}
void Asteroid::Render(void )
{
Renderer *r = &Renderer::Instance();
r->SetLineColour(colour);
using RustyLib::Matrix44;
Matrix44 *rot = Matrix44::CreateRotationZ(rotation);
//This should in theory draw triangles from asteroid center
//out to its hull points
int i = 0;
for(; i < num_verts-1; i++)
{
Vector3 *v1 = *rot*m_hull[i];
Vector3 *v2 = *rot*m_hull[i+1];
*v1 += position;
*v2 += position;
r->RenderTri(position,*v1,*v2);
delete v1,v2;
}
Vector3 *v1 = *rot*m_hull[i];
Vector3 *v2 = *rot*m_hull[0];
*v1 += position;
*v2 += position;
r->RenderTri(position,*v1,*v2);
//Draw the b_sphere
//r->RenderCircle(b_sphere.GetPosition(),b_sphere.GetRadius());
delete rot,v1,v2;
}
Error Asteroid::HullGenerate(void )
{
//The number of verts will be based on
//the size of the asteroid
Vector3 *verts;
num_verts = (int)size * 2;
verts = new Vector3[num_verts];
srand(timeGetTime());
for(int i=0; i<num_verts; i++)
{
verts[i] = Vector3(
(rand() % (int)size*2 - size),
(rand() % (int)size*2) - size,
0.0f);
}
//GRAHAM SCAN
//Sort through the verts that have been randomised and
//make a convex hull by sorting them in to order
//Step 1: Find the bottom right point
Vector3 *scan_start_point = &verts[0];
for(int i=1; i<num_verts; i++)
{
//If it is lower in space
if(verts[i].y > scan_start_point->y)
{
//Swap
scan_start_point = &verts[i];
}
//Otherwise if they are the same
else if(verts[i].y == scan_start_point->y)
{
//Check which is right most
if(verts[i].x > scan_start_point->x)
{
//Swap
scan_start_point = &verts[i];
}
}
}
//Now we get the angles relative to the start point
//and set them up in a pair
struct graham_pair
{
Vector3 *p;
float angle;
};
graham_pair *sorted = new graham_pair[num_verts];
for(int i=0; i<num_verts; i++)
{
sorted[i].p = &verts[i];
Vector3 w = *sorted[i].p - *scan_start_point;
w.Normalise();
sorted[i].angle = w.DotProduct(Vector3::X_AXIS());
}
//Then we sort them in order of the angle they are associated with
graham_pair temp,temp2;
for(int i = 1; i< num_verts; i++)
{
for(int j = 1; j < num_verts-1; j++)
{
if(sorted[j+1].angle < sorted[j].angle)
{
temp = sorted[j];
temp2 = sorted[j+1];
sorted[j] = temp2;
sorted[j+1] = temp;
}
}
}
std::vector<Vector3*> *hull = new std::vector<Vector3*>();
//We can push the starter point
hull->push_back(sorted[0].p);
//Push the next point as it is certain to be part of the hull
hull->push_back(sorted[1].p);
int currHullPointCount = hull->size();
while (currHullPointCount < num_verts)
{
Vector3* p1 = (*hull)[hull->size()-2];
Vector3* p2 = (*hull)[hull->size()-1];
//If we have to turn left to touch it
//then it is next in the hull
if( IsLeftTurn(*p1, *p2, *sorted[currHullPointCount].p) )
{
//Place it on the *ahem* 'stack'
hull->push_back(sorted[currHullPointCount].p);
}
//if it is not a left turn then it means the previous
//verts is not in the convex hull
else
{
if(hull->size() > 2)
{
hull->pop_back();
}
}
currHullPointCount++;
}
//Push the start point to connect the polygon
hull->push_back(sorted[0].p);
//Now we must read that hull in to our verts
m_hull = new Vector3[hull->size()];
int i=0;
std::vector<Vector3*>::iterator iter = hull->begin();
while(iter != hull->end())
{
m_hull[i] = *iter._Ptr;
i++;
iter++;
}
//Set the number of verts in the hull to the final count
num_verts = hull->size();
hull->clear();
delete hull;
delete sorted;
delete [] verts;
return Error::RL_NO_ERROR;
}
bool Asteroid::IsLeftTurn(const Vector3& p1, const Vector3& p2, const Vector3& p3)
{
bool isLeftTurn = false;
float area2 = ( p2.x - p1.x ) * ( p3.y - p1.y ) -
( p3.x - p1.x ) * ( p2.y - p1.y );
isLeftTurn = ( area2 > 0.0 );
return isLeftTurn;
}
BSphere& Asteroid::GetBSphere(void )
{
return b_sphere;
}