Repository navigation
Expand file tree
/
Copy pathnbody.html
More file actions
88 lines (81 loc) · 3.27 KB
/
Copy pathnbody.html
File metadata and controls
88 lines (81 loc) · 3.27 KB
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
<html>
<head>
<title>NBody Model</title>
<script src="../lib/agentscript.js"></script>
<script src="../tools/coffee-script.js"></script>
<script type="text/coffeescript">
u = ABM.Util # ABM.Util alias
class MyModel extends ABM.Model
setup: ->
@refreshPatches = false # for static patches
# globals
@population = 4
@sun = true
@v0max = 1
@gravExp = -2
# use maxX/Ycor: turtle coords extend to +/-.5 further than maxX/Y.
@maxDistanceFromSun = Math.sqrt(Math.pow(@world.maxYcor,2)+Math.pow(@world.maxXcor,2))
# defaults
@turtles.setDefault "shape", "circle"
@turtles.setDefault "heading", 0 # override promotion to random angle
# window.foo = @patches.nOf(@population)
for p in @patches.nOf(@population)
p.sprout 1, @turtles, (a) =>
if @sun and a.id is 0
a.mass = 10000; a.setXY 0,0; a.vx = a.vy = 0
else
a.mass = 10 + u.randomInt(1000)
a.vx = @v0max * (1 - u.randomFloat(2))
a.vy = @v0max * (1 - u.randomFloat(2))
# a.penDown = true
a.size = 3 * u.log10(a.mass)
# @turtles.setDefaultPen(1, true)
@turtles.setDefault "penSize", 1 #Pen(1, true)
@turtles.setDefault "penDown", true #Pen(1, true)
step: ->
# console.log @anim.toString() if @anim.ticks % 100 is 0
for a in @turtles
for a1 in @turtles.other(a)
k = Math.pow(10, @gravExp)*a1.mass/Math.max(Math.pow(a.distance(a1),3), .000001)
a.vx += (a1.x-a.x)*k; a.vy += (a1.y-a.y)*k
for a in @turtles when not (@sun? and a.id is 0)
# unless (@sun and a.id is 0)
# scale opacity by distance to center
opacityScale = (1-a.distance(@patches.patch(0,0))/@maxDistanceFromSun)
# Lots of ways to scale opacity:
# - Build an opacity gradient (ColorMaps) for each planet
# - Create a new color w/ the new opacity/alpha
# - Set the color's r,g,b,a via setter or property name
[r, g, b] = a.color # first, get my r,g,b,a values
# safe to add 10, will clip when expression > 255
# a.color = [r, g, b, (10+opacityScale*255)]
a.setColor r, g, b, (10+opacityScale*255) # way less GC
# console.log opacityScale if a.id is 2
# a.color = a.color
# a.scaleOpacity a.color.slice(0,3), opacityScale
#
a.setXY u.clamp(a.x+a.vx, @patches.minX, @patches.maxX),
u.clamp(a.y+a.vy, @patches.minY, @patches.maxY)
a.vx = u.sign(-a.x)*.001 if Math.abs(a.x) is @patches.maxX
a.vy = u.sign(-a.y)*.001 if Math.abs(a.y) is @patches.maxY
# # a.setXY 0,0 if @sun and a.id is 0
null
# div, patchSize, minX, maxX, minY, maxY, isTorus, hasNeighbors
# Defaults: 13, -16, 16, -16, 16, false, true
model = new MyModel {
div: "layers",
size: 2,
minX: -120,
maxX: 120,
minY: -100,
maxY: 100,
hasNeighbors: false
}
model.debug() # Debug: Put Model vars in global name space
model.start() # Run model immediately after startup initialization
</script>
</head>
<body>
<div id="layers"></div>
</body>
</html>