-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
243 lines (193 loc) · 5.29 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"image"
_ "image/png"
"math"
"math/rand"
"os"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/zergon321/cirno"
"golang.org/x/image/colornames"
)
const (
numberOfParticles = 256
width = 1280
height = 720
speed = 200
intensity = 10
)
func loadPicture(pic string) (pixel.Picture, error) {
file, err := os.Open(pic)
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
}
func run() {
// Create a new window.
cfg := pixelgl.WindowConfig{
Title: "Cirno demo",
Bounds: pixel.R(0, 0, width, height),
//VSync: true,
//Undecorated: true,
//Monitor: pixelgl.PrimaryMonitor(),
}
win, err := pixelgl.NewWindow(cfg)
handleError(err)
// Setup physics.
rand.Seed(time.Now().UnixNano())
space, err := cirno.NewSpace(10, 20, width*2, height*2,
cirno.Zero(), cirno.NewVector(width, height), false)
handleError(err)
spaceTimer := time.Tick(5 * time.Second)
particles := make([]*cirno.Circle, 0)
// Create borders.
borderWest, err := cirno.NewLine(cirno.NewVector(0, 0), cirno.NewVector(0, height))
handleError(err)
borderSouth, err := cirno.NewLine(cirno.NewVector(0, 0), cirno.NewVector(width, 0))
handleError(err)
borderNorth, err := cirno.NewLine(cirno.NewVector(0, height), cirno.NewVector(width, height))
handleError(err)
borderEast, err := cirno.NewLine(cirno.NewVector(width, 0), cirno.NewVector(width, height))
handleError(err)
// Add the borders in the space.
err = space.Add(borderNorth)
handleError(err)
err = space.Add(borderWest)
handleError(err)
err = space.Add(borderSouth)
handleError(err)
err = space.Add(borderEast)
handleError(err)
// Create particles.
for i := 0; i < numberOfParticles/16; i++ {
for j := 0; j < numberOfParticles/16; j++ {
iDist := width / (numberOfParticles / 16)
jDist := height / (numberOfParticles / 16)
position := cirno.NewVector(float64((i+1)*iDist), float64((j+1)*jDist)).MultiplyByScalar(0.8)
particle, err := cirno.NewCircle(position, float64(rand.Intn(3)+5))
handleError(err)
particles = append(particles, particle)
err = space.Add(particle)
handleError(err)
}
}
// Setup graphics.
pic, err := loadPicture("round_particle.png")
handleError(err)
batch := pixel.NewBatch(&pixel.TrianglesData{}, pic)
sprites := make([]*pixel.Sprite, 0)
for range particles {
sprite := pixel.NewSprite(pic, pixel.R(0, 0, 45, 45))
sprites = append(sprites, sprite)
}
// Setup metrics.
last := time.Now()
fps := 0
cps := 0
perSecond := time.Tick(time.Second)
// Setup wireframe drawer.
imd := imdraw.New(nil)
for !win.Closed() {
deltaTime := time.Since(last).Seconds()
last = time.Now()
win.Clear(colornames.White)
// Movement.
for _, particle := range particles {
angle := math.Floor(rand.Float64() * 1000)
direction := cirno.NewVector(1, 0).Rotate(angle)
movement := direction.MultiplyByScalar(speed * deltaTime)
/*shapes, err := space.WouldBeCollidedBy(particle, movement, 0.0)
handleError(err)
// If a collision occurred, the particle won't
// go the full way.
pos := particle.Center()
if len(shapes) > 0 {
pos, err = cirno.Approximate(particle, movement, shapes, intensity)
handleError(err)
movement = pos.Subtract(particle.Center())
}*/
particle.Move(movement)
space.AdjustShapePosition(particle)
_, err = space.Update(particle)
handleError(err)
shapes, err := space.CollidingWith(particle)
handleError(err)
if len(shapes) > 0 {
cps += len(shapes)
particle.Move(movement.MultiplyByScalar(-1))
pos, _, _, err := cirno.Approximate(particle, movement, 0,
shapes, intensity, false)
handleError(err)
movement = pos.Subtract(particle.Center())
particle.Move(movement)
space.AdjustShapePosition(particle)
_, err = space.Update(particle)
handleError(err)
}
}
// Check if the time has come to
// reorganize the quad tree.
select {
case <-spaceTimer:
err = space.Rebuild()
handleError(err)
default:
}
// Rendering.
batch.Clear()
for i, particle := range particles {
scale := particle.Radius() * 2.0 / 45.0
angle := math.Floor(rand.Float64() * 100)
pos := particle.Center()
transform := pixel.IM.
Scaled(pixel.ZV, scale).
Rotated(pixel.ZV, angle).
Moved(pixel.V(pos.X, pos.Y))
sprites[i].Draw(batch, transform)
}
imd.Clear()
for cell, cellShapes := range space.Cells() {
imd.Color = colornames.Green
vertices := cell.Vertices()
imd.Push(
pixel.V(vertices[0].X, vertices[0].Y),
pixel.V(vertices[1].X, vertices[1].Y),
pixel.V(vertices[2].X, vertices[2].Y),
pixel.V(vertices[3].X, vertices[3].Y),
)
imd.Polygon(2)
imd.Push(pixel.V(cell.Center().X, cell.Center().Y))
imd.Circle(float64(len(cellShapes)), 2)
}
batch.Draw(win)
imd.Draw(win)
win.Update()
// Show FPS in the window title.
fps++
select {
case <-perSecond:
win.SetTitle(fmt.Sprintf("%s | FPS: %d, CPS: %d, CPF: %f", cfg.Title, fps, cps, float64(cps)/float64(fps)))
fps = 0
cps = 0
default:
}
}
}
func main() {
pixelgl.Run(run)
}
func handleError(err error) {
if err != nil {
panic(err)
}
}