-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.cc
206 lines (186 loc) · 4.45 KB
/
Particle.cc
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
#include <iostream>
#include <limits>
#include "SDL/SDL.h"
#include <cstdlib>
#include <math.h>
#include "Particle.h"
Particle::
Particle()
{
rx = rand() / (RAND_MAX + 1.0);
ry = rand() / (RAND_MAX + 1.0);
vx = 0.01 * (rand() / (RAND_MAX + 1.0) - 0.5);
vy = 0.01 * (rand() / (RAND_MAX + 1.0) - 0.5);
radius = 0.005;
mass = 0.5;
R = rand() % 256;
G = rand() % 256;
B = rand() % 256;
return;
}
Particle::
Particle(double _rx, double _ry, double _vx, double _vy, double _radius, double _mass, unsigned char _r, unsigned char _g, unsigned char _b)
: rx(_rx), ry(_ry), vx(_vx), vy(_vy), radius(_radius), mass(_mass),
R(_r), G(_g), B(_b) {}
/**
* Updates position to reflect passage of time dt
*/
void
Particle::
move(double dt)
{
rx += vx * dt;
ry += vy * dt;
}
/**
* Update velocities upon collision between this particle and particle b
*/
void
Particle::
bounceOff(Particle& b)
{
double dx = b.rx - rx;
double dy = b.ry - ry;
double dvx = b.vx - vx;
double dvy = b.vy - vy;
double dvdr = dx*dvx + dy*dvy; // dv dot dr
double dist = radius + b.radius; // distance between particle centers at collison
// normal force F, and in x and y directions
double F = 2 * mass * b.mass * dvdr / ((mass + b.mass) * dist);
double fx = F * dx / dist;
double fy = F * dy / dist;
// update velocities according to normal force
vx += fx / mass;
vy += fy / mass;
b.vx -= fx / b.mass;
b.vy -= fy / b.mass;
// update collision counts
count++;
b.count++;
}
/**
* Update velocity of this particle upon collision with a horizontal wall
*/
void
Particle::
bounceOffHorizontalWall()
{
vy = -vy;
count++;
}
/**
* Update velocity of this particle upon collision with a vertical wall
*/
void
Particle::
bounceOffVerticalWall()
{
vx = -vx;
count++;
}
/**
* Return the number of collisions involving this particle
*/
int
Particle::
collisions() const
{
return count;
}
/**
* How long into future until collision between this particle a and b?
*/
double
Particle::
timeToHit(const Particle& b) const
{
if (this == &b) return std::numeric_limits<double>::infinity();
double dx = b.rx - rx;
double dy = b.ry - ry;
double dvx = b.vx - vx;
double dvy = b.vy - vy;
double dvdr = dx*dvx + dy*dvy;
if (dvdr > 0) return std::numeric_limits<double>::infinity();
double dvdv = dvx*dvx + dvy*dvy;
double drdr = dx*dx + dy*dy;
double sigma = radius + b.radius;
double d = (dvdr*dvdr) - dvdv * (drdr - sigma*sigma);
if (d < 0) return std::numeric_limits<double>::infinity();
return -(dvdr + sqrt(d)) / dvdv;
}
/**
* How long into future until this particle collides with a horizontal wall?
*/
double
Particle::
timeToHitHorizontalWall() const
{
if (vy > 0) return (1.0 - ry - radius) / vy;
else if (vy < 0) return (radius - ry) / vy;
else return std::numeric_limits<double>::infinity();
}
/**
* How long into future until this particle collides with a vertical wall?
*/
double
Particle::
timeToHitVerticalWall() const
{
if (vx > 0) return (1.0 - rx - radius) / vx;
else if (vx < 0) return (radius - rx) / vx;
else return std::numeric_limits<double>::infinity();
}
/**
* Draw particle to screen
*/
void
Particle::
draw(SDL_Surface* screen) const
{
if (radius < 0) throw particle_error("circle radius can't be negative");
double xs = screen->w * rx;
double ys = screen->h * ry;
double rs = screen->w * radius;
if (rs <= 1)
{
unsigned int *ptr = (unsigned int*)screen->pixels;
double lineoffset = ys * (screen->pitch / 4);
ptr[(int)(lineoffset + xs)] = SDL_MapRGB(screen->format,R,G,B);
}
else
drawcircle(screen, xs, ys, rs, SDL_MapRGB(screen->format,R,G,B));
}
/**
* Use fairly primitive to draw particle as filled circle
*/
void
Particle::
drawcircle(SDL_Surface* screen, int x, int y, int r, Uint32 c) const
{
int i, j;
for (i = 0; i < 2 * r; i++)
{
// vertical clipping: (top and bottom)
if ((y - r + i) >= 0 && (y - r + i) < screen->h)
{
int len = (int)(sqrt ((double)r * r - (r - i) * (r - i)) * 2.0);
int xofs = x - len / 2;
// left border
if (xofs < 0)
{
len += xofs;
xofs = 0;
}
// right border
if (xofs + len >= screen->w)
{
len -= (xofs + len) - screen->w;
}
int ofs = (y - r + i) * (screen->pitch / 4) + xofs;
// note that len may be 0 at this point,
// and no pixels get drawn!
for (j = 0; j < len; j++)
((unsigned int*)screen->pixels)[ofs + j] = c;
}
}
}