-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path2DPhysics-analytic-springs.glsl
159 lines (126 loc) · 4.16 KB
/
2DPhysics-analytic-springs.glsl
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
/*
* "2D Physics (analytic springs)" by Alexander Alekseev aka TDM - 2023
* License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
* Contact: tdmaav@gmail.com
*/
const int NUM_OBJECTS = 64;
const float OBJECTS_GAP = 0.5 * (8.0 / float(NUM_OBJECTS));
const float BALL_SIZE = OBJECTS_GAP / 2.0;
/*
* Dynamics
*/
const float k = 100.0;
const float damp = 0.45;
vec2 springSystemModel(float t, vec2 b0, vec2 b1, vec2 b2) {
float rh = sqrt(3.*k);
float rp = sqrt(k);
vec2 CH = vec2(b0.x - (b1.x+b2.x)/2.,
(b0.y - (b1.y+b2.y)/2.) / rh);
vec2 CP = vec2((b1.x+b2.x)/2.,
(b1.y+b2.y)/(2.*rp));
vec2 exph = vec2(cos(rh*t), sin(rh*t));
vec2 expp = vec2(cos(rp*t), sin(rp*t));
float x = dot(CH, exph) +
dot(CP, expp);
float dx = dot(CH, rh*exph.yx*vec2(-1.0,1.0)) +
dot(CP, rp*expp.yx*vec2(-1.0,1.0));
return vec2(x,dx) * exp(-t * damp); // new state
}
/*
* body
*/
vec2 getBody(sampler2D buf, vec2 ires, int i) {
return texture(buf, (vec2(float(i),0.0) + 0.5) * ires).xy;
}
void initBody(int id, inout vec2 body) {
body = vec2(0.0);
}
/*
* store
*/
float isInside( vec2 p, vec2 c ) {
vec2 d = abs(p-0.5-c) - 0.5;
return -max(d.x,d.y);
}
void storeBody(in int id, in vec2 b, inout vec4 col, in vec2 uv) {
col = isInside(uv,vec2(float(id),0)) > 0.0 ? vec4(b,0.0,0.0) : col;
}
/**
* coords
*/
vec2 toScreenspace(int id, float y) {
float x = -float(NUM_OBJECTS-1) * 0.5 * OBJECTS_GAP +
float(id) * OBJECTS_GAP;
return vec2(x,y);
}
/*
* "2D Physics (analytic springs)" by Alexander Alekseev aka TDM - 2023
* License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
* Contact: tdmaav@gmail.com
*/
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
int id = int(fragCoord.x);
if(id >= NUM_OBJECTS) discard;
vec2 ires = 1.0 / iChannelResolution[0].xy;
// load
vec2 body = getBody(iChannel0, ires, id);
if(iFrame == 0) {
initBody(id, body); // init
} else {
float dt = min(iTimeDelta, MAX_DT);
// mouse impulse
if(iMouse.z > 0.5) {
vec2 mouse = iMouse.xy / iResolution.xy * 2.0 - 1.0;
mouse.x *= iResolution.x / iResolution.y;
vec2 pos_ss = toScreenspace(id,body.x);
vec2 dir = pos_ss.xy - mouse;
float t = abs(dir.x);
body.y += -k * 0.5 * dt * smoothstep(0.2,0.0,t);
}
// spring
vec2 bl = getBody(iChannel0, ires, id-1);
vec2 br = getBody(iChannel0, ires, id+1);
body = springSystemModel(dt, body, bl, br);
}
// store
storeBody(id, body, fragColor, fragCoord);
}
/*
* "2D Physics (analytic springs)" by Alexander Alekseev aka TDM - 2023
* License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
* Contact: tdmaav@gmail.com
*/
const vec3 COLOR = vec3(72, 143, 240) / 255.;
float line(vec2 p, vec2 p0, vec2 p1, float w) {
vec2 d = p1 - p0;
float t = clamp(dot(d,p-p0) / dot(d,d), 0.0,1.0);
vec2 proj = p0 + d * t;
float dist = length(p - proj);
return step(dist-w,0.0);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
vec2 uv = fragCoord.xy / iResolution.xy * 2.0 - 1.0;
uv.x *= iResolution.x / iResolution.y;
vec2 mouse = iMouse.xy / iResolution.xy * 2.0 - 1.0;
mouse.x *= iResolution.x / iResolution.y;
mouse.y = 0.0;
vec3 c = vec3(1.0);
vec2 ires = 1.0 / iChannelResolution[0].xy;
// objects
for(int i = 0; i < NUM_OBJECTS-1; i++) {
vec2 body = getBody(iChannel0, ires, i);
vec2 b1 = getBody(iChannel0, ires, i+1);
vec2 pos_ss0 = toScreenspace(i,body.x);
vec2 pos_ss1 = toScreenspace(i+1,b1.x);
float ba = line(uv,pos_ss0,pos_ss1,BALL_SIZE);
c = mix(c,COLOR,ba);
vec2 mid = (pos_ss0 + pos_ss1) * 0.5;
ba = line(uv,
mid,
mid+vec2(0.0,-2.0),
BALL_SIZE);
c = mix(c,COLOR,ba * 0.2 - min(uv.y,0.0)*0.005);
}
// final
fragColor = vec4(c,1.0);
}