forked from jasoncoon/arduino-particle-sys
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParticle_Attractor.cpp
86 lines (71 loc) · 1.77 KB
/
Particle_Attractor.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
80
81
82
83
84
85
/*
* Copyright (C) 2013 Gilad Dayagi. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
/*
* Particle_Attractor.cpp - particle with an attractor
*/
#include "Particle_Attractor.h"
uint16_t Particle_Attractor::atx = 0;
uint16_t Particle_Attractor::aty = 0;
int16_t Particle_Attractor::atf = 4;
Particle_Attractor::Particle_Attractor(void)
{
isAlive = 0;
}
Particle_Attractor::Particle_Attractor(uint16_t atx, uint16_t aty) {
this->atx = atx;
this->aty = aty;
isAlive = 0;
}
void Particle_Attractor::update(ParticleSysConfig *g)
{
int16_t dx, dy, tempX, tempY, tempVx, tempVy;
int16_t acx, acy;
float mult;
//this particle does not age
dx = (int16_t)atx - x;
dy = (int16_t)aty - y;
mult = (float)atf/sqrt(dx*dx+dy*dy);
acx = (int16_t)(mult*dx);
acy = (int16_t)(mult*dy);
//apply acceleration
tempVx = vx+acx;
tempVy = vy+acy;
tempX = x + tempVx;
tempY = y + tempVy;
if (tempX < 0 || tempX > g->max_x) {
tempVx = 0;
}
if (tempY < 0 || tempY > g->max_y) {
tempVy = 0;
}
if (tempVx > 50 || tempVx < -50) {
vx = random(10)-5;
} else {
vx = tempVx;
}
if (tempVy > 50 || tempVy < -50) {
vy = random(10)-5;
} else {
vy = tempVy;
}
if (tempX > g->max_x) {
x = g->max_x;
} else if (tempX < 0) {
x = 0;
} else {
x = tempX;
}
if (tempY > g->max_y) {
y = g->max_y;
} else if (tempY < 0) {
y = 0;
} else {
y = tempY;
}
}