forked from OneLoneCoder/Javidx9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneLoneCoder_Splines1.cpp
236 lines (193 loc) · 5.76 KB
/
OneLoneCoder_Splines1.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
/*
OneLoneCoder.com - Splines Part 1
"Bendy Wavy Curly" - @Javidx9
License
~~~~~~~
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
~~~~~~~~~~
Curvy things are always better. Splines are a nice way to approximate
curves and loops for games. This video is the first of two parts
demonstrating how Catmull-Rom splines can be implemented.
Use Z + X to select a point and move it with the arrow keys
Use A + S to move the agent around the spline loop
Author
~~~~~~
Twitter: @javidx9
Blog: www.onelonecoder.com
Video:
~~~~~~
https://youtu.be/9_aJGUTePYo
Last Updated: 06/08/2017
*/
#include <iostream>
#include <string>
using namespace std;
#include "olcConsoleGameEngine.h"
struct sPoint2D
{
float x;
float y;
};
struct sSpline
{
vector<sPoint2D> points;
sPoint2D GetSplinePoint(float t, bool bLooped = false)
{
int p0, p1, p2, p3;
if (!bLooped)
{
p1 = (int)t + 1;
p2 = p1 + 1;
p3 = p2 + 1;
p0 = p1 - 1;
}
else
{
p1 = (int)t;
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, bool bLooped = false)
{
int p0, p1, p2, p3;
if (!bLooped)
{
p1 = (int)t + 1;
p2 = p1 + 1;
p3 = p2 + 1;
p0 = p1 - 1;
}
else
{
p1 = (int)t;
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;
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 };
}
};
class OneLoneCoder_Splines : public olcConsoleGameEngine
{
public:
OneLoneCoder_Splines()
{
m_sAppName = L"Splines";
}
private:
sSpline path;
int nSelectedPoint = 0;
float fMarker = 0.0f;
protected:
// Called by olcConsoleGameEngine
virtual bool OnUserCreate()
{
//path.points = { { 10, 41 },{ 40, 41 },{ 70, 41 },{ 100, 41 } };
path.points = { { 10, 41 },{ 20, 41 },{ 30, 41 },{ 40, 41 },{ 50, 41 },{ 60, 41 },{ 70, 41 },{ 80, 41 },{ 90, 41 },{ 100, 41 } };
return true;
}
// Called by olcConsoleGameEngine
virtual bool OnUserUpdate(float fElapsedTime)
{
// Clear Screen
Fill(0, 0, ScreenWidth(), ScreenHeight(), L' ');
// Handle input
if (m_keys[L'X'].bReleased)
{
nSelectedPoint++;
if (nSelectedPoint >= path.points.size())
nSelectedPoint = 0;
}
if (m_keys[L'Z'].bReleased)
{
nSelectedPoint--;
if (nSelectedPoint < 0)
nSelectedPoint = path.points.size() - 1;
}
if (m_keys[VK_LEFT].bHeld)
path.points[nSelectedPoint].x -= 30.0f * fElapsedTime;
if (m_keys[VK_RIGHT].bHeld)
path.points[nSelectedPoint].x += 30.0f * fElapsedTime;
if (m_keys[VK_UP].bHeld)
path.points[nSelectedPoint].y -= 30.0f * fElapsedTime;
if (m_keys[VK_DOWN].bHeld)
path.points[nSelectedPoint].y += 30.0f * fElapsedTime;
if (m_keys[L'A'].bHeld)
fMarker -= 5.0f * fElapsedTime;
if (m_keys[L'S'].bHeld)
fMarker += 5.0f * fElapsedTime;
if (fMarker >= (float)path.points.size())
fMarker -= (float)path.points.size();
if (fMarker < 0.0f)
fMarker += (float)path.points.size();
// Draw Spline
for (float t = 0; t < (float)path.points.size(); t += 0.005f)
{
sPoint2D pos = path.GetSplinePoint(t, true);
Draw(pos.x, pos.y);
}
// Draw Control Points
for (int i = 0; i < path.points.size(); i++)
{
Fill(path.points[i].x - 1, path.points[i].y - 1, path.points[i].x + 2, path.points[i].y + 2, PIXEL_SOLID, FG_RED);
DrawString(path.points[i].x, path.points[i].y, to_wstring(i));
}
// Highlight control point
Fill(path.points[nSelectedPoint].x - 1, path.points[nSelectedPoint].y - 1, path.points[nSelectedPoint].x + 2, path.points[nSelectedPoint].y + 2, PIXEL_SOLID, FG_YELLOW);
DrawString(path.points[nSelectedPoint].x, path.points[nSelectedPoint].y, to_wstring(nSelectedPoint));
// Draw agent to demonstrate gradient
sPoint2D p1 = path.GetSplinePoint(fMarker, true);
sPoint2D g1 = path.GetSplineGradient(fMarker, true);
float r = atan2(-g1.y, g1.x);
DrawLine(5.0f * sin(r) + p1.x, 5.0f * cos(r) + p1.y, -5.0f * sin(r) + p1.x, -5.0f * cos(r) + p1.y, PIXEL_SOLID, FG_BLUE);
return true;
}
};
int main()
{
OneLoneCoder_Splines demo;
demo.ConstructConsole(160, 80, 10, 10);
demo.Start();
return 0;
}