summaryrefslogtreecommitdiffstats
path: root/balls.cpp
blob: e20e9548326b912b440a092b31a26794abecd684 (plain) (blame)
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
#include "balls.h"

#define VMAX_INIT 0.1
	/* max initial velocity [m/frame] */
#define RMIN 0.05
#define RMAX 0.10
	/* min/max radius [m] */
#define DENSITY 1500.0
	/* density of ball [kg/m^3] */
#define G (9.81/FPS/FPS)
	/* acceleration of gravity [m/(frame*frame)] */

enum {
	WIDTH = 800,
	HEIGHT = 600,

	KEY_QUIT = 'q',

	CIRCLE_SEGS = 32,

	FPS = 60,
	MS_PER_S = 1000,
	FRAME_TIME_MS = MS_PER_S / FPS,

	NBALLS_DEFAULT = 3,
};

void keyboard(unsigned char key, int x, int y);
void display(void);
void drawBackground(void);
void drawCircle(double radius, Point p, Color color);
void reshape(int w, int h);
vector<Ball *> makeBalls(int n);
vector<Point> noOverlapCircles(unsigned int n);
double mass(double radius);
double volume(double radius);
void animate(int v);
Color randColor(void);

const static Rectangle bounds = {{-1.5, -1.0}, {1.5, 1.0}};

vector<Ball *> balls;
vector<vector<Collision>> collisionPartition;

int
main(int argc, char *argv[]) {
	int nballs;

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(WIDTH, HEIGHT);
	glutCreateWindow("Balls");

	glutKeyboardFunc(keyboard);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);

	nballs = NBALLS_DEFAULT;
	if (argc > 1) {
		if (sscanf(argv[1], "%d", &nballs) != 1 || nballs < 1) {
			printf("usage: balls [number of balls]\n");
			return 1;
		}
	}

	balls = makeBalls(nballs);
	collisionPartition = partitionCollisions(balls);

	glutTimerFunc(FRAME_TIME_MS, animate, 0);

	glutMainLoop();

	return 1;
}

void
keyboard(unsigned char key, int x, int y) {
	if (key == KEY_QUIT)
		glutDestroyWindow(glutGetWindow());
}

void
display(void) {
	glClearColor(0, 0, 0, 1);
	glClear(GL_COLOR_BUFFER_BIT);
	drawBackground();
	/* TODO: parallel */
	for (const Ball *b : balls)
		drawCircle(b->r, b->p, b->color);

	glutSwapBuffers();
}

void
drawBackground(void) {
	glColor3f(1, 1, 1);
	glBegin(GL_QUADS);
		glVertex2f(bounds.min.x, bounds.min.y);
		glVertex2f(bounds.max.x, bounds.min.y);
		glVertex2f(bounds.max.x, bounds.max.y);
		glVertex2f(bounds.min.x, bounds.max.y);
	glEnd();
}

void
drawCircle(double radius, Point p, Color color) {
	int i;
	double theta, x, y;

	glColor3f(color.r, color.g, color.b);
	glBegin(GL_TRIANGLE_FAN);
		glVertex2f(p.x, p.y);
		for (i = 0; i <= CIRCLE_SEGS; i++) {
			theta = 2.0 * M_PI * i / CIRCLE_SEGS;
			x = radius * cosf(theta);
			y = radius * sinf(theta);
			glVertex2f(x+p.x, y+p.y);
		}
	glEnd();
}

void
reshape(int w, int h) {
	double ratio;

	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	ratio = (double) w / (double) h;
	if (ratio >= 1.0)
		glOrtho(-ratio, ratio, -1, 1, -1, 1);
	else
		glOrtho(-1, 1, -1.0/ratio, 1.0/ratio, -1, 1);
	glMatrixMode(GL_MODELVIEW);
}


vector<Ball *>
makeBalls(int n) {
	size_t i;

	vector<Point> positions = noOverlapCircles(n);
	
	vector<Ball *> balls(n);
	/* TODO: parallel */
	for(i = 0; i < balls.size(); i++) {
		cout << "Creating ball " << i << "\n";
		if ((balls[i] = (Ball *) malloc(sizeof(Ball))) == NULL) {
			cerr << "failed to allocate ball\n";
			while (i-- > 0)
				free(balls[i]);
			exit(1);
		}
		
		balls[i]->p = positions[i];
		balls[i]->v.x = randDouble(-VMAX_INIT, VMAX_INIT);
		balls[i]->v.y = randDouble(-VMAX_INIT, VMAX_INIT);
		balls[i]->r = randDouble(RMIN, RMAX);
		balls[i]->m = mass(balls[i]->r);
		balls[i]->color = randColor();
	};
	return balls;
}

/* return n-length vector of positions of non-overlapping circles with radius RMAX within the bounds */
vector<Point>
noOverlapCircles(unsigned int n) {
	vector<Point> ps(n);
	Rectangle r;
	unsigned int i, j;

	r = insetRect(bounds, RMAX);
	for (i = 0; i < n; i++) {
		cout << "Create non-overlapping circle " << i << "\n";
		ps[i] = randPtInRect(r);
		for (j = 0; j < i; j++) /* TODO: parallel reduce */
			if (isCollision(ps[j], RMAX, ps[i], RMAX))
				break;
		if (j < i) { /* overlapping */
			i--;
			continue;
		}
	}
	return ps;
}

/* mass [kg] of ball as function of radius [m] */
double
mass(double radius) {
	return volume(radius) * DENSITY;
}

/* volume [m^3] of ball as function of radius [m] */
double
volume(double radius) {
	return 4.0 * M_PI * radius*radius*radius / 3.0;
}

void
animate(int v) {
	/* Update position. */
	parallel_for(size_t(0), balls.size(), [] (size_t i) {
		balls[i]->v.y -=G;
		balls[i]->p = ptAddVec(balls[i]->p, balls[i]->v);
	});

	/* Collide with balls. */
	for (vector<Collision> cell : collisionPartition) {
		parallel_for(size_t(0), cell.size(), [cell] (size_t i) {
			collideBall(cell[i].b1, cell[i].b2);
		});
	}

	/* Collide with walls. */
	parallel_for(size_t(0), balls.size(), [] (size_t i) {
		collideWall(balls[i], bounds);
	});

	display();
	glutTimerFunc(FRAME_TIME_MS, animate, 0);
}

double
randDouble(double lo, double hi) {
	double r, diff;
	static int isInitialized = 0;

	if (!isInitialized) { /* first call */
		srand(time(0));
		isInitialized = 1;
	}

	r = (double) rand() / (double) RAND_MAX;
	diff = hi - lo;
	return lo + r*diff;
}

Color
randColor(void) {
	Color color;

	color.r = randDouble(0, 1);
	color.g = randDouble(0, 1);
	color.b = randDouble(0, 1);
	return color;
}