-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.cpp
474 lines (430 loc) · 19.2 KB
/
entity.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// entity.cpp v1.3
#include "entity.h"
//=============================================================================
// constructor
//=============================================================================
Entity::Entity() : Image()
{
radius = 1.0;
edge.left = -1;
edge.top = -1;
edge.right = 1;
edge.bottom = 1;
mass = 1.0;
velocity.x = 0.0;
velocity.y = 0.0;
deltaV.x = 0.0;
deltaV.y = 0.0;
active = true; // the entity is active
rotatedBoxReady = false;
collisionType = entityNS::CIRCLE;
health = 100;
gravity = entityNS::GRAVITY;
}
//=============================================================================
// Initialize the Entity.
// Pre: *gamePtr = pointer to Game object
// width = width of Image in pixels (0 = use full texture width)
// height = height of Image in pixels (0 = use full texture height)
// ncols = number of columns in texture (1 to n) (0 same as 1)
// *textureM = pointer to TextureManager object
// Post: returns true if successful, false if failed
//=============================================================================
bool Entity::initialize(Game *gamePtr, int width, int height, int ncols,
TextureManager *textureM)
{
input = gamePtr->getInput(); // the input system
audio = gamePtr->getAudio(); // the audio system
return(Image::initialize(gamePtr->getGraphics(), width, height, ncols, textureM));
}
//=============================================================================
// activate the entity
//=============================================================================
void Entity::activate()
{
active = true;
}
//=============================================================================
// update
// typically called once per frame
// frameTime is used to regulate the speed of movement and animation
//=============================================================================
void Entity::update(float frameTime)
{
velocity += deltaV;
deltaV.x = 0;
deltaV.y = 0;
Image::update(frameTime);
rotatedBoxReady = false; // for rotatedBox collision detection
}
//=============================================================================
// ai (artificial intelligence)
// typically called once per frame
// performs ai calculations, ent is passed for interaction
//=============================================================================
void Entity::ai(float frameTime, Entity &ent)
{}
//=============================================================================
// Perform collision detection between this entity and the other Entity.
// Each entity must use a single collision type. Complex shapes that require
// multiple collision types may be done by treating each part as a separate
// entity.
// Typically called once per frame.
// The collision types: CIRCLE, BOX, or ROTATED_BOX.
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
//=============================================================================
bool Entity::collidesWith(Entity &ent, VECTOR2 &collisionVector)
{
// if either entity is not active then no collision may occcur
if (!active || !ent.getActive())
return false;
// If both entities are CIRCLE collision
if (collisionType == entityNS::CIRCLE && ent.getCollisionType() == entityNS::CIRCLE)
return collideCircle(ent, collisionVector);
// If both entities are BOX collision
if (collisionType == entityNS::BOX && ent.getCollisionType() == entityNS::BOX)
return collideBox(ent, collisionVector);
// All other combinations use separating axis test
// If neither entity uses CIRCLE collision
if (collisionType != entityNS::CIRCLE && ent.getCollisionType() != entityNS::CIRCLE)
return collideRotatedBox(ent, collisionVector);
else // one of the entities is a circle
if (collisionType == entityNS::CIRCLE) // if this entity uses CIRCLE collision
{
// Check for collision from other box with our circle
bool collide = ent.collideRotatedBoxCircle(*this, collisionVector);
// Put the collision vector in the proper direction
collisionVector *= -1; // reverse collision vector
return collide;
}
else // the other entity uses CIRCLE collision
return collideRotatedBoxCircle(ent, collisionVector);
return false;
}
//=============================================================================
// Circular collision detection method
// Called by collision(), default collision detection method
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
//=============================================================================
bool Entity::collideCircle(Entity &ent, VECTOR2 &collisionVector)
{
// difference between centers
distSquared = *getCenter() - *ent.getCenter();
distSquared.x = distSquared.x * distSquared.x; // difference squared
distSquared.y = distSquared.y * distSquared.y;
// Calculate the sum of the radii (adjusted for scale)
sumRadiiSquared = (radius*getScale()) + (ent.radius*ent.getScale());
sumRadiiSquared *= sumRadiiSquared; // square it
// if entities are colliding
if(distSquared.x + distSquared.y <= sumRadiiSquared)
{
// set collision vector
collisionVector = *ent.getCenter() - *getCenter();
return true;
}
return false; // not colliding
}
//=============================================================================
// Axis aligned bounding box collision detection method
// Called by collision()
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
//=============================================================================
bool Entity::collideBox(Entity &ent, VECTOR2 &collisionVector)
{
// if either entity is not active then no collision may occcur
if (!active || !ent.getActive())
return false;
// Check for collision using Axis Aligned Bounding Box.
if( (getCenterX() + edge.right*getScale() >= ent.getCenterX() + ent.getEdge().left*ent.getScale()) &&
(getCenterX() + edge.left*getScale() <= ent.getCenterX() + ent.getEdge().right*ent.getScale()) &&
(getCenterY() + edge.bottom*getScale() >= ent.getCenterY() + ent.getEdge().top*ent.getScale()) &&
(getCenterY() + edge.top*getScale() <= ent.getCenterY() + ent.getEdge().bottom*ent.getScale()) )
{
// set collision vector
collisionVector = *ent.getCenter() - *getCenter();
return true;
}
return false;
}
//=============================================================================
// Rotated Box collision detection method
// Called by collision()
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
// Uses Separating Axis Test to detect collision.
// The separating axis test:
// Two boxes are not colliding if their projections onto a line do not overlap.
//=============================================================================
bool Entity::collideRotatedBox(Entity &ent, VECTOR2 &collisionVector)
{
computeRotatedBox(); // prepare rotated box
ent.computeRotatedBox(); // prepare rotated box
if (projectionsOverlap(ent) && ent.projectionsOverlap(*this))
{
// set collision vector
collisionVector = *ent.getCenter() - *getCenter();
return true;
}
return false;
}
//=============================================================================
// Projects other box onto this edge01 and edge03.
// Called by collideRotatedBox()
// Post: returns true if projections overlap, false otherwise
//=============================================================================
bool Entity::projectionsOverlap(Entity &ent)
{
float projection, min01, max01, min03, max03;
// project other box onto edge01
projection = graphics->Vector2Dot(&edge01, ent.getCorner(0)); // project corner 0
min01 = projection;
max01 = projection;
// for each remaining corner
for(int c=1; c<4; c++)
{
// project corner onto edge01
projection = graphics->Vector2Dot(&edge01, ent.getCorner(c));
if (projection < min01)
min01 = projection;
else if (projection > max01)
max01 = projection;
}
if (min01 > edge01Max || max01 < edge01Min) // if projections do not overlap
return false; // no collision is possible
// project other box onto edge03
projection = graphics->Vector2Dot(&edge03, ent.getCorner(0)); // project corner 0
min03 = projection;
max03 = projection;
// for each remaining corner
for(int c=1; c<4; c++)
{
// project corner onto edge03
projection = graphics->Vector2Dot(&edge03, ent.getCorner(c));
if (projection < min03)
min03 = projection;
else if (projection > max03)
max03 = projection;
}
if (min03 > edge03Max || max03 < edge03Min) // if projections do not overlap
return false; // no collision is possible
return true; // projections overlap
}
//=============================================================================
// Rotated Box and Circle collision detection method
// Called by collision()
// Uses separating axis test on edges of box and radius of circle.
// If the circle center is outside the lines extended from the collision box
// edges (also known as the Voronoi region) then the nearest box corner is checked
// for collision using a distance check.
// The nearest corner is determined from the overlap tests.
//
// Voronoi0 | | Voronoi1
// ---0---1---
// | |
// ---3---2---
// Voronoi3 | | Voronoi2
//
// Pre: This entity must be box and other entity (ent) must be circle.
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
//=============================================================================
bool Entity::collideRotatedBoxCircle(Entity &ent, VECTOR2 &collisionVector)
{
float min01, min03, max01, max03, center01, center03;
computeRotatedBox(); // prepare rotated box
// project circle center onto edge01
center01 = graphics->Vector2Dot(&edge01, ent.getCenter());
min01 = center01 - ent.getRadius()*ent.getScale(); // min and max are Radius from center
max01 = center01 + ent.getRadius()*ent.getScale();
if (min01 > edge01Max || max01 < edge01Min) // if projections do not overlap
return false; // no collision is possible
// project circle center onto edge03
center03 = graphics->Vector2Dot(&edge03, ent.getCenter());
min03 = center03 - ent.getRadius()*ent.getScale(); // min and max are Radius from center
max03 = center03 + ent.getRadius()*ent.getScale();
if (min03 > edge03Max || max03 < edge03Min) // if projections do not overlap
return false; // no collision is possible
// circle projection overlaps box projection
// check to see if circle is in voronoi region of collision box
if(center01 < edge01Min && center03 < edge03Min) // if circle in Voronoi0
return collideCornerCircle(corners[0], ent, collisionVector);
if(center01 > edge01Max && center03 < edge03Min) // if circle in Voronoi1
return collideCornerCircle(corners[1], ent, collisionVector);
if(center01 > edge01Max && center03 > edge03Max) // if circle in Voronoi2
return collideCornerCircle(corners[2], ent, collisionVector);
if(center01 < edge01Min && center03 > edge03Max) // if circle in Voronoi3
return collideCornerCircle(corners[3], ent, collisionVector);
// circle not in voronoi region so it is colliding with edge of box
// set collision vector, uses simple center of circle to center of box
collisionVector = *ent.getCenter() - *getCenter();
return true;
}
//=============================================================================
// The box corner is checked for collision with circle using a distance check.
// Called by collideRotatedBoxCircle()
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
//=============================================================================
bool Entity::collideCornerCircle(VECTOR2 corner, Entity &ent, VECTOR2 &collisionVector)
{
distSquared = corner - *ent.getCenter(); // corner - circle
distSquared.x = distSquared.x * distSquared.x; // difference squared
distSquared.y = distSquared.y * distSquared.y;
// Calculate the sum of the radii, then square it
sumRadiiSquared = ent.getRadius()*ent.getScale(); // (0 + circleR)
sumRadiiSquared *= sumRadiiSquared; // square it
// if corner and circle are colliding
if(distSquared.x + distSquared.y <= sumRadiiSquared)
{
// set collision vector
collisionVector = *ent.getCenter() - corner;
return true;
}
return false;
}
//=============================================================================
// Compute corners of rotated box, projection edges and min and max projections
// 0---1 corner numbers
// | |
// 3---2
//=============================================================================
void Entity::computeRotatedBox()
{
if(rotatedBoxReady)
return;
float projection;
VECTOR2 rotatedX(cos(spriteData.angle), sin(spriteData.angle));
VECTOR2 rotatedY(-sin(spriteData.angle), cos(spriteData.angle));
const VECTOR2 *center = getCenter();
corners[0] = *center + rotatedX * ((float)edge.left*getScale()) +
rotatedY * ((float)edge.top*getScale());
corners[1] = *center + rotatedX * ((float)edge.right*getScale()) +
rotatedY * ((float)edge.top*getScale());
corners[2] = *center + rotatedX * ((float)edge.right*getScale()) +
rotatedY * ((float)edge.bottom*getScale());
corners[3] = *center + rotatedX * ((float)edge.left*getScale()) +
rotatedY * ((float)edge.bottom*getScale());
// corners[0] is used as origin
// The two edges connected to corners[0] are used as the projection lines
edge01 = VECTOR2(corners[1].x - corners[0].x, corners[1].y - corners[0].y);
graphics->Vector2Normalize(&edge01);
edge03 = VECTOR2(corners[3].x - corners[0].x, corners[3].y - corners[0].y);
graphics->Vector2Normalize(&edge03);
// this entities min and max projection onto edges
projection = graphics->Vector2Dot(&edge01, &corners[0]);
edge01Min = projection;
edge01Max = projection;
// project onto edge01
projection = graphics->Vector2Dot(&edge01, &corners[1]);
if (projection < edge01Min)
edge01Min = projection;
else if (projection > edge01Max)
edge01Max = projection;
// project onto edge03
projection = graphics->Vector2Dot(&edge03, &corners[0]);
edge03Min = projection;
edge03Max = projection;
projection = graphics->Vector2Dot(&edge03, &corners[3]);
if (projection < edge03Min)
edge03Min = projection;
else if (projection > edge03Max)
edge03Max = projection;
rotatedBoxReady = true;
}
//=============================================================================
// Is this Entity outside the specified rectangle
// Post: returns true if outside rect, false otherwise
//=============================================================================
bool Entity::outsideRect(RECT rect)
{
if( spriteData.x + spriteData.width*getScale() < rect.left ||
spriteData.x > rect.right ||
spriteData.y + spriteData.height*getScale() < rect.top ||
spriteData.y > rect.bottom)
return true;
return false;
}
//=============================================================================
// damage
// This entity has been damaged by a weapon.
// Override this function in the inheriting class.
//=============================================================================
void Entity::damage(int weapon)
{}
//=============================================================================
// Entity bounces after collision with another entity
//=============================================================================
void Entity::bounce(VECTOR2 &collisionVector, Entity &ent)
{
VECTOR2 Vdiff = ent.getVelocity() - velocity;
VECTOR2 cUV = collisionVector; // collision unit vector
Graphics::Vector2Normalize(&cUV);
float cUVdotVdiff = Graphics::Vector2Dot(&cUV, &Vdiff);
float massRatio = 2.0f;
if (getMass() != 0)
massRatio *= (ent.getMass() / (getMass() + ent.getMass()));
// If entities are already moving apart then bounce must
// have been previously called and they are still colliding.
// Move entities apart along collisionVector
if(cUVdotVdiff > 0)
{
setX(getX() - cUV.x * massRatio);
setY(getY() - cUV.y * massRatio);
}
else
deltaV += ((massRatio * cUVdotVdiff) * cUV);
}
//=============================================================================
// Force of gravity on this entity from other entity
// Adds the gravitational force to the velocity vector of this entity
// force = GRAVITY * m1 * m2 / r*r
// 2 2
// r*r = (Ax - Bx) + (Ay - By)
//=============================================================================
void Entity::gravityForce(Entity *ent, float frameTime)
{
// if either entity is not active then no gravity effect
if (!active || !ent->getActive())
return ;
rr = pow((ent->getCenterX() - getCenterX()),2) +
pow((ent->getCenterY() - getCenterY()),2);
force = gravity * ent->getMass() * mass/rr;
// --- Using vector math to create gravity vector ---
// Create vector between entities
VECTOR2 gravityV(ent->getCenterX() - getCenterX(),
ent->getCenterY() - getCenterY());
// Normalize the vector
Graphics::Vector2Normalize(&gravityV);
// Multipy by force of gravity to create gravity vector
gravityV *= force * frameTime;
// Add gravity vector to moving velocity vector to change direction
velocity += gravityV;
}
void Entity::newBounce(VECTOR2 &collisionVector, Entity &ent)
{
VECTOR2 Vdiff = ent.getVelocity() - velocity;
VECTOR2 cUV = collisionVector; // collision unit vector
Graphics::Vector2Normalize(&cUV);
//have to change cUV so it is rotated correctly
cUV *= cos(ent.getSpriteInfo().angle);
float cUVdotVdiff = Graphics::Vector2Dot(&cUV, &Vdiff);
float massRatio = 2.0f;
if (getMass() != 0)
massRatio *= (ent.getMass() / (getMass() + ent.getMass()));
// If entities are already moving apart then bounce must
// have been previously called and they are still colliding.
// Move entities apart along collisionVector
if(cUVdotVdiff > 0)
{
setX((getX() - cUV.x * massRatio));
setY((getY() - cUV.y * massRatio));
}
else
deltaV += ((massRatio * cUVdotVdiff) * cUV);
}