-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.v
282 lines (229 loc) · 5.69 KB
/
objects.v
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
import math
import term.ui as tui
import term
struct ProceduralCircle {
mut:
position Vec
radius f64
samples int
}
fn (c ProceduralCircle) sample_point(index int)Vec{
assert index < c.samples
angle := f64(index) / f64(c.samples) * 2.0 * math.pi
return Vec{
c.radius * math.cos(angle),
c.radius * math.sin(angle)
} + c.position
}
fn (c ProceduralCircle) render(a voidptr) {
for i in 0..c.samples {
put_pixel(c.sample_point(i), a)
}
}
fn (c ProceduralCircle) make_real( mass f64,stiffness f64, damping f64 ) SoftBodyCircle {
mut sbcircle := SoftBodyCircle{
position: c.position,
vertices: []Vertex{len: c.samples, cap: c.samples},
springs: []Spring{len: c.samples, cap: c.samples},
}
mass_per := mass / f64(c.samples)
for i in 0..c.samples {
sbcircle.vertices[i] = Vertex{
position: c.sample_point(i),
mass: mass_per,
}
}
space := sbcircle.vertices[0].position.distance(sbcircle.vertices[1].position)
//? SHOULD be proportional to circumference
for i in 0..c.samples {
sbcircle.springs[i] = Spring{
a: &sbcircle.vertices[i],
b: &sbcircle.vertices[(i + 1) % c.samples],
length: space,
stiffness: stiffness,
damping: damping,
}
}
return sbcircle
}
struct SoftBodyCircle {
radius f64
mut:
position Vec
vertices[] Vertex
springs[] Spring
}
// bresenham's line algorithm taken from term.ui but modified to work with Vec + board
fn draw_line(vec1 Vec, vec2 Vec, a voidptr) {
mut ctx := &App(a)
ssx, ssy := term.get_terminal_size()
x := vec1.int().x
y := vec1.int().y
x2 := vec2.int().x
y2 := vec2.int().y
min_x, min_y := if x < x2 { x } else { x2 }, if y < y2 { y } else { y2 }
max_x, _ := if x > x2 { x } else { x2 }, if y > y2 { y } else { y2 }
// Draw the various points with Bresenham's line algorithm:
mut x0, x1 := x, x2
mut y0, y1 := y, y2
sx := if x0 < x1 { 1 } else { -1 }
sy := if y0 < y1 { 1 } else { -1 }
dx := if x0 < x1 { x1 - x0 } else { x0 - x1 }
dy := if y0 < y1 { y0 - y1 } else { y1 - y0 } // reversed
mut err := dx + dy
for {
// res << Segment{ x0, y0 }
new := IVec{x0, y0}.screen(a)
if new.x+1 >= ssx || new.y+1 >= ssy {
return
}
if new.x >= ssx || new.y >= ssy || new.x < 0.0 || new.y < 0.0 {
return
}
ctx.tui.draw_text(new.x,new.y, pchar)
ctx.tui.draw_text(new.x+1,new.y, pchar)
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 >= dy {
err += dy
x0 += sx
}
if e2 <= dx {
err += dx
y0 += sy
}
}
}
fn draw_line_unobstruct(vec1 Vec, vec2 Vec, a voidptr) {
mut ctx := &App(a)
ssx, ssy := term.get_terminal_size()
x := vec1.int().x
y := vec1.int().y
x2 := vec2.int().x
y2 := vec2.int().y
min_x, min_y := if x < x2 { x } else { x2 }, if y < y2 { y } else { y2 }
max_x, _ := if x > x2 { x } else { x2 }, if y > y2 { y } else { y2 }
// Draw the various points with Bresenham's line algorithm:
mut x0, x1 := x, x2
mut y0, y1 := y, y2
sx := if x0 < x1 { 1 } else { -1 }
sy := if y0 < y1 { 1 } else { -1 }
dx := if x0 < x1 { x1 - x0 } else { x0 - x1 }
dy := if y0 < y1 { y0 - y1 } else { y1 - y0 } // reversed
mut err := dx + dy
for {
// res << Segment{ x0, y0 }
new := IVec{x0, y0}.screen(a)
if !(new.x >= ssx || new.y >= ssy || new.x < 0.0 || new.y < 0.0) {
ctx.tui.draw_text(new.x,new.y, pchar)
}
if !(new.x+1 >= ssx || new.y+1 >= ssy) {
ctx.tui.draw_text(new.x+1,new.y, pchar)
}
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 >= dy {
err += dy
x0 += sx
}
if e2 <= dx {
err += dx
y0 += sy
}
}
}
struct Vertex {
mut:
position Vec = Vec{0, 0}
velocity Vec
force Vec
mass f64 = 1.0
fixed bool
calculated bool
integrated bool
//? juuuust making sure for debugging
}
fn (v Vertex) render(a voidptr) {
put_pixel(v.position, a)
}
fn (v Vertex) info(a voidptr) {
mut app := &App(a)
text := "mass: ${v.mass}, force: ${math.round(v.force.length())}"
point := (v.position + vec(2,0)).int().screen(a)
app.tui.draw_text(point.x, point.y, term.magenta(text))
}
struct Spring {
mut:
a &Vertex
b &Vertex
length f64
stiffness f64
damping f64
// used for rendering spring forces
factor f64
}
//todo render as points + render as lines
//todo when rendering as lines draw red or green based on spring
fn (s Spring) render_points(a voidptr) {
s.a.render(a)
s.b.render(a)
}
fn (s Spring) info(a voidptr) {
mut app := &App(a)
text := "rest: ${s.length},stiffness: ${s.stiffness}"
point := (s.a.position.midpoint(s.b.position) - vec(text.len/2 + 2,0)).int().screen(a)
app.tui.draw_text(point.x, point.y, term.yellow(text))
}
fn (s Spring) render(a voidptr) {
mut app := &App(a)
app.tui.set_bg_color(lerp_colour(s.factor,tui.Color{
0,
200,
0
},tui.Color{
255,
0,
0
}))
draw_line(s.a.position, s.b.position, a)
app.tui.reset_bg_color()
}
struct Line {
mut:
position Vec
direction Vec
facing bool
//? Unit Vector!
}
//? infinitely long line
fn (l Line) get_closest(vec Vec)Vec {
v := vec - l.position
d := v.dot(l.direction)
return l.position + l.direction * svec(d)
}
fn (l Line) is_passing(vec Vec)bool {
dot := l.get_normal().dot((vec - l.position).normalize())
return (dot > 0.0) != l.facing
}
fn (l Line) get_normal()Vec {
mut nrm_direction := Vec{l.direction.y, -l.direction.x} * svec(0.5)
if l.facing {
nrm_direction = nrm_direction * svec(-1)
}
return nrm_direction
}
fn (l Line) render( a voidptr ) {
mut app := &App(a)
nrm_direction := l.get_normal()
point1 := l.position - l.direction * svec(30)
point2 := l.position + l.direction * svec(30)
app.tui.set_bg_color(r: 255, g: 255, b: 255)
draw_line_unobstruct(point1, point2, a)
app.tui.set_bg_color(r: 100, g: 100, b: 100)
draw_line_unobstruct(point1 + nrm_direction, point2 + nrm_direction, a)
app.tui.reset_bg_color()
}