-
Notifications
You must be signed in to change notification settings - Fork 0
/
gograv.go
206 lines (176 loc) · 4.11 KB
/
gograv.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
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
package main
import (
"fmt"
"golang.org/x/image/colornames"
//"golang.org/x/image/colornames"
"image"
_ "image/png"
"math"
"math/rand"
// "math/rand"
"os"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
// Instructions:
// Use the arrow keys to zoom
// Zoom with pinch/scroll
// Gravity Physics
const N = 100 // No. of bodies
const G = 100 // Gravity strength
const eps = 0.01/2 // time step
const metric = 800 // Coordinate scaling
const zoom = 0.2 // Initial Zoom
const winHeight = 600 // Window height
const winWidth = 600 // Window
func max(a float64, b float64) float64{
if a > b{
return a
}else{
return b
}
}
// Vector Definition
type Vector struct {
x, y float64
}
// Vector Methods
func (this Vector) Neg() Vector{
return Vector{this.x*-1, this.y*-1}
}
func (this Vector) Norm() float64{
return math.Sqrt(this.x*this.x + this.y*this.y)
}
func (this Vector) Add(that Vector) Vector{
return Vector{this.x + that.x,this.y + that.y}
}
func (this Vector) Scale(C float64) Vector{
return Vector{this.x*C,this.y*C}
}
//Body Struct
type Body struct{
mass float64
pos Vector
vel Vector
acc Vector
stat float64
}
// Body Methods
func (this Body) getGravity(Bodies [N]Body) Vector{
this.acc = Vector{0,0}
for i := 0; i<N; i++ {
var r = Bodies[i].pos.Add(this.pos.Neg()).Norm()
if r == 0 {
this.acc.Add(Vector{0, 0})
}else {
this.acc = (Bodies[i].pos.Add(this.pos.Neg()).Scale((G * Bodies[i].mass) / (r * r * r))).Add(this.acc)
}
}
return this.acc
}
func (this Body) getVel() Vector{
this.vel.x += this.acc.x*eps
this.vel.y += this.acc.y*eps
return this.vel
}
func (this Body) getPos() Vector{
this.pos.x += this.vel.x*eps*this.stat
this.pos.y += this.vel.y*eps*this.stat
return this.pos
}
// Loading the image given the path
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
// Running and Setup of the
func run() {
cfg := pixelgl.WindowConfig{
Title: "Giri's Gravity Simulations!",
Bounds: pixel.R(0, 0, winHeight, winWidth),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
win.SetSmooth(true)
pic, err := loadPicture("hiking.png")
if err != nil {
panic(err)
}
var bodyFrames [N]*pixel.Sprite
for i:=0;i<N;i++{
bodyFrames[i] = pixel.NewSprite(pic, pic.Bounds())
}
// Camera Setup
var(
camPos = Vector{0,0}
camSpeed float64 = 600
camZoomSpeed = 1.2
camZoom = 1.0
)
// Body Initialisation
var Bodies [N]Body
for i := 0; i<N; i++ {
c := 100.
Bodies[i] = Body{rand.Float64()*40 + 10, Vector{rand.Float64()*metric-metric/2, rand.Float64()*metric-metric/2}, Vector{rand.Float64()*c-c/2,rand.Float64()*c-c/2}, Vector{0,0}, 1.0}
}
// Fps Setup
var (
frames = 0
second = time.Tick(time.Second)
)
// Computing of Trajectories
last := time.Now()
for !win.Closed() {
win.Clear(colornames.Midnightblue)
dt := time.Since(last).Seconds()
last = time.Now()
// Keypress Activities
if win.Pressed(pixelgl.KeyLeft) {
camPos.x += camSpeed * dt
}
if win.Pressed(pixelgl.KeyRight) {
camPos.x -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyUp) {
camPos.y -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyDown) {
camPos.y += camSpeed * dt
}
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
// Computation of Trajectories
for i:=0;i<N;i++{
Bodies[i].acc = Bodies[i].getGravity(Bodies)
Bodies[i].vel = Bodies[i].getVel()
Bodies[i].pos = Bodies[i].getPos()
var x, y = Bodies[i].pos.x+winWidth/2, Bodies[i].pos.y+winHeight/2
mat := pixel.IM
mat = mat.Moved(pixel.V(x,y)).Scaled(pixel.V(x,y),zoom).Scaled(pixel.V(x,y),Bodies[i].mass/10).Moved(pixel.V(camPos.x,camPos.y)).Scaled(pixel.V(winWidth/2,winHeight/2),camZoom)
bodyFrames[i].Draw(win, mat)
}
win.Update()
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
}
}
// Finally running the program
func main() {
pixelgl.Run(run)
}