-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathOrbitModel.js
111 lines (85 loc) · 3.09 KB
/
OrbitModel.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
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
import Model from 'https://code.agentscript.org/src/Model.js'
import * as util from 'https://code.agentscript.org/src/utils.js'
// Earth’s radius: 6,371,000 meters (6,371 km)
// LEO height: 200,000 meters (200 km)
// LEO as a percentage of Earth's radius: ~3.14%
let lastGravity = 0
export default class OrbitModel extends Model {
earthRadius
LEOHeight
gravity
LEORadius
orbitalVelocity
// gravity = 0.01 // Adjusted gravity for more realistic orbit
xPosition = 1 // should be +/- 1
vDirection = 1 // should be +/- 1
trail = []
trailSize = 200
// ======================
// constructor() {
// super() // use default world options.
// }
setLEOHeight(fraction) {
this.LEOHeight = fraction * this.world.maxX
this.resetRocket()
}
setEarthRadius(fraction) {
this.earthRadius = fraction * this.world.maxX
this.resetRocket()
}
setGravity(fraction) {
this.gravity = fraction
this.resetRocket()
}
resetRocket() {
if (isNaN(this.earthRadius * this.LEOHeight * this.gravity)) return
this.LEORadius = this.earthRadius + this.LEOHeight
this.orbitalVelocity = Math.sqrt(this.gravity * this.LEORadius)
this.rocket.setxy(0, this.xPosition * this.LEORadius)
this.rocket.vx = this.vDirection * this.orbitalVelocity
this.rocket.vy = 0
}
setNodes() {
const a1 = this.nodes.createOne(t => t.setxy(0, this.world.maxY))
const a2 = this.nodes.createOne(t => t.setxy(0, this.world.minY))
const a3 = this.nodes.createOne(t => t.setxy(this.world.maxX, 0))
const a4 = this.nodes.createOne(t => t.setxy(this.world.minX, 0))
this.links.createOne(a1, a2)
this.links.createOne(a3, a4)
}
setup() {
// why does this fail?
// this.turtles.setDefault('atEdge', 'bounce')
this.turtleBreeds('rockets earths trails nodes')
const earth = this.earths.createOne() // default: located at 0,0
this.rocket = this.rockets.createOne()
this.setNodes()
this.setLEOHeight(0.03)
this.setEarthRadius(0.2)
this.setGravity(0.01)
}
step() {
// const rocket = this.rocket
const { rocket, trail, trailSize } = this
const distance = Math.sqrt(rocket.x ** 2 + rocket.y ** 2)
const gravityForce =
(this.gravity * this.earthRadius ** 2) / distance ** 2
if (lastGravity !== this.gravity)
console.log('grav', this.gravity, gravityForce)
lastGravity = this.gravity
// Normalize gravity direction
const ux = -rocket.x / distance
const uy = -rocket.y / distance
// Apply gravity acceleration
rocket.vx += ux * gravityForce
rocket.vy += uy * gravityForce
// Update position
rocket.x += rocket.vx
rocket.y += rocket.vy
// add a trail object
trail.push(this.trails.createOne(t => t.setxy(rocket.x, rocket.y)))
if (trail.length > trailSize) {
trail.shift().die() // Limit trail length
}
}
}