-
Notifications
You must be signed in to change notification settings - Fork 13
/
velocityspring.c
149 lines (105 loc) · 3.81 KB
/
velocityspring.c
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
#include "common.h"
//--------------------------------------
void velocity_spring_damper_exact(
float& x,
float& v,
float& xi,
float x_goal,
float v_goal,
float halflife,
float dt,
float apprehension = 2.0f,
float eps = 1e-5f)
{
float x_diff = ((x_goal - xi) > 0.0f ? 1.0f : -1.0f) * v_goal;
float t_goal_future = dt + apprehension * halflife;
float x_goal_future = fabs(x_goal - xi) > t_goal_future * v_goal ?
xi + x_diff * t_goal_future : x_goal;
simple_spring_damper_exact(x, v, x_goal_future, halflife, dt);
xi = fabs(x_goal - xi) > dt * v_goal ? xi + x_diff * dt : x_goal;
}
//--------------------------------------
enum
{
HISTORY_MAX = 256
};
float x_prev[HISTORY_MAX];
float v_prev[HISTORY_MAX];
float t_prev[HISTORY_MAX];
float xi_prev[HISTORY_MAX];
int main(void)
{
// Init Window
const int screenWidth = 640;
const int screenHeight = 360;
InitWindow(screenWidth, screenHeight, "raylib [springs] example - velocityspring");
// Init Variables
float t = 0.0;
float x = screenHeight / 2.0f;
float v = 0.0;
float g = x;
float goalOffset = 600;
float halflife = 0.1f;
float dt = 1.0 / 60.0f;
float timescale = 240.0f;
float goal_velocity = 100.0f;
float apprehension = 2.0f;
float xi = x;
SetTargetFPS(1.0f / dt);
for (int i = 0; i < HISTORY_MAX; i++)
{
x_prev[i] = x;
v_prev[i] = v;
t_prev[i] = t;
xi_prev[i] = x;
}
while (!WindowShouldClose())
{
// Shift History
for (int i = HISTORY_MAX - 1; i > 0; i--)
{
x_prev[i] = x_prev[i - 1];
v_prev[i] = v_prev[i - 1];
t_prev[i] = t_prev[i - 1];
xi_prev[i] = xi_prev[i - 1];
}
// Get Goal
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON))
{
g = GetMousePosition().y;
}
// Timed Spring
GuiSliderBar((Rectangle){ 100, 20, 120, 20 }, "halflife", TextFormat("%5.3f", halflife), &halflife, 0.0f, 1.0f);
GuiSliderBar((Rectangle){ 100, 45, 120, 20 }, "goal velocity", TextFormat("%5.3f", goal_velocity), &goal_velocity, 0.0f, 500.0f);
GuiSliderBar((Rectangle){ 100, 75, 120, 20 }, "apprehension", TextFormat("%5.3f", apprehension), &apprehension, 0.0f, 5.0f);
// Update Spring
SetTargetFPS(1.0f / dt);
t += dt;
velocity_spring_damper_exact(x, v, xi, g, goal_velocity, halflife, dt);
x_prev[0] = x;
v_prev[0] = v;
t_prev[0] = t;
xi_prev[0] = xi;
BeginDrawing();
ClearBackground(RAYWHITE);
DrawCircleV((Vector2){goalOffset, g}, 5, MAROON);
DrawCircleV((Vector2){goalOffset, x}, 5, DARKBLUE);
for (int i = 0; i < HISTORY_MAX - 1; i++)
{
Vector2 g_start = {goalOffset - (t - t_prev[i + 0]) * timescale, xi_prev[i + 0]};
Vector2 g_stop = {goalOffset - (t - t_prev[i + 1]) * timescale, xi_prev[i + 1]};
DrawLineV(g_start, g_stop, MAROON);
DrawCircleV(g_start, 2, MAROON);
}
for (int i = 0; i < HISTORY_MAX - 1; i++)
{
Vector2 x_start = {goalOffset - (t - t_prev[i + 0]) * timescale, x_prev[i + 0]};
Vector2 x_stop = {goalOffset - (t - t_prev[i + 1]) * timescale, x_prev[i + 1]};
DrawLineV(x_start, x_stop, BLUE);
DrawCircleV(x_start, 2, BLUE);
}
EndDrawing();
}
CloseWindow();
return 0;
}