-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathHello3ZModel.js
55 lines (46 loc) · 1.51 KB
/
Hello3ZModel.js
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
import Model from 'https://code.agentscript.org/src/Model.js'
import * as util from 'https://code.agentscript.org/src/utils.js'
export default class HelloModel extends Model {
population = 100
speed = 0.1 // patches per step
wiggleAngle = 10 //util.degToRad(10)
radius
// ======================
// We can use Model's constructor, due to using Model's default World.
// If you pass in world options, Model will use them
// constructor() {
// super() // use default world options.
// }
setup() {
this.radius = this.world.maxX * 0.85
this.turtles.setDefault('atEdge', 'bounce')
this.turtles.create(this.population, t => {
t.moveTo(this.patches.oneOf())
this.moveToSphere(t)
})
this.turtles.ask(t => {
this.links.create(t, this.turtles.otherOneOf(t))
})
}
step() {
this.turtles.ask(t => {
// t.theta += util.randomCentered(this.wiggleAngle)
t.heading += util.randomCentered(this.wiggleAngle)
t.forward(this.speed)
this.moveToSphere(t)
})
}
moveToSphere(t) {
const { x, y } = t
const r = this.radius
const z2 = r * r - (x * x + y * y)
if (z2 <= 0) {
const theta = Math.atan2(y, x)
t.setxy(r * Math.cos(theta), r * Math.sin(theta), 0)
// t.theta = t.theta + Math.PI / 2
t.heading += 90
} else {
t.z = Math.sqrt(z2)
}
}
}