-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (111 loc) · 3.26 KB
/
main.go
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
package main
import (
"fmt"
"log"
"math"
"time"
"github.com/temhelk/tgrav/renderer"
"github.com/temhelk/tgrav/simulation"
"github.com/gdamore/tcell/v2"
"gonum.org/v1/gonum/spatial/r2"
)
func main() {
const timeStep float64 = 0.0001
sim := simulation.NewSimulation(timeStep)
sim.Bodies = simulation.LagrangeL4L5[:]
screen, err := tcell.NewScreen()
if err != nil {
log.Panicf("%+v", err)
}
if err := screen.Init(); err != nil {
log.Panicf("%+v", err)
}
defaultStyle := tcell.StyleDefault
screen.SetStyle(defaultStyle)
screen.EnableMouse()
screenDragging := false
var previousMouseX, previousMouseY int
var worldOffset r2.Vec
renderForceField := false
clearFrame := true
rend := renderer.NewRenderer()
// Ratio between simulation time and real time
var simulationSpeed float64 = 1
var simulationTimeAvailable float64
targetFrameTime := time.Duration(math.Floor(1.0 / 60 * float64(time.Second)))
lastFrameTime := time.Now()
outer:
for {
for screen.HasPendingEvent() {
event := screen.PollEvent()
switch event := event.(type) {
case *tcell.EventKey:
key := event.Key()
r := event.Rune()
if key == tcell.KeyCtrlC || key == tcell.KeyEscape {
break outer
}
if r == '+' {
simulationSpeed *= 2
} else if r == '-' {
simulationSpeed /= 2
}
if r == 'f' {
renderForceField = !renderForceField
}
if r == 'c' {
clearFrame = !clearFrame
}
case *tcell.EventMouse:
buttons := event.Buttons()
x, y := event.Position()
if buttons&tcell.WheelDown != 0 {
rend.WorldWidth *= 1.2
} else if buttons&tcell.WheelUp != 0 {
rend.WorldWidth /= 1.2
}
if screenDragging {
offsetX, offsetY := x - previousMouseX, y - previousMouseY
screenOffset := r2.Vec{X: float64(-offsetX), Y: float64(offsetY)}
worldOffset = r2.Add(worldOffset, rend.CellDirToWorld(screen, screenOffset))
previousMouseX = x
previousMouseY = y
}
if !screenDragging && (buttons&tcell.Button1 != 0) {
screenDragging = true
previousMouseX, previousMouseY = x, y
} else if screenDragging && (buttons&tcell.Button1 == 0) {
screenDragging = false
}
}
}
newFrameTime := time.Now()
deltaTime := newFrameTime.Sub(lastFrameTime)
lastFrameTime = newFrameTime
rend.AddFrameMessage(fmt.Sprintf("Δt: %.2f", deltaTime.Seconds()*1000))
rend.AddFrameMessage(fmt.Sprintf("Speed: %.2f", simulationSpeed))
simulationTimeAvailable += deltaTime.Seconds() * simulationSpeed
for simulationTimeAvailable >= sim.TimeStep {
simulationTimeAvailable -= sim.TimeStep
sim.Step()
}
rend.AddFrameMessage(fmt.Sprintf("Step: %d", sim.SimulationStep))
// totalEnergy := sim.CalculateTotalEnergy()
// rend.AddFrameMessage(fmt.Sprintf("Total energy: %.2e", totalEnergy))
// @TODO: Don't recalculate it all the time?
centerOfMass := sim.CalculateCenterOfMass()
rend.Center = r2.Add(centerOfMass, worldOffset)
// rend.AddFrameMessage(fmt.Sprintf("Center: <%.3e, %.3e>", centerOfMass.X, centerOfMass.Y))
if clearFrame {
screen.Clear()
}
if renderForceField {
rend.RenderForceField(screen, sim)
}
rend.Render(screen, sim)
screen.Show()
sleepFor := targetFrameTime - time.Now().Sub(lastFrameTime)
time.Sleep(sleepFor)
}
screen.Fini()
}