/
Object.h
480 lines (419 loc) · 11.7 KB
/
Object.h
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
475
476
477
478
479
480
#ifndef OBJECT_H
#define OBJECT_H
#include "Color.h"
using namespace std;
//CLASS HEADER:
class Object {
public:
//Default constructor for Object
Object();
//Constructor for object with position, width, height, color, and
//density specified
Object(int xPos_,
int yPos_,
Color* ptrColor_,
int width_,
int height_,
double density_,
int id_);
//Destructor for Object
~Object();
//Assignment operator for Object
Object& operator= (const Object &rhs);
//Copy constructor for Object
Object(const Object &other);
//FUNCTIONS USED IN CALCULATION
//Function to define the constants used in calculation (Default values)
void setDefaultConstants();
//SET AND GET FUNCTIONS FOR DISPLAY PROPERTIES
//Get function for ptrColor
Color* getPtrColor();
//Set function for ptrColor
void setPtrColor(Color* ptrColor_);
//Function to get the x position on the screen, in pixels
int xPosGet();
//Function to set the x position on the screen, in pixels
void xPosSet(int pos_);
//Function to get the y position on the screen, in pixels
int yPosGet();
//Function to set the y position on the screen, in pixels
void yPosSet(int pos_);
//Returns the number of elements that the data array does (or will) hold
int getNumElts();
//Returns the element of dataPtr at the specified index
int getElt(int index);
//Returns the last object with which the object collided;
int getLastCollided();
//Sets the last object with which the object collided();
void setLastCollided(int lastCollided_);
//Gets the object id
int getObjectID();
//Sets the object id
int setObjectID(int id_);
//Gets the x velocity following the object's last collision
double vxColGet();
//Sets the x velocity following the object's last collision
double vxColSet(double vxCol_);
//Gets the y velocity following the object's last collision
double vyColGet();
//Sets the y velocity following the object's last collision
double vyColSet(double vyCol_);
//Function to build the data array
void buildData();
//SET AND GET FUNCTIONS FOR PHYSICAL PROPERTIES
//WIDTH/HEIGHT
//Get function for width
double getWidth();
//Set function for width
void setWidth(double width_);
//Get function for height
double getHeight();
//Set function for height
void setHeight(double height_);
//MASS
//Get function for mass
double getMass();
//Set function for mass (calculated based on density and area)
void setMass(double sizeOfPixel);
//DENSITY
//Get function for density
double getDensity();
//Set function for density
void setDensity(double density_);
//POSITION
//Get function for xc
double xcGet();
//Set function for xc (calculated based on width and xi)
void xcSet(double sizeOfPixel);
//Get function for yc
double ycGet();
//Set function for yc (calculated based on height and yi)
void ycSet(double sizeOfPixel);
//Get function for xi
double xiGet();
//Set function for xi
void xiSet(double xi_);
//Get function for yi
double yiGet();
//Set function for yi
void yiSet(double yi_);
//Get function for xf
double xfGet();
//Set function for xf
void xfSet(double xf_);
//Get function for yf
double yfGet();
//Set function for yf
void yfSet(double yf_);
//VELOCITY
//Get function for vxi
double vxiGet();
//Set function for vxi
void vxiSet(double vxi_);
//Get function for vyi
double vyiGet();
//Set function for vyi
void vyiSet(double vyi_);
//Get function for vxf
double vxfGet();
//Set function for vxf
void vxfSet(double vxf_);
//Get function for vyf
double vyfGet();
//Set function for vyf
void vyfSet(double vyf_);
private:
//COLLISION INFORMATION
int lastCollided;
int id;
double vxCol;
double vyCol;
//DISPLAY PROPERTIES
int xPos;
int yPos;
Color* ptrColor;
int width;
int height;
int* ptrData;
bool dataFull;
//PHYSICAL PROPERTIES
double density; //in (kg)/(m^3)
double mass; //in kg
//xc and yc denote the coordinates of the centers of these objects, in m
double xc;
double yc;
//initial and final x and y positions of top left corners, in meters
double xi;
double yi;
double xf;
double yf;
//initial and final x and y velocities, in m/s
double vxi;
double vyi;
double vxf;
double vyf;
};
//DEFINITIONS:
//Default constructor for Object
Object::Object() :
xPos(0),
yPos(0),
ptrColor(new Color()),
width(1),
height(1),
ptrData(new int[7]),
dataFull(false),
density(1000),
id(-1) {
lastCollided = -1;
}
//Constructor for object with position, width, height, and color specified
Object::Object(int xPos_,
int yPos_,
Color* ptrColor_,
int width_,
int height_,
double density_,
int id_) :
xPos(xPos_),
yPos(yPos_),
ptrColor(new Color(*ptrColor_)),
width(width_),
height(height_),
ptrData(new int[(3 * width_ * height_) + 4]),
dataFull(false),
density(density_),
id(id_) {
lastCollided = -1;
}
//Destructor for Object
Object::~Object(){
delete ptrColor;
delete[] ptrData;
}
//FUNCTIONS USED IN CALCULATION
//Function to define the constants used in calculation to their default values
void Object::setDefaultConstants() {
//Density, in (kg)/(m^3)
//NOTE: Based on my calculations, this will vary extremely with the scale of
// the objects being displayed. So for a planet sized object it should
// be maybe something around 1*10^6. Not sure about other scales.
density = 1000;
}
//Get function for ptrColor
Color* Object::getPtrColor() {
return ptrColor;
}
//Set function for ptrColor
void Object::setPtrColor(Color* ptrColor_) {
ptrColor = ptrColor_;
}
//Function to get the x position on the screen, in pixels
int Object::xPosGet() {
return xPos;
}
//Function to set the x position on the screen, in pixels
void Object::xPosSet(int pos_) {
xPos = pos_;
}
//Function to get the y position on the screen, in pixels
int Object::yPosGet() {
return yPos;
}
//Function to set the y position on the screen, in pixels
void Object::yPosSet(int pos_) {
yPos = pos_;
}
//Returns the number of elements that the data array does (or will) hold
//NOTE: Should create some kind of invariant later to test this and make sure
// that it's actually the same size of the data array.
int Object::getNumElts() {
if (dataFull) {
return (3 * width * height) + 4;
}
//If ptrData is not full, assume that it has yet to be populated
else {
return 0;
}
}
//Returns the element of dataPtr at the specified index
int Object::getElt(int index) {
int elt;
if (dataFull) {
elt = ptrData[index];
}
else {
cout << "dataFull == false" << endl;
elt = 0;
}
return elt;
}
//Returns the last object with which the object collided;
int Object::getLastCollided(){
return lastCollided;
}
//Sets the last object with which the object collided();
void Object::setLastCollided(int lastCollided_) {
lastCollided = lastCollided_;
}
//Gets the object id
int Object::getObjectID() {
return id;
}
//Sets the object id
int Object::setObjectID(int id_) {
id = id_;
}
//Gets the x velocity following the object's last collision
double Object::vxColGet() {
return vxCol;
}
//Sets the x velocity following the object's last collision
double Object::vxColSet(double vxCol_) {
vxCol = vxCol_;
}
//Gets the y velocity following the object's last collision
double Object::vyColGet() {
return vyCol;
}
//Sets the y velocity following the object's last collision
double Object::vyColSet(double vyCol_) {
vyCol = vyCol_;
}
//Function to build the data array
void Object::buildData() {
int r = ptrColor->getRed();
int g = ptrColor->getGreen();
int b = ptrColor->getBlue();
ptrData[0] = xPos;
ptrData[1] = yPos;
ptrData[2] = width;
ptrData[3] = height;
for (int h = 0; h < height; ++h) {
for (int w = 0; w < width; ++w) {
ptrData[(3 * ((h * width) + w)) + 4] = r;
ptrData[(3 * ((h * width) + w)) + 5] = g;
ptrData[(3 * ((h * width) + w)) + 6] = b;
}
}
dataFull = true;
}
//SET AND GET FUNCTIONS FOR OBJECT PHYSICAL PROPERTIES
//WIDTH/HEIGHT
//Get function for width
double Object::getWidth() {
return width;
}
//Set function for width
void Object::setWidth(double width_) {
width = width_;
}
//Get function for height
double Object::getHeight() {
return height;
}
//Set function for height
void Object::setHeight(double height_) {
height = height_;
}
//MASS
//Get function for mass
double Object::getMass() {
return mass;
}
//Set function for mass (calculated based on density and area)
void Object::setMass(double sizeOfPixel) {
mass = (double(width * height) * pow(sizeOfPixel, 2) * density);
}
//DENSITY
//Get function for density
double Object::getDensity() {
return density;
}
//Set function for density
void Object::setDensity(double density_) {
density = density_;
}
//POSITION
//Get function for xc
double Object::xcGet() {
return xc;
}
//Set function for xc (calculated based on width and xi)
void Object::xcSet(double sizeOfPixel) {
xc = xi + ((double(width) / 2) * sizeOfPixel);
}
//Get function for yc
double Object::ycGet() {
return yc;
}
//Set function for yc (calculated based on height and yi)
void Object::ycSet(double sizeOfPixel) {
yc = yi + ((double(height) / 2) * sizeOfPixel);
}
//Get function for xi
double Object::xiGet() {
return xi;
}
//Set function for xi
void Object::xiSet(double xi_) {
xi = xi_;
}
//Get function for yi
double Object::yiGet() {
return yi;
}
//Set function for yi
void Object::yiSet(double yi_) {
yi = yi_;
}
//Get function for xf
double Object::xfGet() {
return xf;
}
//Set function for xf
void Object::xfSet(double xf_) {
xf = xf_;
}
//Get function for yf
double Object::yfGet() {
return yf;
}
//Set function for yf
void Object::yfSet(double yf_) {
yf = yf_;
}
//VELOCITY
//Get function for vxi
double Object::vxiGet() {
return vxi;
}
//Set function for vxi
void Object::vxiSet(double vxi_) {
vxi = vxi_;
}
//Get function for vyi
double Object::vyiGet() {
return vyi;
}
//Set function for vyi
void Object::vyiSet(double vyi_) {
vyi = vyi_;
}
//Get function for vxf
double Object::vxfGet() {
return vxf;
}
//Set function for vxf
void Object::vxfSet(double vxf_) {
vxf = vxf_;
}
//Get function for vyf
double Object::vyfGet() {
return vyf;
}
//Set function for vyf
void Object::vyfSet(double vyf_) {
vyf = vyf_;
}
#endif //OBJECT_H