forked from OneLoneCoder/Javidx9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneLoneCoder_RacingLines.cpp
432 lines (353 loc) · 11.9 KB
/
OneLoneCoder_RacingLines.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
OneLoneCoder.com - Programming Racing Lines
"Brake! Brake! Hard Left! " - @Javidx9
License
~~~~~~~
One Lone Coder Console Game Engine Copyright (C) 2018 Javidx9
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; See license for details.
Original works located at:
https://www.github.com/onelonecoder
https://www.onelonecoder.com
https://www.youtube.com/javidx9
GNU GPLv3
https://github.com/OneLoneCoder/videos/blob/master/LICENSE
From Javidx9 :)
~~~~~~~~~~~~~~~
Hello! Ultimately I don't care what you use this for. It's intended to be
educational, and perhaps to the oddly minded - a little bit of fun.
Please hack this, change it and use it in any way you see fit. You acknowledge
that I am not responsible for anything bad that happens as a result of
your actions. However this code is protected by GNU GPLv3, see the license in the
github repo. This means you must attribute me if you use it. You can view this
license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
Cheers!
Background
~~~~~~~~~~
Algorithmically generating a racing line is quite tricky. This simple framework
allows me to explore different methods. Use mouse to drag points, and A & S keys
to change the number of iterations.
Author
~~~~~~
Twitter: @javidx9
Blog: http://www.onelonecoder.com
Discord: https://discord.gg/WhwHUMV
Video:
~~~~~~
https://youtu.be/FlieT66N9OM
Last Updated: 27/05/2018
*/
#include <iostream>
#include <string>
using namespace std;
#include "olcConsoleGameEngine.h"
// See Programming Splines! Videos
struct sPoint2D
{
float x;
float y;
float length;
};
struct sSpline
{
vector<sPoint2D> points;
float fTotalSplineLength = 0.0f;
bool bIsLooped = true;
sPoint2D GetSplinePoint(float t)
{
int p0, p1, p2, p3;
if (!bIsLooped)
{
p1 = (int)t + 1;
p2 = p1 + 1;
p3 = p2 + 1;
p0 = p1 - 1;
}
else
{
p1 = ((int)t) % points.size();
p2 = (p1 + 1) % points.size();
p3 = (p2 + 1) % points.size();
p0 = p1 >= 1 ? p1 - 1 : points.size() - 1;
}
t = t - (int)t;
float tt = t * t;
float ttt = tt * t;
float q1 = -ttt + 2.0f*tt - t;
float q2 = 3.0f*ttt - 5.0f*tt + 2.0f;
float q3 = -3.0f*ttt + 4.0f*tt + t;
float q4 = ttt - tt;
float tx = 0.5f * (points[p0].x * q1 + points[p1].x * q2 + points[p2].x * q3 + points[p3].x * q4);
float ty = 0.5f * (points[p0].y * q1 + points[p1].y * q2 + points[p2].y * q3 + points[p3].y * q4);
return{ tx, ty };
}
sPoint2D GetSplineGradient(float t)
{
int p0, p1, p2, p3;
if (!bIsLooped)
{
p1 = (int)t + 1;
p2 = p1 + 1;
p3 = p2 + 1;
p0 = p1 - 1;
}
else
{
p1 = ((int)t) % points.size();
p2 = (p1 + 1) % points.size();
p3 = (p2 + 1) % points.size();
p0 = p1 >= 1 ? p1 - 1 : points.size() - 1;
}
t = t - (int)t;
float tt = t * t;
float ttt = tt * t;
float q1 = -3.0f * tt + 4.0f*t - 1.0f;
float q2 = 9.0f*tt - 10.0f*t;
float q3 = -9.0f*tt + 8.0f*t + 1.0f;
float q4 = 3.0f*tt - 2.0f*t;
float tx = 0.5f * (points[p0].x * q1 + points[p1].x * q2 + points[p2].x * q3 + points[p3].x * q4);
float ty = 0.5f * (points[p0].y * q1 + points[p1].y * q2 + points[p2].y * q3 + points[p3].y * q4);
return{ tx, ty };
}
float CalculateSegmentLength(int node)
{
float fLength = 0.0f;
float fStepSize = 0.1;
sPoint2D old_point, new_point;
old_point = GetSplinePoint((float)node);
for (float t = 0; t < 1.0f; t += fStepSize)
{
new_point = GetSplinePoint((float)node + t);
fLength += sqrtf((new_point.x - old_point.x)*(new_point.x - old_point.x)
+ (new_point.y - old_point.y)*(new_point.y - old_point.y));
old_point = new_point;
}
return fLength;
}
float GetNormalisedOffset(float p)
{
// Which node is the base?
int i = 0;
while (p > points[i].length)
{
p -= points[i].length;
i++;
}
// The fractional is the offset
return (float)i + (p / points[i].length);
}
void UpdateSplineProperties()
{
// Use to cache local spline lengths and overall spline length
fTotalSplineLength = 0.0f;
if (bIsLooped)
{
// Each node has a succeeding length
for (int i = 0; i < points.size(); i++)
{
points[i].length = CalculateSegmentLength(i);
fTotalSplineLength += points[i].length;
}
}
else
{
for (int i = 1; i < points.size() - 2; i++)
{
points[i].length = CalculateSegmentLength(i);
fTotalSplineLength += points[i].length;
}
}
}
void DrawSelf(olcConsoleGameEngine* gfx, float ox, float oy, wchar_t c = 0x2588, short col = 0x000F)
{
if (bIsLooped)
{
for (float t = 0; t < (float)points.size() - 0; t += 0.005f)
{
sPoint2D pos = GetSplinePoint(t);
gfx->Draw(pos.x, pos.y, c, col);
}
}
else // Not Looped
{
for (float t = 0; t < (float)points.size() - 3; t += 0.005f)
{
sPoint2D pos = GetSplinePoint(t);
gfx->Draw(pos.x, pos.y, c, col);
}
}
}
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
class OneLoneCoder_RacingLine : public olcConsoleGameEngine
{
public:
OneLoneCoder_RacingLine()
{
m_sAppName = L"Racing Line";
}
private:
sSpline path, trackLeft, trackRight, racingLine; // Various splines
int nNodes = 20; // Number of nodes in spline
float fDisplacement[20]; // Displacement along spline node normal
int nIterations = 1;
float fMarker = 1.0f;
int nSelectedNode = -1;
vector<pair<float, float>> vecModelCar;
protected:
// Called by olcConsoleGameEngine
virtual bool OnUserCreate()
{
for (int i = 0; i < nNodes; i++)
{
//path.points.push_back(
// { 30.0f * sinf((float)i / (float)nNodes * 3.14159f * 2.0f) + ScreenWidth() / 2,
// 30.0f * cosf((float)i / (float)nNodes * 3.14159f * 2.0f) + ScreenHeight() / 2 });
// Could use allocation functions for thes now, but just size via
// append
trackLeft.points.push_back({ 0.0f, 0.0f });
trackRight.points.push_back({ 0.0f, 0.0f });
racingLine.points.push_back({ 0.0f, 0.0f });
}
// A hand crafted track
path.points = { { 81.8f, 196.0f }, { 108.0f,210.0f }, { 152.0f,216.0f },
{ 182.0f,185.6f }, { 190.0f,159.0f }, { 198.0f,122.0f }, { 226.0f,93.0f },
{ 224.0f,41.0f }, { 204.0f,15.0f }, { 158.0f,24.0f }, { 146.0f,52.0f },
{ 157.0f,93.0f }, { 124.0f,129.0f }, { 83.0f,104.0f }, { 77.0f,62.0f },
{ 40.0f,57.0f }, { 21.0f,83.0f }, { 33.0f,145.0f }, { 30.0f,198.0f },
{ 48.0f,210.0f } };
vecModelCar = { { 2,0 },{ 0,-1 },{ 0,1 } };
path.UpdateSplineProperties();
return true;
}
// Called by olcConsoleGameEngine
virtual bool OnUserUpdate(float fElapsedTime)
{
// Clear Screen
Fill(0, 0, ScreenWidth(), ScreenHeight(), PIXEL_SOLID, FG_DARK_GREEN);
// Handle iteration count
if (m_keys[L'A'].bHeld) nIterations++;
if (m_keys[L'S'].bHeld) nIterations--;
if (nIterations < 0) nIterations = 0;
// Check if node is selected with mouse
if (GetMouse(0).bPressed)
{
for (int i = 0; i < path.points.size(); i++)
{
float d = sqrtf(powf(path.points[i].x - GetMouseX(), 2) + powf(path.points[i].y - GetMouseY(), 2));
if (d < 5.0f)
{
nSelectedNode = i;
break;
}
}
}
if (GetMouse(0).bReleased)
nSelectedNode = -1;
// Move selected node
if (GetMouse(0).bHeld && nSelectedNode >= 0)
{
path.points[nSelectedNode].x = GetMouseX();
path.points[nSelectedNode].y = GetMouseY();
path.UpdateSplineProperties();
}
// Move car around racing line
fMarker += 2.0f * fElapsedTime;
if (fMarker >= (float)racingLine.fTotalSplineLength)
fMarker -= (float)racingLine.fTotalSplineLength;
// Calculate track boundary points
float fTrackWidth = 10.0f;
for (int i = 0; i < path.points.size(); i++)
{
sPoint2D p1 = path.GetSplinePoint(i);
sPoint2D g1 = path.GetSplineGradient(i);
float glen = sqrtf(g1.x*g1.x + g1.y*g1.y);
trackLeft.points[i].x = p1.x + fTrackWidth * (-g1.y / glen);
trackLeft.points[i].y = p1.y + fTrackWidth * ( g1.x / glen);
trackRight.points[i].x = p1.x - fTrackWidth * (-g1.y / glen);
trackRight.points[i].y = p1.y - fTrackWidth * (g1.x / glen);
}
// Draw Track
float fRes = 0.2f;
for (float t = 0.0f; t < path.points.size(); t += fRes)
{
sPoint2D pl1 = trackLeft.GetSplinePoint(t);
sPoint2D pr1 = trackRight.GetSplinePoint(t);
sPoint2D pl2 = trackLeft.GetSplinePoint(t + fRes);
sPoint2D pr2 = trackRight.GetSplinePoint(t + fRes);
FillTriangle(pl1.x, pl1.y, pr1.x, pr1.y, pr2.x, pr2.y, PIXEL_SOLID, FG_GREY);
FillTriangle(pl1.x, pl1.y, pl2.x, pl2.y, pr2.x, pr2.y, PIXEL_SOLID, FG_GREY);
}
// Reset racing line
for (int i = 0; i < racingLine.points.size(); i++)
{
racingLine.points[i] = path.points[i];
fDisplacement[i] = 0;
}
racingLine.UpdateSplineProperties();
for (int n = 0; n < nIterations; n++)
{
for (int i = 0; i < racingLine.points.size(); i++)
{
// Get locations of neighbour nodes
sPoint2D pointRight = racingLine.points[(i + 1) % racingLine.points.size()];
sPoint2D pointLeft = racingLine.points[(i + racingLine.points.size() - 1) % racingLine.points.size()];
sPoint2D pointMiddle = racingLine.points[i];
// Create vectors to neighbours
sPoint2D vectorLeft = { pointLeft.x - pointMiddle.x, pointLeft.y - pointMiddle.y };
sPoint2D vectorRight = { pointRight.x - pointMiddle.x, pointRight.y - pointMiddle.y };
// Normalise neighbours
float lengthLeft = sqrtf(vectorLeft.x*vectorLeft.x + vectorLeft.y*vectorLeft.y);
sPoint2D leftn = { vectorLeft.x / lengthLeft, vectorLeft.y / lengthLeft };
float lengthRight = sqrtf(vectorRight.x*vectorRight.x + vectorRight.y*vectorRight.y);
sPoint2D rightn = { vectorRight.x / lengthRight, vectorRight.y / lengthRight };
// Add together to create bisector vector
sPoint2D vectorSum = { rightn.x + leftn.x, rightn.y + leftn.y };
float len = sqrtf(vectorSum.x*vectorSum.x + vectorSum.y*vectorSum.y);
vectorSum.x /= len; vectorSum.y /= len;
// Get point gradient and normalise
sPoint2D g = path.GetSplineGradient(i);
float glen = sqrtf(g.x*g.x + g.y*g.y);
g.x /= glen; g.y /= glen;
// Project required correction onto point tangent to give displacment
float dp = -g.y*vectorSum.x + g.x * vectorSum.y;
// Shortest path
fDisplacement[i] += (dp * 0.3f);
// Curvature
//fDisplacement[(i + 1) % racingLine.points.size()] += dp * -0.2f;
//fDisplacement[(i - 1 + racingLine.points.size()) % racingLine.points.size()] += dp * -0.2f;
}
// Clamp displaced points to track width
for (int i = 0; i < racingLine.points.size(); i++)
{
if (fDisplacement[i] >= fTrackWidth) fDisplacement[i] = fTrackWidth;
if (fDisplacement[i] <= -fTrackWidth) fDisplacement[i] = -fTrackWidth;
sPoint2D g = path.GetSplineGradient(i);
float glen = sqrtf(g.x*g.x + g.y*g.y);
g.x /= glen; g.y /= glen;
racingLine.points[i].x = path.points[i].x + -g.y * fDisplacement[i];
racingLine.points[i].y = path.points[i].y + g.x * fDisplacement[i];
}
}
path.DrawSelf(this, 0, 0);
//trackLeft.DrawSelf(this, 0, 0);
//trackRight.DrawSelf(this, 0, 0);
racingLine.UpdateSplineProperties();
racingLine.DrawSelf(this, 0, 0, PIXEL_SOLID, FG_BLUE);
for (auto i : path.points)
Fill(i.x - 1, i.y - 1, i.x + 2, i.y + 2, PIXEL_SOLID, FG_RED);
sPoint2D car_p = racingLine.GetSplinePoint(fMarker);
sPoint2D car_g = racingLine.GetSplineGradient(fMarker);
DrawWireFrameModel(vecModelCar, car_p.x, car_p.y, atan2f(car_g.y, car_g.x), 4.0f, FG_BLACK);
return true;
}
};
int main()
{
OneLoneCoder_RacingLine demo;
demo.ConstructConsole(256, 240, 4, 4);
demo.Start();
return 0;
}