-
Notifications
You must be signed in to change notification settings - Fork 0
/
mond.go
189 lines (166 loc) · 3.72 KB
/
mond.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
package main
import (
"fmt"
"golang.org/x/image/colornames"
"image"
_ "image/png"
"math"
"math/rand"
"os"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
//"golang.org/x/image/colornames"
)
// Gravity Physics Stuff
const N = 1500
const G = 0.1
const eps = 0.1
const metric = 800
const zoom = 0.4
const winHeight = 800
const winWidth = 800
// 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
force Vector
}
// 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})
}
}
if this.pos.Norm() > 800{
this.acc = this.acc.Neg().Scale(100)
}
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.pos.y += this.vel.y*eps
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 Setip 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 := 0.0
Bodies[i] = Body{rand.Float64()*30 + 10, Vector{rand.Float64()*metric-metric/2, rand.Float64()*metric-metric/2}, Vector{c,c}, Vector{0,0}, Vector{0,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/25).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)
}