-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.cpp
More file actions
449 lines (379 loc) · 11.7 KB
/
simulation.cpp
File metadata and controls
449 lines (379 loc) · 11.7 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
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
/*-----------------------------------------------------------
Simulation Source File
-----------------------------------------------------------*/
#include"stdafx.h"
#include"simulation.h"
#include<stdio.h>
#include<stdlib.h>
/*-----------------------------------------------------------
macros
-----------------------------------------------------------*/
#define SMALL_VELOCITY (0.01f)
/*-----------------------------------------------------------
globals
-----------------------------------------------------------*/
/*
vec2 gPlaneNormal_Left(1.0,0.0);
vec2 gPlaneNormal_Top(0.0,1.0);
vec2 gPlaneNormal_Right(-1.0,0.0);
vec2 gPlaneNormal_Bottom(0.0,-1.0);
*/
table gTable;
lane gLane;
static const float gRackPositionX[] = {0.0f,0.0f,(BALL_RADIUS*5.0f),(-BALL_RADIUS*10.0f),(BALL_RADIUS*10.0f)};
static const float gRackPositionZ[] = {0.5f,0.0f,(-BALL_RADIUS*3.0f),(-BALL_RADIUS*3.0f)};
float gCoeffRestitution = 0.5f;
float gCoeffFriction = 0.03f;
float gGravityAccn = 9.8f;
/*-----------------------------------------------------------
cushion class members
-----------------------------------------------------------*/
void cushion::MakeNormal(void)
{
//can do this in 2d
vec2 temp = vertices[1]-vertices[0];
normal(0) = temp(1);
normal(1) = -temp(0);
normal.Normalise();
}
void cushion::MakeCentre(void)
{
centre = vertices[0];
centre += vertices[1];
centre/=2.0;
}
/*-----------------------------------------------------------
player class members
-----------------------------------------------------------*/
int player::playerIndex = 0;
/*-----------------------------------------------------------
ball class members
-----------------------------------------------------------*/
int ball::ballIndexCnt = 0;
void ball::Reset(void)
{
//set velocity to zero
velocity = 0.0;
//set touched to 0
touched = 0;
//work out rack position
if(index==0)
{
position(1) = 0.5;
position(0) = 0.0;
return;
}
static const float sep = (BALL_RADIUS*3.0f);
static const float rowSep = (BALL_RADIUS*2.0f);
int row = 1;
int rowIndex = index;
while(rowIndex > row)
{
rowIndex -= row;
row++;
}
position(1) = -(rowSep * (row-1)) - 3.5f;
position(0) = (((row-1)*sep)/2.0f) - (sep*(row-rowIndex));
}
void ball::ApplyImpulse(vec2 imp)
{
velocity = imp;
}
void ball::ApplyFrictionForce(int ms)
{
if(velocity.Magnitude()<=0.0) return;
//accelaration is opposite to direction of motion
vec2 accelaration = -velocity.Normalised();
//friction force = constant * mg
//F=Ma, so accelaration = force/mass = constant*g
accelaration *= (gCoeffFriction * gGravityAccn);
//integrate velocity : find change in velocity
vec2 velocityChange = ((accelaration * ms)/1000.0f);
//cap magnitude of change in velocity to remove integration errors
if(velocityChange.Magnitude() > velocity.Magnitude()) velocity = 0.0;
else velocity += velocityChange;
}
void ball::DoBallCollision(ball &b)
{
if(HasHitBall(b)) HitBall(b);
}
void ball::DoPlaneCollision(const cushion &b)
{
if(HasHitPlane(b)) HitPlane(b);
}
void ball::Update(int ms)
{
//apply friction
ApplyFrictionForce(ms);
//integrate position
position += ((velocity * ms)/1000.0f);
//set small velocities to zero
if(velocity.Magnitude()<SMALL_VELOCITY) velocity = 0.0;
}
bool ball::HasHitPlane(const cushion &c) const
{
//if moving away from plane, cannot hit
if(velocity.Dot(c.normal) >= 0.0) return false;
//if in front of plane, then have not hit
vec2 relPos = position - c.vertices[0];
double sep = relPos.Dot(c.normal);
if(sep > radius) return false;
return true;
}
bool ball::HasHitBall(const ball &b) const
{
//work out relative position of ball from other ball,
//distance between balls
//and relative velocity
vec2 relPosn = position - b.position;
float dist = (float) relPosn.Magnitude();
vec2 relPosnNorm = relPosn.Normalised();
vec2 relVelocity = velocity - b.velocity;
//if moving apart, cannot have hit
if(relVelocity.Dot(relPosnNorm) >= 0.0) return false;
//if distnce is more than sum of radii, have not hit
if(dist > (radius+b.radius)) return false;
return true;
}
void ball::HitPlane(const cushion &c)
{
//reverse velocity component perpendicular to plane
double comp = velocity.Dot(c.normal) * (1.0+gCoeffRestitution);
vec2 delta = -(c.normal * comp);
velocity += delta;
//make some particles
int n = (rand()%4)+3;
vec3 pos(position(0),radius/2.0,position(1));
vec3 oset(c.normal(0),0.0,c.normal(1));
pos+=(oset*radius);
for(int i=0;i<n;i++)
{
gTable.parts.AddParticle(pos);
}
/*
//assume elastic collision
//find plane normal
vec2 planeNorm = gPlaneNormal_Left;
//split velocity into 2 components:
//find velocity component perpendicular to plane
vec2 perp = planeNorm*(velocity.Dot(planeNorm));
//find velocity component parallel to plane
vec2 parallel = velocity - perp;
//reverse perpendicular component
//parallel component is unchanged
velocity = parallel + (-perp)*gCoeffRestitution;
*/
}
void ball::HitBall(ball &b)
{
//find direction from other ball to this ball
vec2 relDir = (position - b.position).Normalised();
//split velocities into 2 parts: one component perpendicular, and one parallel to
//the collision plane, for both balls
//(NB the collision plane is defined by the point of contact and the contact normal)
float perpV = (float)velocity.Dot(relDir);
float perpV2 = (float)b.velocity.Dot(relDir);
vec2 parallelV = velocity-(relDir*perpV);
vec2 parallelV2 = b.velocity-(relDir*perpV2);
//Calculate new perpendicluar components:
//v1 = (2*m2 / m1+m2)*u2 + ((m1 - m2)/(m1+m2))*u1;
//v2 = (2*m1 / m1+m2)*u1 + ((m2 - m1)/(m1+m2))*u2;
float sumMass = mass + b.mass;
float perpVNew = (float)((perpV*(mass-b.mass))/sumMass) + (float)((perpV2*(2.0*b.mass))/sumMass);
float perpVNew2 = (float)((perpV2*(b.mass-mass))/sumMass) + (float)((perpV*(2.0*mass))/sumMass);
//find new velocities by adding unchanged parallel component to new perpendicluar component
velocity = parallelV + (relDir*perpVNew);
b.velocity = parallelV2 + ((relDir*perpVNew2)/(1.5+(((float)(rand() % 10)) / 10)));
b.touched = 1;
//make some particles
int n = (rand()%5)+5;
vec3 pos(position(0),radius/2.0,position(1));
vec3 oset(relDir(0),0.0,relDir(1));
pos+=(oset*radius);
for(int i=0;i<n;i++)
{
gTable.parts.AddParticle(pos);
}
}
/*-----------------------------------------------------------
particle class members
-----------------------------------------------------------*/
void particle::update(int ms)
{
position += (velocity*ms)/1000.0;
velocity(1) -= (4.0*ms)/1000.0; //(9.8*ms)/1000.0;
}
/*-----------------------------------------------------------
particle set class members
-----------------------------------------------------------*/
void particleSet::AddParticle(const vec3 &pos)
{
if(num >= MAX_PARTICLES) return;
particles[num] = new particle;
particles[num]->position = pos;
particles[num]->velocity(0) = ((rand() % 200)-100)/200.0;
particles[num]->velocity(2) = ((rand() % 200)-100)/200.0;
particles[num]->velocity(1) = 2.0*((rand() % 100)/100.0);
num++;
}
void particleSet::update(int ms)
{
int i=0;
while(i<num)
{
particles[i]->update(ms);
if((particles[i]->position(1) < 0.0) && (particles[i]->velocity(1)<0.0))
{
delete particles[i];
particles[i] = particles[num-1];
num--;
}
else i++;
}
}
/*-----------------------------------------------------------
table class members
-----------------------------------------------------------*/
void table::SetupCushions(void)
{
cushions[0].vertices[0](0) = -TABLE_X;
cushions[0].vertices[0](1) = -TABLE_Z;
cushions[0].vertices[1](0) = -TABLE_X;
cushions[0].vertices[1](1) = TABLE_Z;
cushions[1].vertices[0](0) = -TABLE_X;
cushions[1].vertices[0](1) = TABLE_Z;
cushions[1].vertices[1](0) = TABLE_X;
cushions[1].vertices[1](1) = TABLE_Z;
cushions[2].vertices[0](0) = TABLE_X;
cushions[2].vertices[0](1) = TABLE_Z;
cushions[2].vertices[1](0) = TABLE_X;
cushions[2].vertices[1](1) = -TABLE_Z + 0.3;
cushions[3].vertices[0](0) = TABLE_X;
cushions[3].vertices[0](1) = -TABLE_Z + 0.3;
cushions[3].vertices[1](0) = TABLE_X - 0.3;
cushions[3].vertices[1](1) = -TABLE_Z;
cushions[4].vertices[0](0) = TABLE_X - 0.3;
cushions[4].vertices[0](1) = -TABLE_Z;
cushions[4].vertices[1](0) = -TABLE_X;
cushions[4].vertices[1](1) = -TABLE_Z;
for(int i=0;i<NUM_CUSHIONS;i++)
{
cushions[i].MakeCentre();
cushions[i].MakeNormal();
}
}
void table::Update(int ms)
{
//check for collisions for each ball
for(int i=0;i<NUM_BALLS;i++)
{
for(int j=0;j<NUM_CUSHIONS;j++)
{
balls[i].DoPlaneCollision(cushions[j]);
}
for(int j=(i+1);j<NUM_BALLS;j++)
{
balls[i].DoBallCollision(balls[j]);
}
}
//update all balls
for(int i=0;i<NUM_BALLS;i++) balls[i].Update(ms);
//update particles
parts.update(ms);
//make some new particles
//vec3 pos(0.0,BALL_RADIUS,0.0);
//parts.AddParticle(pos);
}
bool table::AnyBallsMoving(void) const
{
//return true if any ball has a non-zero velocity
for(int i=0;i<NUM_BALLS;i++)
{
if(balls[i].velocity(0)!=0.0) return true;
if(balls[i].velocity(1)!=0.0) return true;
}
return false;
}
/*-----------------------------------------------------------
lane class members
-----------------------------------------------------------*/
void lane::SetupCushions(void)
{
cushions[0].vertices[0](0) = -TABLE_X +1.55f;
cushions[0].vertices[0](1) = -TABLE_Z;
cushions[0].vertices[1](0) = -TABLE_X + 1.55f;
cushions[0].vertices[1](1) = TABLE_Z;
cushions[1].vertices[0](0) = -TABLE_X + 1.55f;
cushions[1].vertices[0](1) = TABLE_Z;
cushions[1].vertices[1](0) = TABLE_X + 1.55f;
cushions[1].vertices[1](1) = TABLE_Z;
cushions[2].vertices[0](0) = TABLE_X + 1.55f;
cushions[2].vertices[0](1) = TABLE_Z;
cushions[2].vertices[1](0) = TABLE_X + 1.55f;
cushions[2].vertices[1](1) = -TABLE_Z ;
cushions[3].vertices[0](0) = TABLE_X + 1.55f;
cushions[3].vertices[0](1) = -TABLE_Z ;
cushions[3].vertices[1](0) = TABLE_X + 1.55f;
cushions[3].vertices[1](1) = -TABLE_Z;
cushions[4].vertices[0](0) = TABLE_X + 1.55f;
cushions[4].vertices[0](1) = -TABLE_Z;
cushions[4].vertices[1](0) = -TABLE_X + 1.55f;
cushions[4].vertices[1](1) = -TABLE_Z;
cushions[5].vertices[0](0) = -TABLE_X - 1.55f;
cushions[5].vertices[0](1) = -TABLE_Z;
cushions[5].vertices[1](0) = -TABLE_X - 1.55f;
cushions[5].vertices[1](1) = TABLE_Z;
cushions[6].vertices[0](0) = -TABLE_X - 1.55f;
cushions[6].vertices[0](1) = TABLE_Z;
cushions[6].vertices[1](0) = TABLE_X - 1.55f;
cushions[6].vertices[1](1) = TABLE_Z;
cushions[7].vertices[0](0) = TABLE_X - 1.55f;
cushions[7].vertices[0](1) = TABLE_Z;
cushions[7].vertices[1](0) = TABLE_X - 1.55f;
cushions[7].vertices[1](1) = -TABLE_Z;
cushions[8].vertices[0](0) = TABLE_X - 1.55f;
cushions[8].vertices[0](1) = -TABLE_Z;
cushions[8].vertices[1](0) = TABLE_X - 1.55f;
cushions[8].vertices[1](1) = -TABLE_Z;
cushions[9].vertices[0](0) = TABLE_X - 1.55f;
cushions[9].vertices[0](1) = -TABLE_Z;
cushions[9].vertices[1](0) = -TABLE_X - 1.55f;
cushions[9].vertices[1](1) = -TABLE_Z;
for (int i = 0; i < NUM_CUSHIONS_lane; i++)
{
cushions[i].MakeCentre();
cushions[i].MakeNormal();
}
}
void lane::Update(int ms)
{
//check for collisions for each ball
for (int i = 0; i < NUM_BALLS; i++)
{
for (int j = 0; j < NUM_CUSHIONS; j++)
{
balls[i].DoPlaneCollision(cushions[j]);
}
for (int j = (i + 1); j < NUM_BALLS; j++)
{
balls[i].DoBallCollision(balls[j]);
}
}
//update all balls
for (int i = 0; i < NUM_BALLS; i++) balls[i].Update(ms);
//update particles
parts.update(ms);
//make some new particles
//vec3 pos(0.0,BALL_RADIUS,0.0);
//parts.AddParticle(pos);
}
bool lane::AnyBallsMoving(void) const
{
//return true if any ball has a non-zero velocity
for (int i = 0; i < NUM_BALLS; i++)
{
if (balls[i].velocity(0) != 0.0) return true;
if (balls[i].velocity(1) != 0.0) return true;
}
return false;
}