-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
GravityHole.cpp
79 lines (61 loc) · 1.27 KB
/
GravityHole.cpp
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
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tri-Edge AI <triedgeai@gmail.com>
*/
#include "Particle.hpp"
#include "GravityHole.hpp"
#include <math.h>
#include <stdlib.h>
#define frand() ((float)rand() / (float)RAND_MAX)
GravityHole::GravityHole()
{
x = 0.0f;
y = 0.0f;
z = 0.0f;
vx = 0.0f;
vy = 0.0f;
vz = 0.0f;
ax = frand() * 30.0f - 15.0f;
ay = frand() * 30.0f - 15.0f;
az = frand() * 10.0f - 5.0f;
}
void
GravityHole::Run()
{
float dx = ax - x;
float dy = ay - y;
float dz = az - z;
float d = dx * dx + dy * dy + dz * dz;
vx += dx * 0.005f;
vy += dy * 0.005f;
vz += dz * 0.005f;
x += vx;
y += vy;
z += vz;
vx *= 0.95f;
vy *= 0.95f;
vz *= 0.95f;
if (dx * dx + dy * dy + dz * dz < 10.0f) {
ax = frand() * 30.0f - 15.0f;
ay = frand() * 30.0f - 15.0f;
az = frand() * 10.0f - 5.0f;
}
for (uint32 i = 0; i < Particle::list.size(); i++) {
dx = x - Particle::list[i]->x;
dy = y - Particle::list[i]->y;
dz = z - Particle::list[i]->z;
d = dx * dx + dy * dy + dz * dz;
Particle::list[i]->vx += dx / d * 0.5f;
Particle::list[i]->vy += dy / d * 0.5f;
Particle::list[i]->vz += dz / d * 0.5f;
Particle::list[i]->vr += 1.0f / d;
}
}
void
GravityHole::Draw()
{
//...
}