-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.cpp
More file actions
176 lines (162 loc) · 4.82 KB
/
Copy pathSprite.cpp
File metadata and controls
176 lines (162 loc) · 4.82 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
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
/*
Author: Nick Witten
Date Last Modified: 4/15/21
Organization: ECE2036 Class
Description:
Implements the sprite functions to control
the moving,drawing, and setting properties for
objects on the screen.
*/
#include <math.h>
#include "Sprite.h"
#include "uLCD_4DGL.h"
#include "graphic.h"
#include <vector>
extern uLCD_4DGL guLCD;
extern double gTimeStep;
extern double gOriginX;
extern double gOriginY;
/*******************************
Sprite constructor initializes
member variables.
*******************************/
Sprite::Sprite():
m_xCenter(gOriginX),
m_yCenter(gOriginY),
m_DirX(0.0),
m_DirY(0.0),
m_Velocity(0.0),
m_color(WHITE),
m_RotationVelocity(0.0),
m_RotationDirection(NONE),
m_numPoints(0),
m_bIsValid(false)
{
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// move
// Erase Sprite from current location
// Move the sprite to the new location
// Draw sprite at new location
void Sprite::move()
{
// Erase object
draw(true);
// Translate to the origin all points around the origin using the center points
for (int ii = 0 ; ii < m_numPoints ; ++ii)
{
m_xPoints[ii] -= m_xCenter;
m_yPoints[ii] -= m_yCenter;
}
// Rotate around the origin
float tmpX, tmpY;
for (int ii = 0 ; ii < m_numPoints ; ++ii)
{
tmpX = m_xPoints[ii]*std::cos(m_RotationVelocity*PI/180.0) - m_yPoints[ii]*std::sin(m_RotationVelocity*PI/180.0);
tmpY = m_yPoints[ii]*std::cos(m_RotationVelocity*PI/180.0) + m_xPoints[ii]*std::sin(m_RotationVelocity*PI/180.0);
m_xPoints[ii] = tmpX;
m_yPoints[ii] = tmpY;
}
// Translate back to original location
for (int ii = 0 ; ii < m_numPoints ; ++ii)
{
m_xPoints[ii] += m_xCenter;
m_yPoints[ii] += m_yCenter;
}
// Move to new location
for (int ii = 0 ; ii < m_numPoints ; ++ii)
{
m_xPoints[ii] += m_DirX*m_Velocity*(gTimeStep);
m_yPoints[ii] += m_DirY*m_Velocity*(gTimeStep);
}
m_xCenter += m_DirX*m_Velocity*(gTimeStep);
m_yCenter += m_DirY*m_Velocity*(gTimeStep);
// Draw at the new location
draw(false);
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// draw sprite at new location
void Sprite::draw(bool bErase)
{
int currentColor = BLACK;
if (!bErase)
{
currentColor = m_color;
}
if (m_numPoints < 2)
{
return;
}
else if (m_numPoints == 2)
{
guLCD.line((int)m_xPoints[m_numPoints-1], (int)m_yPoints[m_numPoints-1] , (int)m_xPoints[0], (int)m_yPoints[0], currentColor);
}
else
{
for (int ii = 0 ; ii < (m_numPoints-1) ; ++ii)
{
guLCD.line((int)m_xPoints[ii], (int)m_yPoints[ii] , (int)m_xPoints[ii+1], (int)m_yPoints[ii+1], currentColor);
}
// go from last point to first point
guLCD.line((int)m_xPoints[m_numPoints-1], (int)m_yPoints[m_numPoints-1] , (int)m_xPoints[0], (int)m_yPoints[0], currentColor);
}
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// addPoint
// Adds a point to the vector of points
void Sprite::addPoint(int x, int y)
{
m_xPoints[m_numPoints] = x;
m_yPoints[m_numPoints] = y;
m_numPoints++;
if (m_numPoints == NUM_POINTS)
{
m_numPoints = 0;
}
}
/********************************************
Sets the direction of the object to unit
vector (u, v)
********************************************/
void Sprite::setDirection(double u, double v)
{
m_DirX = u;
m_DirY = v;
}
/*******************************************
Returns the line segments of the object in
a multidimensional vector of the form:
[ [ [PointX1, PointY1], [PointX2, PointY2] ],
[ [PointX1, PointY1], [PointX2, PointY2] ] ]
*******************************************/
vector<vector<vector<double> > > Sprite::getLineSegments()
{
// Initialize vector
vector<vector<vector<double> > > segments;
// Loop through points
for (int ii = 0; ii < m_numPoints - 1; ++ii)
{
// Initialize segment vector
vector<vector<double> > segment;
// Create Point1
vector<double> point1;
point1.push_back(m_xPoints[ii]);
point1.push_back(m_yPoints[ii]);
segment.push_back(point1);
// Create Point2
int nextPoint = ii + 1;
if (nextPoint == m_numPoints)
{
nextPoint = 0;
}
vector<double> point2;
point2.push_back(m_xPoints[nextPoint]);
point2.push_back(m_yPoints[nextPoint]);
segment.push_back(point2);
segments.push_back(segment);
}
return segments;
}