/
hamstrograph.bmx
469 lines (394 loc) · 9.29 KB
/
hamstrograph.bmx
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
Const gravity#=.001
Global grandcircles:tlist=New tlist
Type grandcircle
Field radius#
Field an#
Field van#
Field weights:tlist
Field cx# , cy#
Field upramps:tlist
Field downramps:tlist
Method New()
grandcircles.addlast Self
weights = New tlist
upramps = New tlist
downramps=New tlist
End Method
Function create:grandcircle(radius#)
gc:grandcircle = New grandcircle
gc.radius = radius
Return gc
End Function
Method update()
an:+ van
van:* .99
For w:weight = EachIn weights
f# = w.mass * gravity * Sin(w.an - an+180)
van:+f
Next
For h:hamster = EachIn hamsters
If h.mycircle=Self
f# = h.mass * gravity * Sin(h.an - an) * h.pradius/radius
van:+ f
EndIf
Next
End Method
Method Draw()
cx# = gwidth / 2
cy# = gheight/2
SetColor 0 , 0 , 0
SetAlpha .2
DrawOval cx - radius , cy - radius , 2 * radius , 2 * radius
For spokean = 0 To 360 Step 60
ex# = cx + radius * Cos(spokean + an)
ey# = cy + radius * Sin(spokean + an)
DrawLine ex , ey , cx , cy
Next
SetColor 255,255,255
SetAlpha 1
For w:weight = EachIn weights
x# = cx + radius*Sin(w.an - an)
y# = cy + radius * Cos(w.an - an)
w.draw(x,y)
Next
Rem
x# = cx
y# = cy + radius
For a# = 0 To 180
r#=radius*(.25*Cos(a)+.75)
ex# = cx + r * Sin(a)
ey# = cy + r * Cos(a)
DrawLine x , y , ex , ey
x = ex
y = ey
Next
EndRem
End Method
End Type
Global ramps:tlist=New tlist
Type ramp
Field mycircle:grandcircle
Field endcircle:grandcircle
Field an# , anwide#
Field rdiff#
Method New()
ramps.addlast Self
End Method
Function create:ramp(gc:grandcircle , egc:grandcircle , an1# , an2#)
r:ramp = New ramp
r.mycircle = gc
gc.upramps.addlast r
r.endcircle = egc
egc.downramps.addlast r
r.an = an1
r.anwide = an2
r.rdiff# = r.mycircle.radius - r.endcircle.radius
Return r
End Function
Method Draw()
SetAlpha .3
For h:hamster = EachIn hamsters
If h.mycircle = mycircle Or h.mycircle=endcircle
SetAlpha 1
EndIf
Next
x# = mycircle.cx + mycircle.radius*Sin(an-mycircle.an)
y# = mycircle.cy + mycircle.radius*Cos(an-mycircle.an)
For t# = 0 To 1 Step .01
r#=radius(t)
ex# = mycircle.cx + r * Sin(an+t*anwide-mycircle.an)
ey# = mycircle.cy + r * Cos(an+t*anwide-mycircle.an)
DrawLine x , y , ex , ey
x = ex
y = ey
Next
End Method
Method radius#(t#)
r# = endcircle.radius + rdiff * .5 * (Cos(t * 180) + 1)
Return r
End Method
Method endan#()
Return an + anwide - mycircle.an + endcircle.an
End Method
End Type
Global allweights:tlist=New tlist
Type weight
Field mass#
Field an#
Field x#,y#
Method New()
allweights.addlast Self
End Method
Function create:weight(mass#,an#)
w:weight = New weight
w.mass = mass
w.an=an
Return w
End Function
Method Draw(dx# , dy#)
x = dx
y = dy
size#=mass*2
DrawRect x - size / 2 , y - size / 2 , size , size
End Method
End Type
Global hamsters:tlist=New tlist
Type hamster
Field mycircle:grandcircle
Field myramp:ramp
Field radius#
Field penheight#
Field an#
Field spin#
Field v#
Field mass#
Field runspeed#
Field weights:tlist
Field otan#,otr#,otwist#
Field van#
Field pradius#
Field getonrampan#
Method New()
hamsters.addlast Self
weights=New tlist
End Method
Function create:hamster(gc:grandcircle , radius#,an#=0,runspeed#=2)
h:hamster = New hamster
h.mycircle = gc
h.radius = radius
h.an = an
h.runspeed = runspeed
h.otan = h.an
h.otr=gc.radius-h.radius*.5
Return h
End Function
Method Draw()
drawan#=an-mycircle.an
x# = mycircle.cx + (pradius-radius)*Sin(drawan)
y# = mycircle.cy + (pradius - radius) * Cos(drawan)
'DrawText v,x,y
SetColor 0 , 0 , 255
SetAlpha .5
DrawOval x - radius , y - radius , 2 * radius , 2 * radius
SetAlpha .7
For spokean = 0 To 360 Step 60
ex# = x + Sin(spokean+spin) * radius
ey# = y + Cos(spokean + spin) * radius
'DrawText spokean , ex , ey
'DrawText Int(niceangle(spokean+spin)),ex,ey+12
DrawLine x,y,ex,ey
Next
SetAlpha 1
SetColor 255 , 255 , 255
For w:weight = EachIn weights
wx# = x + Sin(w.an + spin) * radius
wy# = y + Cos(w.an + spin) * radius
w.draw(wx , wy)
Next
End Method
Method update()
If KeyDown(KEY_LEFT)
v:+ runspeed/mass
ElseIf KeyDown(KEY_RIGHT)
v:- runspeed/mass
EndIf
If myramp
SetColor 255 , 0 , 0
myramp.draw()
SetColor 255 , 255 , 255
t# = (an - getonrampan) / myramp.anwide
opradius# = pradius
pradius# = myramp.radius(t)
v:* opradius / pradius
DrawText t,0,24
If t >= 1
mycircle = myramp.endcircle
an=an-myramp.mycircle.an+myramp.endcircle.an
myramp = Null
otan=an
EndIf
If t <= 0
myramp = Null
EndIf
EndIf
If myramp=Null
pradius# = mycircle.radius
If KeyDown(KEY_UP)
For r:ramp = EachIn mycircle.upramps
If rollover(niceangle(r.an-an)) And v*r.anwide>0
myramp = r
getonrampan#=an
EndIf
Next
EndIf
If KeyDown(KEY_DOWN)
For r:ramp = EachIn mycircle.downramps
If rollover(niceangle(r.endan() - an) ) And v * r.anwide < 0
myramp = r
mycircle = r.mycircle
an# = an + mycircle.an - r.endcircle.an
getonrampan# = an - r.anwide
EndIf
Next
EndIf
If KeyDown(KEY_SPACE)
For w:weight = EachIn mycircle.weights
wan# = niceangle(w.an - an)
If rollover(wan)
mycircle.weights.remove w
w.an# = -spin + wan * radius / mycircle.radius
weights.addlast w
EndIf
Next
Else
For w:weight = EachIn weights
wan# = niceangle(w.an + spin)
If rollunder(wan)
weights.remove w
mycircle.weights.addlast w
w.an = an
EndIf
Next
EndIf
EndIf
mass# = radius
For w:weight = EachIn weights
mass:+ w.mass
Next
v:-gravity*radius*Sin(an-mycircle.an)
v:*.99
an:+v
van# = - v * pradius / radius
spin#:+ van
penheight# = radius * .5
ty# = penheight * Cos(spin)
tx# = penheight * Sin(spin)
s1# = pradius - radius + ty
andiff# = ATan2(tx , s1)
DrawText s1,0,12
trail.create(mycircle , otan , otr , otwist , andiff + an , s1 , spin)
otan = andiff + an
otr = s1
otwist = spin
DrawText allweights.count()-weights.count(),0,0
End Method
Method rollover(wan#)
If wan / v < 1 And wan/v>=0
Return 1
EndIf
Return 0
End Method
Method rollunder(wan#)
'DrawText Int(wan),w.x,w.y
If wan / van < 1 And wan / van >= 0
Return 1
EndIf
Return 0
End Method
End Type
Global trails:tlist=New tlist
Type trail
Field mycircle:grandcircle
Field an1#,an2#
Field r1#,r2#
Field nxt:trail
Field life#
Field twist1# , twist2#
Field width#
Method New()
trails.addlast Self
life#=1
End Method
Function create:trail(gc:grandcircle , an1# , r1# , twist1# , an2# , r2# , twist2# , width#=10)
t:trail = New trail
t.mycircle = gc
t.an1 = an1
t.r1 = r1
t.twist1 = twist1
t.an2 = an2
t.r2 = r2
t.twist2 = twist2
t.width=width
Return t
End Function
Method Draw()
dan1# = an1 - mycircle.an
dan2# = an2 - mycircle.an
SetAlpha life*.5
x1# = mycircle.cx + r1 * Sin(dan1)
y1# = mycircle.cy + r1 * Cos(dan1)
x2# = mycircle.cx + r2 * Sin(dan2)
y2# = mycircle.cy + r2 * Cos(dan2)
'dtwist1#=twist1-mycircle.an
'dtwist2#=twist2-mycircle.an
dtwist1# =-twist1 - dan1
dtwist2#=-twist2-dan2
x1a# = x1 + width * Cos(dtwist1)
y1a# = y1 + width * Sin(dtwist1)
x1b# = x1 - width * Cos(dtwist1)
y1b# = y1 - width * Sin(dtwist1)
x2a# = x2 + width * Cos(dtwist2)
y2a# = y2 + width * Sin(dtwist2)
x2b# = x2 - width * Cos(dtwist2)
y2b# = y2 - width * Sin(dtwist2)
Local poly#[6]
poly = [x1a , y1a , x1b , y1b , x2b , y2b]
'DrawPoly poly
poly = [x1a , y1a , x2a , y2a , x2b , y2b]
'DrawPoly poly
DrawLine x1a , y1a , x1b , y1b
DrawLine x1a , y1a , x2a , y2a
DrawLine x1b , y1b , x2b , y2b
End Method
Method update()
life:- .01
If life <= 0
trails.remove Self
EndIf
End Method
End Type
Function niceangle#(an#)
an = an Mod 360
If an > 180 an:- 360
If an < - 180 an:+ 360
Return an
End Function
AppTitle="Hamstrograph"
Const gheight = 800
Const gwidth = 800
Graphics gheight , gwidth , 0
SetBlend ALPHABLEND
SetClsColor 180 , 180 , 180
Cls
gc:grandcircle = grandcircle.create(350)
For i = 1 To 15
gc.weights.addlast(weight.create(Rnd(4,10),Rand(180,210)))
Next
egc:grandcircle = grandcircle.create(180)
For i = 1 To 5
egc.weights.addlast(weight.create(5 , i * 72) )
Next
ramp.create(gc , egc , 180 , 180)
ramp.create(gc,egc,0,180)
ham:hamster=hamster.create(gc,60)
While Not KeyHit(KEY_ESCAPE)
For gc:grandcircle = EachIn grandcircles
gc.update()
gc.draw()
Next
For r:ramp = EachIn ramps
r.draw()
Next
For t:trail = EachIn trails
t.draw()
t.update()
Next
SetAlpha 1 'from trails
For h:hamster = EachIn hamsters
h.update()
h.draw()
Next
gc=grandcircle(grandcircles.first())
Flip
Cls
Wend