[ create a new paste ] login | about

Link: http://codepad.org/0Q0zWxD4    [ raw code | fork ]

Evetro - C++, pasted on Apr 17:
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
/*
 * Ball Collision, simulates collision between several
 * freely moving rigid balls in 3D (intermediate).
 * Uses FreeGLUT for graphics.
 * Press SPACE or ENTER to generate new circles, ESC or Q to quit.
 */
#include <cstdlib>    // for rand/srand
#include "GL/freeglut.h"  // normally should be <>
#include <ctime>      // for clock
#include <iostream>   // logging to console
#include <cmath>      // for sqrt


//
// simple definitions

/// 3D vector
struct Vec3
{
  double x, y, z;
  
  explicit Vec3(double nx = 0.0, double ny = 0.0, double nz = 0.0)
    : x(nx), y(ny), z(nz)
  {
  }

  Vec3& operator+=(const Vec3 &other)
  {
    x += other.x;
    y += other.y;
    z += other.z;
    return *this;
  }

  Vec3& operator-=(const Vec3 &other)
  {
    x -= other.x;
    y -= other.y;
    z -= other.z;
    return *this;
  }

  Vec3& operator*=(const double coef)
  {
    x *= coef;
    y *= coef;
    z *= coef;
    return *this;
  }

  double length() const
  {
    return std::sqrt(x * x + y * y + z * z);
  }
};

inline Vec3 operator+(const Vec3 &a, const Vec3 &b)
{
  return Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
}

inline Vec3 operator-(const Vec3 &a, const Vec3 &b)
{
  return Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
}

inline Vec3 operator*(double coef, const Vec3 &v)
{
  return Vec3(coef * v.x, coef * v.y, coef * v.z);
}

inline double operator*(const Vec3 &a, const Vec3 &b)
{
  return a.x * b.x + a.y * b.y + a.z * b.z;
}

std::ostream& operator<<(std::ostream &os, const Vec3 &v)
{
  return os << '(' << v.x << ", " << v.y << ", " << v.z << ')';
}


/// RGBA color
struct Color
{
  float red, green, blue, alpha;

  explicit Color(float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 1.0f)
    : red(r), green(g), blue(b), alpha(a)
  {
  }

  void select() const
  {
    glColor4f(red, green, blue, alpha);
  }
};


/// rigid ball
struct Ball
{
  Vec3 p; ///< position
  Vec3 v; ///< velocity
  Vec3 a; ///< acceleration
  double m; ///< mass
  double r; ///< radius
  Color color;

  Ball
    (
      const Vec3 &pos = Vec3(),
      const Vec3 &vel = Vec3(),
      double mass = 1.0, 
      double rad = 10.0,
      const Color &col = Color(1.0f, 1.0f) // yellow by default
    )
      : p(pos), v(vel), m(mass), r(rad), color(col), a()
  {
  }

  /// OpenGL code to bring Ball to the frame buffer is here
  void paint() const
  {
    glLoadIdentity();
    glTranslated(p.x, p.y, p.z);
    glScaled(r, r, r);
    color.select();
    glCallList(1);
  }

  /// calculate dynamics
  void move(double time_step)
  {
    const Vec3 ta = time_step * a;
    p += time_step * (v + 0.5 * ta);
    v += ta;
    a = Vec3();
  }
};

std::ostream& operator<<(std::ostream &os, const Ball &c)
{
  return os << "position: " << c.p
          << "\nvelocity: " << c.v
          << "\nmass: " << c.m
          << "\nradius: " << c.r << std::endl;
}


//
// constants and current (global) state
// 
/// amount of bodies
const size_t N = 64;

/// relaxation coefficient (should fall into [0, 1])
const double k = 0.8;

/// central force constant
const double G = 0.5;

/// friction constant
const double F = 0.0;

/// actual bodies
Ball C[N];

/// last frame time moment
std::clock_t t;


//
// aux functions
//

/// returns a random number from [minv, maxv)
inline double random(double minv, double maxv)
{
  static const double anti_rand_max = 1.0 / double(RAND_MAX);
  return minv + (maxv - minv) * std::rand() * anti_rand_max;
}


/// generates randomly moving circles going to collide somewhere
void newBalls()
{
  std::cout << "\nGenerating new circles:\n";

  // target collision point
  const Vec3 cp(random(-300.0, 300.0), random(-300.0, 300.0), random(-300.0, 300.0));
  std::cout << "Collision point: " << cp << "\n";

  for (size_t i = 0; i < N; ++i)
  {
    Ball &b = C[i]; // setup C[i] fields
    b.p = Vec3(random(-300.0, 300.0), random(-300.0, 300.0), random(-300.0, 300.0)); // position
    b.v = 0.25 * (cp - b.p); // 4 seconds before it may reach the collision point
    b.r = random(4.0, 20.0); // radius
    b.m = random(0.25, 25.0) * b.r * b.r * b.r; // mass
    b.color.red   = static_cast<float>(random(0.0, 1.0));
    b.color.green = static_cast<float>(random(0.0, 1.0));
    b.color.blue  = static_cast<float>(random(0.0, 1.0));

    std::cout << i << ")\n" << b;
  }
}


/// gravity
inline void gravity(Ball &b1, Ball &b2)
{
  Vec3 r = b2.p - b1.p;
  const double dist = r.length();
  const double dist3 = dist * dist * dist;

  if (dist < b1.r + b2.r)
  {
    const double dist4 = dist3 * 0.5 + std::numeric_limits<double>::epsilon();
    r *= -G / dist4;
  }
  else
    r *= G / dist3;

  b1.a += b2.m * r;
  b2.a -= b1.m * r;
}


/// viscous friction force
inline void friction(Ball &b)
{
  b.a -= F * b.r * b.r / b.m * b.v;
}


//
// GLUT callbacks
//

/// redraw our scene
void repaint()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  // paint all balls
  for (size_t i = 0; i < N; ++i)
    C[i].paint();

  // finish and present our frame to the user
  glFinish();
  glutSwapBuffers();
}


/// calculate the next frame
void frameMove()
{
  static const double anti_clocks_per_sec = 1.0 / double(CLOCKS_PER_SEC);

  // check time
  std::clock_t t1 = std::clock();
  if(t1 != t)
  {
    double h = anti_clocks_per_sec * (t1 - t);
    t = t1;
    
    // check for balls intersection
    # pragma omp parallel for
    for (int i = 0; i < N; ++i)
    {
      Ball &a = C[i];

      for (size_t j = i + 1; j < N; ++j)
      {
        Ball &b = C[j];
        const Vec3 dp = a.p - b.p;
        const double dplen = dp.length();
        const double delta = dplen - (a.r + b.r);

        if (delta < 0.0 && (a.v - b.v) * dp < 0.0) // are overlapping and approaching?
        {
          const Vec3 r = 1.0 / dplen * dp;
          // collision detected -- calculate new velocities
          const Vec3 u(-r.y, r.x);
          // old velocities in (r, u) basis
          const double
            v1r = a.v * r,
            v2r = b.v * r,
            v1u = a.v * u,
            v2u = b.v * u,
          // new velocities
            v1n = k / (a.m + b.m) * ((a.m - b.m) * v1r + 2.0 * b.m * v2r),
            v2n = k / (a.m + b.m) * (2.0 * a.m * v1r + (b.m - a.m) * v2r);

          // calculate resulting velocities in original coordinates
          a.v = v1n * r + v1u * u;
          b.v = v2n * r + v2u * u;
        }
      }
    }


    h *= 0.25;
    for (int round = 0; round < 4; ++round)
    {

      // forces
      # pragma omp parallel for
      for (int i = 0; i < N; ++i)
      {
        Ball &b = C[i];
        //applyCentralForce(b);
      
        for (size_t j = i + 1; j < N; ++j)
          gravity(b, C[j]);
      }

      // move
      # pragma omp parallel for
      for (int i = 0; i < N; ++i)
        C[i].move(h);
    }

    // say GLUT that our frame has changed
    glutPostRedisplay();
  }
}


/// work out a keypress
void keyPressed(unsigned char key, int x, int y)
{
  switch (key)
  {
  case 27: // ESC ASCII code
  case 'q':
  case 'Q':
    std::cout << "Exiting\n";
    glutExit(); // quit

  case ' ':
  case '\r':
  case '\n':
    newBalls();
  
  default:
    ; // ignore it, suppress compiler's possible warning
  }
}


/// work out window resize to width of w pixels and height of h pixels
void reshape(int w, int h)
{
  // setup projection
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  if (w < h)
  {
    const double aspect = double(w) / double(h);
    glOrtho(-500.0 * aspect, 500.0 * aspect, -500.0, 500.0, -500.0, 500.0);
  }
  else
  {
    const double aspect = double(h) / double(w);
    glOrtho(-500.0, 500.0, -500.0 * aspect, 500.0 * aspect, -500.0, 500.0);
  }

  glMatrixMode(GL_MODELVIEW);

  // setup viewport
  glViewport(0, 0, w, h);
}


//
// main
//

int main(int argc, char **argv)
{
  // initialization
  glutInit(&argc, argv);
  std::srand(static_cast<unsigned>(std::time(nullptr)));

  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
  glutInitWindowPosition(100, 100);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Circle collision simulation");

  // setup some OpenGL state
  glClearColor(0.0, 0.0, 0.0, 1.0); // default one (black), just to show glClearColor
  glEnable(GL_COLOR_MATERIAL);
  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  
  // create a disk image
  glNewList(1, GL_COMPILE);
  glutSolidSphere(1.0, 16, 8);
  glEndList();
  
  // register callbacks
  glutDisplayFunc(repaint);
  glutKeyboardFunc(keyPressed);
  glutReshapeFunc(reshape);
  glutIdleFunc(frameMove);

  // run
  std::cout << "Starting...\n" << "k = " << k << "\n";
  newBalls();
  t = std::clock();
  glutMainLoop();
}


Create a new paste based on this one


Comments: