-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRacer.cpp
More file actions
191 lines (175 loc) · 5.65 KB
/
Racer.cpp
File metadata and controls
191 lines (175 loc) · 5.65 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
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
//// ----------------------------- Racer (Particle sys) ------------------------------
//rewrited from https://editor.soulmatelights.com/gallery/655-space-racer
//(c)stepko + kostyamat
//23.08.21
#define WIDTH LED_COLS
#define HEIGHT LED_ROWS
bool straightLineDir = 1;
float FADE_KOEF = 0.5;
void drawPixelXYF(float x, float y, CRGB color) {
if (x < 0 || y < 0 || x > ((float) LED_COLS - 1) || y > ((float) LED_ROWS - 1)) return;
uint8_t xx = (x - (int) x) * 255, yy = (y - (int) y) * 255, ix = 255 - xx, iy = 255 - yy;
// calculate the intensities for each affected pixel
#define WU_WEIGHT(a, b)((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
uint8_t wu[4] = {
WU_WEIGHT(ix, iy),
WU_WEIGHT(xx, iy),
WU_WEIGHT(ix, yy),
WU_WEIGHT(xx, yy)
};
// multiply the intensities by the colour, and saturating-add them to the pixels
for (uint8_t i = 0; i < 4; i++) {
int16_t xn = x + (i & 1), yn = y + ((i >> 1) & 1);
CRGB clr = leds[XY(xn, yn)];
clr.r = qadd8(clr.r, (color.r * wu[i]) >> 8);
clr.g = qadd8(clr.g, (color.g * wu[i]) >> 8);
clr.b = qadd8(clr.b, (color.b * wu[i]) >> 8);
leds[XY(xn, yn)] = clr;
}
}
void drawCircleF(float x0, float y0, float radius,
const CRGB & color, float step = 0.25) {
float a = radius, b = 0.;
float radiusError = step - a;
if (radius <= step * 2) {
drawPixelXYF(x0, y0, color);
return;
}
while (a >= b) {
drawPixelXYF(a + x0, b + y0, color);
drawPixelXYF(b + x0, a + y0, color);
drawPixelXYF(-a + x0, b + y0, color);
drawPixelXYF(-b + x0, a + y0, color);
drawPixelXYF(-a + x0, -b + y0, color);
drawPixelXYF(-b + x0, -a + y0, color);
drawPixelXYF(a + x0, -b + y0, color);
drawPixelXYF(b + x0, -a + y0, color);
b += step;
if (radiusError < 0.)
radiusError += 2. * b + step;
else {
a -= step;
radiusError += 2 * (b - a + step);
}
}
}
void drawLineF(float x1, float y1, float x2, float y2,
const CRGB & color) {
float deltaX = fabs(x2 - x1);
float deltaY = fabs(y2 - y1);
float error = deltaX - deltaY;
float signX = x1 < x2 ? 0.5 : -0.5;
float signY = y1 < y2 ? 0.5 : -0.5;
while (x1 != x2 || y1 != y2) {
if ((signX > 0. && x1 > x2 + signX) || (signX < 0. && x1 < x2 + signX))
break;
if ((signY > 0. && y1 > y2 + signY) || (signY < 0. && y1 < y2 + signY))
break;
drawPixelXYF(x1, y1, color);
float error2 = error;
if (error2 > -deltaY) {
error -= deltaY;
x1 += signX;
}
if (error2 < deltaX) {
error += deltaX;
y1 += signY;
}
}
}
void drawStarF(float x, float y, float biggy, float little, int16_t points, float dangle, CRGB color) {
float radius2 = 255.0 / points;
for (int i = 0; i < points; i++) {
drawLineF(x + ((little * (sin8(i * radius2 + radius2 / 2 - dangle) - 128.0)) / 128), y + ((little * (cos8(i * radius2 + radius2 / 2 - dangle) - 128.0)) / 128), x + ((biggy * (sin8(i * radius2 - dangle) - 128.0)) / 128), y + ((biggy * (cos8(i * radius2 - dangle) - 128.0)) / 128), color);
drawLineF(x + ((little * (sin8(i * radius2 - radius2 / 2 - dangle) - 128.0)) / 128), y + ((little * (cos8(i * radius2 - radius2 / 2 - dangle) - 128.0)) / 128), x + ((biggy * (sin8(i * radius2 - dangle) - 128.0)) / 128), y + ((biggy * (cos8(i * radius2 - dangle) - 128.0)) / 128), color);
}
}
float dist(float x1, float y1, float x2, float y2) {
return sqrtf(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
}
float RPos[2];
float RSpeed[2];
float RFade;
float DPos[2];
bool run = true;
byte hue;
float radius;
int angle;
byte starPoints;
bool loadingFlag = true;
void reg() {
RPos[0] = random() % WIDTH;
RPos[1] = random() % HEIGHT;
RSpeed[0] = 0;
RSpeed[1] = 0;
RFade = 255;
}
void phisics() {
float force[2] { DPos[0] - RPos[0], DPos[1] - RPos[1] };
float d = dist(force[0], force[1], force[0] * 2, force[1] * 2);
force[0] *= (1. / d);
force[1] *= (1. / d);
d = constrain(d, 5., HEIGHT * 2.);
float s = (7.5 * 10) / (d * d);
force[0] *= s;
force[1] *= s;
RSpeed[0] += force[0];
RSpeed[1] += force[1];
RFade -= (255. / (float)(((HEIGHT + WIDTH) / 2) * FADE_KOEF));
float sq = ((RSpeed[0] * RSpeed[0]) + (RSpeed[1] * RSpeed[1]));
if (sq > 2.25) {
sq = sqrtf(sq);
RSpeed[0] *= (1. / sq) * 1.5;
RSpeed[1] *= (1. / sq) * 1.5;
}
RPos[0] += RSpeed[0];
RPos[1] += RSpeed[1];
}
const float addRadius = (float) NUM_LEDS / 8000;
void render(CRGB Col) {
phisics();
if (RPos[1] < (HEIGHT - 1.) and RPos[1] >= 0.)
if (RPos[0] < (WIDTH - 1.) and RPos[0] >= 0.) {
CRGB color = Col;
drawPixelXYF(RPos[0], RPos[1], color);
}
}
void start() {
DPos[0] = random() % WIDTH;
DPos[1] = random() % HEIGHT;
reg();
}
void draw() {
if (loadingFlag) {
start();
loadingFlag = false;
}
fadeToBlackBy(leds, NUM_LEDS, 20);
render(CRGB(255, 255, 255));
if (dist(RPos[0], RPos[1], DPos[0], DPos[1]) <= .75) {
{ DPos[0] = random() % WIDTH;
DPos[1] = random() % HEIGHT; }
if (straightLineDir) {
float a = dist(DPos[0], DPos[1], RPos[0], RPos[1]);
RSpeed[0] *= (1. / a);
RSpeed[1] *= (1. / a);
}
starPoints = random(3, 7);
radius = 1;
hue = millis() >> 1;
}
radius += addRadius;
angle += radius;
switch (hue % 3) {
case 0:
drawCircleF(DPos[0], DPos[1], radius, ColorFromPalette(HeatColors_p, 255 - RFade)); // ðèñóåì êðóã
break;
case 1:
drawStarF(DPos[0], DPos[1], 1.3 * radius, radius, 4, angle, ColorFromPalette(HeatColors_p, 255 - RFade)); // ðèñóåì êâàäðàò
break;
case 2:
drawStarF(DPos[0], DPos[1], 2 * radius, radius, starPoints, angle, ColorFromPalette(HeatColors_p, 255 - RFade)); // ðèñóåì çâåçäó
break;
}
delay(16);
}