-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.cpp
281 lines (257 loc) · 5.87 KB
/
Engine.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
#include <string.h>
#include <stdio.h>
#include "MathStructs.h"
#include "Table.h"
#include "Ball.h"
#include "Engine.h"
const double Engine::STICK_LENGTH = 1.25;
const double Engine::BALL_MASS = 1;
const double Engine::TABLE_HEIGHT = 1;
const double Engine::TABLE_WIDTH = 1.5;
const double Engine::BALL_RADIUS = 0.05;
const int Engine::TIME_INTERVAL = 40;
Engine::Engine():
table(Point(-0.75, -0.5, 0), TABLE_HEIGHT, TABLE_WIDTH),
cueBall(
Point(
-0.75 + (TABLE_WIDTH/6),
-0.50 + (TABLE_HEIGHT/2),
BALL_RADIUS
),
BALL_RADIUS,
BALL_MASS,
Ball::WHITE
),
eightBall(
Point(
-0.75 + (TABLE_WIDTH/6),
-0.50 + (5*TABLE_HEIGHT/2),
BALL_RADIUS
),
BALL_RADIUS,
BALL_MASS,
Ball::BLACK
),
stick(STICK_LENGTH),
initial(true)
{
directStick(Point(0.0, 0.0, 0.0));
eightBall.hide();
}
Table &Engine::getTable()
{
return table;
}
Ball &Engine::getCue()
{
return cueBall;
}
Ball &Engine::getEight()
{
return eightBall;
}
Stick &Engine::getStick()
{
return stick;
}
void Engine::nextStep(Ball &ball)
{
if (!ball.isPocketed()) // case the ball is not already in the hole
{
// check whether ball collide with borders or fit in a hole,
// and apply the proper forces
table.checkBorders(ball);
// update position according to the current velocity
ball.updateLocation();
// apply frictional impulse on the ball
table.applyFriction(ball);
}
}
Engine::State Engine::nextStep()
{
collideBalls();
nextStep(cueBall);
nextStep(eightBall);
if (isStatic() && (getState() != GAME_OVER))
stick.show();
if (isStabilized())
directStick(eightBall.getLocation());
return getState();
}
bool Engine::isStatic() const
{
return (cueBall.isStatic() && eightBall.isStatic());
}
bool Engine::isStabilized() const
{
static bool state = false;
bool oldState = state;
state = isStatic();
return ((state != oldState) && (state));
}
bool Engine::hit(Point dest)
{
if (initial)
setEightBallAt(dest);
else if (isStatic() /*&& (getState() == GAME_ON)*/)
{
directStick(dest);
stick.hit(cueBall);
stick.resetPower();
}
else
return false;
return true;
}
int Engine::shiftPower()
{
// return zero in case of initial state
if (initial)
return (int) !setEightBallAt();
else
return stick.shiftPower();
}
void Engine::collideBalls()
{
Vector dPos = ((Vector) cueBall.getLocation() + cueBall.getVelocity()) - ((Vector) eightBall.getLocation() + eightBall.getVelocity());
double xSpace = dPos.length() - (cueBall.getRadius() + eightBall.getRadius());
// if the two balls collide
if (xSpace <= 0)
{
// normal to the surface of the collision, toward the cue ball
Vector n = dPos.unit(); // unit vector
// calculate impulses applied on each ball
double impFactor = 2 * (cueBall.getMass() * eightBall.getMass()) / (cueBall.getMass() + eightBall.getMass());
double imp = impFactor * ((eightBall.getVelocity() - cueBall.getVelocity()).dot(n));
cueBall.applyImpulse(n * (+imp));
eightBall.applyImpulse(n * (-imp));
}
}
void Engine::directStick(Point p)
{
// calculate the inclination angle of the stick
double angle = ((Vector) p - (Vector) cueBall.getLocation()).inclination2D();
// direct the stick
stick.setAngle(angle);
// now put the stick at the right place
stick.putAt((Vector) cueBall.getLocation());
}
void Engine::reset()
{
initial = true;
cueBall.resetAt(Point(
table.getLeft() + (table.getWidth()/6),
table.getBottom() + (table.getHeight()/2),
eightBall.getRadius()
));
cueBall.show();
eightBall.resetAt(Point(
table.getRight() - (table.getWidth()/6),
table.getBottom() + (table.getHeight()/2),
eightBall.getRadius()
));
eightBall.hide();
}
Engine::State Engine::getState() const
{
if (initial)
return INITIAL;
else if (cueBall.isPocketed())
return GAME_OVER;
else if (eightBall.isPocketed())
return YOU_WIN;
else
return GAME_ON;
}
double Engine::howFar() const
{
return ((Vector) cueBall.getLocation() - (Vector) eightBall.getLocation()).length();
}
double Engine::cueInitialVelocity() const
{
return stick.getImpulse() / cueBall.getMass();
}
int Engine::estimateHeadOnTime() const
{
// regardless the friction
return (int) (howFar() / cueInitialVelocity()) * timeInterval();
}
char *Engine::infoString(int line, char *out) const
{
switch (line)
{
case 1:
switch (getState())
{
case GAME_ON:
strcpy(out, "Game On.");
break;
case GAME_OVER:
strcpy(out, "Game Over.");
break;
case YOU_WIN:
strcpy(out, "You Win.");
break;
case INITIAL:
strcpy(out, "Initial Stage.");
break;
}
break;
case 2:
if (initial)
sprintf(out, "");
else
sprintf(out, "Current stick power level: %d.", stick.getPower());
break;
case 3:
if (initial)
sprintf(out, "");
else
sprintf(out, "Initial Speed: %.2f.", cueInitialVelocity());
break;
case 4:
if (initial)
sprintf(out, "Left Click: Put The Eight Ball.");
else
sprintf(out, "Distance between tow balls: %.2f.", howFar());
break;
case 5:
if (initial)
sprintf(out, "Right Click: Default Place.");
else
sprintf(out, "Estimated time: %d ms.", estimateHeadOnTime());
break;
case 6:
if (initial)
sprintf(out, "");
else
sprintf(out, "(in case of head on collision)");
break;
default:
sprintf(out, "");
break;
}
return out;
}
bool Engine::setEightBallAt(Point pos)
{
// insure its the initial state
if (getState() != INITIAL)
return false;
eightBall.putAt(pos);
eightBall.show();
initial = false;
return true;
}
bool Engine::setEightBallAt()
{
return setEightBallAt(Point(
table.getRight() - (table.getWidth()/6),
table.getBottom() + (table.getHeight()/2),
eightBall.getRadius()
));
}
int Engine::timeInterval() const
{
return TIME_INTERVAL;
}