-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGO.cpp
More file actions
211 lines (197 loc) · 4.26 KB
/
GO.cpp
File metadata and controls
211 lines (197 loc) · 4.26 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
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
/* Author: Brendon Kofink
* Johann Rajadurai
* Aaron Sierra
* David Day
* Lucy Ray
* Assignment Title: Ball Game
* Assignment Description: user can launch balls to hit objects.
* Due Date: 12/08/2021
* Date Created: 11/02/2021
* Date Last Modified: 12/04/2021
*/
#include "GO.h"
GO::GO(vec2 center, Shape shape)
{
this->center = center;
this->shape = shape;
physics = DEFAULT_PHYSICS;
moveMethod = EASE;
}
void GO::Init() {
shape.setCenter(¢er);
}
void GO::setDeltaTime(double deltaTime)
{
this->deltaTime = deltaTime;
}
void GO::setCenter(vec2 center)
{
this->center = center;
}
void GO::setPhysics(Physics physics)
{
this->physics = physics;
}
void GO::setMoveMethod(MoveMethod moveMethod)
{
this->moveMethod = moveMethod;
}
void GO::setMoving(bool moving)
{
this->moving = moving;
}
void GO::setVisible(bool visible)
{
this->visible = visible;
}
void GO::SetDest(vec2 dest)
{
destination = dest;
setMoving(true);
}
vec2 GO::getCenter()
{
return center;
}
Physics GO::getPhysics()
{
return physics;
}
MoveMethod GO::getMoveMethod()
{
return moveMethod;
}
bool GO::isMoving()
{
return moving;
}
bool GO::isColliding()
{
return colliding;
}
bool GO::isVisible()
{
return visible;
}
void GO::Translate(double x, double y)
{
center.x += x;
center.y += y;
}
void GO::Translate(vec2 delta)
{
center += delta;
}
void GO::ApplyForce(vec2 force)
{
physics.velocity += force;
}
void GO::update(SDL_Plotter &g)
{
// check if the object is moving
// if it is moving, cover up the old "frame" and draw new one
// if it has reached its destination, isMoving = false
// still draw things that aren't moving
colliding = false;
if (isMoving() || moveMethod == PHYSICS)
{
if(visible) {
erase(g);
}
// Linear movement
if (moveMethod == LINEAR)
{
vec2 linear = destination - getCenter();
linear = linear.normalized();
center += (linear*deltaTime*1.2);
}
// Ease movement
else if (moveMethod == EASE)
{
setCenter(center.lerp(destination, .01 * deltaTime));
// setCenter(center.lerp(destination, .01));
}
// Physics movement
else if (moveMethod == PHYSICS)
{
physics.velocity += DOWN * deltaTime * 0.01;
center += physics.velocity * deltaTime;
// physics.velocity += DOWN * 0.03;
// center += physics.velocity;
}
if ((center - destination).sqrMagnitude() < 0.5)
{
setMoving(false);
}
}
if((angle!=shape.getAngle() || scaleChange!=0) && visible)
{
if(!moving) {
erase(g);
}
shape.setAngle(angle);
shape.setRadius(shape.getRadius()+scaleChange*deltaTime);
}
scaleChange = 0;
if (visible)
{
shape.draw(g,center);
}
Init();
}
void GO::erase(SDL_Plotter &g)
{
Color color = shape.getColor();
shape.setColor(BLANK);
shape.draw(g,center);
shape.setColor(color);
}
// Use their shapes to check collision and apply force if they are.
// The GameController will call this function for you if it thinks the two are colliding.
void GO::CheckCollision(GO& other)
{
if (physics.canCollide && other.physics.canCollide)
{
if (shape.isColliding(other.shape))
{
Collide(other);
other.Collide(*this);
}
}
}
//Called if is colliding.
void GO::Collide(GO& other)
{
if(moveMethod == PHYSICS) {
vec2 normal = other.shape.getCollisionAxis(shape).normalized();
vec2 relativeVelocity = physics.velocity - other.physics.velocity;
double normalVelocity = normal.dot(relativeVelocity);
if (normalVelocity > 0)
{
normal = normal * -1;
normalVelocity = normal.dot(relativeVelocity);
}
double e = (physics.bounce + other.physics.bounce) / 2;
double j = -(1 + e) * normalVelocity;
double impulse = j / (physics.mass + other.physics.mass);
vec2 impulsePerMass = normal * impulse;
physics.velocity += impulsePerMass * physics.mass;
}
colliding = true;
}
void GO::Rotate(double a)
{
angle += a * deltaTime;
}
void GO::Scale(double factor)
{
scaleChange += factor;
}
void GO::SetColor(Color color)
{
shape.setColor(color);
}
Shape GO::getShape()
{
return shape;
}