-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.cl
More file actions
75 lines (65 loc) · 2.21 KB
/
Simulation.cl
File metadata and controls
75 lines (65 loc) · 2.21 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
typedef struct {
float2 position;
float2 velocity;
} Particle;
__kernel void updateParticles(
__global Particle* particles,
const int numParticles,
const float deltaTime,
const float gravitationalConstant)
{
int gid = get_global_id(0);
if (gid >= numParticles) {
return;
}
Particle p = particles[gid];
// sum up gravity relative to each other particle
float xAcceleration = 0;
float yAcceleration = 0;
Particle q;
for(int i = 0; i < numParticles; i++) {
if(i != gid) {
q = particles[i];
float dx = fabs(q.position.x - p.position.x);
float dy = fabs(q.position.y - p.position.y);
float distance = sqrt(dx * dx + dy * dy);
float xDirection = (q.position.x - p.position.x) > 0 ? 1.0f : -1.0f;
float yDirection = (q.position.y - p.position.y) > 0 ? 1.0f : -1.0f;
if(distance > 0.001f) {
float totalAcceleration = gravitationalConstant / (distance * distance); // assume all masses are 1kg
// cap acceleration
if(totalAcceleration > 1.0f) {
totalAcceleration = 1.0f;
}
// needed to avoid NaN due to float point errors that cause dx/distance > 1 which is invalid for acos
float adjOverHypot = dx / distance;
if(adjOverHypot > 1.0f) {
adjOverHypot = 1.0f;
}
float angle1 = acos(adjOverHypot);
xAcceleration += xDirection * cos(angle1) * totalAcceleration;
yAcceleration += yDirection * sin(angle1) * totalAcceleration;
}
}
}
p.velocity.x += xAcceleration * deltaTime;
p.velocity.y += yAcceleration * deltaTime;
p.position += p.velocity * deltaTime;
if (p.position.x < 0.05f) {
p.position.x = 0.05f;
p.velocity.x = 0;
}
if(p.position.x > 0.95f) {
p.position.x = 0.95f;
p.velocity.x = 0;
}
if (p.position.y < 0.05f) {
p.position.y = 0.05f;
p.velocity.y = 0;
}
if(p.position.y > 0.95f) {
p.position.y = 0.95f;
p.velocity.y = 0;
}
particles[gid] = p;
}