/
main.go
513 lines (445 loc) · 13.3 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package main
import (
"bytes"
"fmt"
"image"
"image/color"
_ "image/png"
"log"
"math"
"math/rand"
"strconv"
"golang.org/x/image/font"
"github.com/auyer/electrical-charges/sprites"
"github.com/hajimehoshi/ebiten/text"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font/gofont/goregular"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil"
)
// distance calculates the distance (Pythagorean Theorem) using the spacial coordinates of the charges
func distance(particle1, particle2 *Sprite) float64 {
deltaX := float64(particle1.x - particle2.x)
deltaY := float64(particle1.y - particle2.y)
return math.Sqrt(deltaX*deltaX+deltaY*deltaY) / 100 // this division turns the distance scale to cm/px instead for m/px
}
// force calculates the force between two charges
func force(particle1 *Sprite, particle2 *Sprite) float64 {
d := distance(particle1, particle2)
return k * (particle1.charge * particle2.charge) / (d * d)
}
// field calculates the eletric field on a given radius
func field(charge float64, radius float64) float64 {
return k * charge / (radius * radius)
}
// angle calculates the angle in rads by the arc tangent of the tangent formed by the two charges
func angle(particle1 *Sprite, particle2 *Sprite) float64 {
return math.Atan2(float64(particle1.y-particle2.y), float64(particle1.x-particle2.x))
}
func midPoint(particle1, particle2 *Sprite) (int, int) {
return (particle1.x + particle2.x) / 2, (particle1.y + particle2.y) / 2
}
// drawElectricalInformation draws the electrical information generated between two charges
func drawElectricalInformation(screen *ebiten.Image, sprite1, sprite2 *Sprite) {
opt := &ebiten.DrawImageOptions{}
opt.GeoM.Scale(1, distance(sprite1, sprite2)*100)
opt.GeoM.Rotate(angle(sprite1, sprite2) + math.Pi/2)
opt.GeoM.Translate(float64(sprite1.x)+20, float64(sprite1.y)+20)
screen.DrawImage(line, opt)
midx, midy := midPoint(sprite1, sprite2)
text.Draw(screen, fmt.Sprintf("%.2f m", distance(sprite1, sprite2)), theGame.Font, midx, midy, color.White)
text.Draw(screen, fmt.Sprintf("F= %.2e N", force(sprite1, sprite2)), theGame.Font, sprite2.x, sprite2.y+fontHeight*4, color.White)
text.Draw(screen, fmt.Sprintf("E= %.2e N/C", field(sprite1.charge, distance(sprite1, sprite2))), theGame.Font, sprite2.x, sprite2.y+fontHeight/10+fontHeight*5, color.White)
}
var (
negativeImage, neutralImage, positiveImage *ebiten.Image
rectangle, line *ebiten.Image
theGame *Game
fontHeight int
)
const (
k = 0.000000009 // Nm²/C²
fullScreenWidth = 800
fullScreenHeight = 600
screenWidth = fullScreenWidth
screenHeight = fullScreenHeight * .9
)
// Sprite represents an image.
type Sprite struct {
name string
image *ebiten.Image
x int
y int
charge float64
chosen bool
}
// In returns true if (x, y) is in the sprite, and false otherwise.
func (s *Sprite) In(x, y int) bool {
// Check the actual color (alpha) value at the specified position
// so that the result of In becomes natural to users.
//
// Note that this is not a good manner to use At for logic
// since color from At might include some errors on some machines.
// As this is not so important logic, it's ok to use it so far.
return s.image.At(x-s.x, y-s.y).(color.RGBA).A > 0
}
// MoveBy moves the sprite by (x, y).
func (s *Sprite) MoveBy(x, y int) {
w, h := s.image.Size()
s.x += x
s.y += y
if s.x < 0 {
s.x = 0
}
if s.x > screenWidth-w {
s.x = screenWidth - w
}
if s.y < 0 {
s.y = 0
}
if s.y > screenHeight-h {
s.y = screenHeight - h
}
}
// Draw draws the sprite.
func (s *Sprite) Draw(screen *ebiten.Image, dx, dy int, alpha float64) {
op := &ebiten.DrawImageOptions{}
// op.GeoM.Scale(0.5, 0.5)
op.GeoM.Translate(float64(s.x+dx), float64(s.y+dy))
if s.chosen {
op.ColorM.Scale(0.5, 0.5, 0.5, alpha)
} else {
op.ColorM.Scale(1, 1, 1, alpha)
}
text.Draw(screen, s.name, theGame.Font, s.x, s.y, color.White)
screen.DrawImage(s.image, op)
}
// DrawStatistics draws the sprites charge on the top of the screen.
func (s *Sprite) DrawStatistics(screen *ebiten.Image, x, y int, alpha float64) {
text.Draw(screen, fmt.Sprintf("'E' = Electric Field generated by %s. Negative = repulsion", s.name), theGame.Font, x, y, color.White)
text.Draw(screen, fmt.Sprintf("'F' = Force between %s and each charge. Positive = attraction", s.name), theGame.Font, x, y+fontHeight+fontHeight/2, color.White)
text.Draw(screen, fmt.Sprintf("%s Charge : %.2f C.", s.name, s.charge), theGame.Font, x, screenHeight, color.White)
}
// StrokeSource represents a input device to provide strokes.
type StrokeSource interface {
Position() (int, int)
IsJustReleased() bool
}
// MouseStrokeSource is a StrokeSource implementation of mouse.
type MouseStrokeSource struct{}
// Position returns the cursor position
func (m *MouseStrokeSource) Position() (int, int) {
return ebiten.CursorPosition()
}
// IsJustReleased checks if the mouse button was released
func (m *MouseStrokeSource) IsJustReleased() bool {
return inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft)
}
// TouchStrokeSource is a StrokeSource implementation of touch.
type TouchStrokeSource struct {
ID int
}
// Position returns the touch screen position
func (t *TouchStrokeSource) Position() (int, int) {
return ebiten.TouchPosition(t.ID)
}
// IsJustReleased checks if the touch command was released
func (t *TouchStrokeSource) IsJustReleased() bool {
return inpututil.IsTouchJustReleased(t.ID)
}
// Stroke manages the current drag state by mouse.
type Stroke struct {
source StrokeSource
// initX and initY represents the position when dragging starts.
initX int
initY int
// currentX and currentY represents the current position
currentX int
currentY int
released bool
// draggingObject represents a object (sprite in this case)
// that is being dragged.
draggingObject interface{}
}
// NewStroke creates a new stroke
func NewStroke(source StrokeSource) *Stroke {
cx, cy := source.Position()
return &Stroke{
source: source,
initX: cx,
initY: cy,
currentX: cx,
currentY: cy,
}
}
// Update function updates the stroke information for each game tick
func (s *Stroke) Update() {
if s.released {
return
}
if s.source.IsJustReleased() {
s.released = true
return
}
x, y := s.source.Position()
s.currentX = x
s.currentY = y
}
// IsReleased checks if the stroke was releases
func (s *Stroke) IsReleased() bool {
return s.released
}
// Position returns the current stroke position
func (s *Stroke) Position() (int, int) {
return s.currentX, s.currentY
}
// PositionDiff points the difference between the initial position and the current position
func (s *Stroke) PositionDiff() (int, int) {
dx := s.currentX - s.initX
dy := s.currentY - s.initY
return dx, dy
}
// DraggingObject returns the object being dragged
func (s *Stroke) DraggingObject() interface{} {
return s.draggingObject
}
// SetDraggingObject sets an object as being dragged
func (s *Stroke) SetDraggingObject(object interface{}) {
s.draggingObject = object
}
// Game struct stores the game state, its sprites, strokes, Font and the selected sprite
type Game struct {
strokes map[*Stroke]struct{}
sprites []*Sprite
Font font.Face
ChosenSprite *Sprite
}
func init() {
rand.Seed(25) // Deterministic rand seed
// creating a white rectangle to be used in the bottom of the screen
rectangle, _ = ebiten.NewImage(screenWidth, screenHeight/10, ebiten.FilterNearest)
rectangle.Fill(color.White)
// creating the line to link particles
line, _ = ebiten.NewImage(1, 1, ebiten.FilterNearest)
line.Fill(color.NRGBA{0x00, 0xff, 0x00, 0xff})
// negative sprite image
negimg, _, err := image.Decode(bytes.NewReader(sprites.Negative))
if err != nil {
log.Fatal(err)
}
negativeImage, _ = ebiten.NewImageFromImage(negimg, ebiten.FilterDefault)
// neutral sprite image
netimg, _, err := image.Decode(bytes.NewReader(sprites.Neutral))
if err != nil {
log.Fatal(err)
}
neutralImage, _ = ebiten.NewImageFromImage(netimg, ebiten.FilterDefault)
// positive sprite image
posimg, _, err := image.Decode(bytes.NewReader(sprites.Positive))
if err != nil {
log.Fatal(err)
}
positiveImage, _ = ebiten.NewImageFromImage(posimg, ebiten.FilterDefault)
// creating the font
tt, err := truetype.Parse(goregular.TTF)
if err != nil {
log.Fatal(err)
}
// Initialize the sprites.
sprites := []*Sprite{}
w, h := neutralImage.Size()
for i := 0; i < 2; i++ {
s := &Sprite{
name: "Q" + strconv.Itoa(i),
image: neutralImage,
x: rand.Intn(screenWidth - w),
y: rand.Intn(screenHeight - h),
charge: 0,
}
sprites = append(sprites, s)
}
// Initialize the game.
theGame = &Game{
strokes: map[*Stroke]struct{}{},
sprites: sprites,
Font: truetype.NewFace(tt, &truetype.Options{
Size: 12,
DPI: 142,
Hinting: font.HintingFull,
}),
ChosenSprite: nil,
}
b, _, _ := theGame.Font.GlyphBounds('M')
fontHeight = (b.Max.Y - b.Min.Y).Ceil()
}
// spriteAt function returns a sprite at the requested function or nil if none is found
func (g *Game) spriteAt(x, y int) *Sprite {
// As the sprites are ordered from back to front,
// search the clicked/touched sprite in reverse order.
for i := len(g.sprites) - 1; i >= 0; i-- {
s := g.sprites[i]
if s.In(x, y) {
return s
}
}
return nil
}
// drawHelp draws the help text on the bottom of the screen
func drawHelp(screen *ebiten.Image) {
textHeight := int(fullScreenHeight - fullScreenHeight*.05)
opts := &ebiten.DrawImageOptions{}
opts.GeoM.Translate(0, fullScreenHeight*.9+fullScreenHeight*.01)
screen.DrawImage(rectangle, opts)
text.Draw(screen, "LMB to select charge, drag to move, 'A' to add a new charge, ", theGame.Font, 0, textHeight, color.NRGBA{0xff, 0x00, 0x00, 0xff})
text.Draw(screen, "'P' to increase charge, 'N' to decrease charge. ", theGame.Font, 0, textHeight+fontHeight+fontHeight/5, color.NRGBA{0xff, 0x00, 0x00, 0xff})
}
func (g *Game) updateStroke(stroke *Stroke) {
stroke.Update()
if !stroke.IsReleased() {
return
}
s := stroke.DraggingObject().(*Sprite)
if s == nil {
return
}
s.MoveBy(stroke.PositionDiff())
index := -1
for i, ss := range g.sprites {
if ss == s {
index = i
break
}
}
// Move the dragged sprite to the front.
g.sprites = append(g.sprites[:index], g.sprites[index+1:]...)
g.sprites = append(g.sprites, s)
stroke.SetDraggingObject(nil)
}
func (g *Game) update(screen *ebiten.Image) error {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
s := NewStroke(&MouseStrokeSource{})
spriteAtPos := g.spriteAt(s.Position())
s.SetDraggingObject(spriteAtPos)
g.strokes[s] = struct{}{}
for _, s := range g.sprites {
s.chosen = false
}
if spriteAtPos != nil {
spriteAtPos.chosen = true
}
g.ChosenSprite = spriteAtPos
}
for _, id := range inpututil.JustPressedTouchIDs() {
s := NewStroke(&TouchStrokeSource{id})
spriteAtPos := g.spriteAt(s.Position())
s.SetDraggingObject(spriteAtPos)
g.strokes[s] = struct{}{}
for _, s := range g.sprites {
s.chosen = false
}
if spriteAtPos != nil {
spriteAtPos.chosen = true
}
g.ChosenSprite = spriteAtPos
}
if inpututil.IsKeyJustPressed(ebiten.KeyA) {
s := &Sprite{
name: "Q" + strconv.Itoa(len(theGame.sprites)),
image: neutralImage,
x: rand.Intn(screenWidth),
y: rand.Intn(screenHeight),
charge: 0.,
}
theGame.sprites = append(theGame.sprites, s)
}
if inpututil.IsKeyJustPressed(ebiten.KeyP) {
for _, s := range g.sprites {
if s.chosen {
s.charge += 0.1
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
for _, s := range g.sprites {
if s.chosen {
s.charge -= 0.1
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
for _, s := range g.sprites {
if s.chosen {
s.y -= screenHeight / 10
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
for _, s := range g.sprites {
if s.chosen {
s.y += screenHeight / 10
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
for _, s := range g.sprites {
if s.chosen {
s.x += screenWidth / 10
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
for _, s := range g.sprites {
if s.chosen {
s.x -= screenWidth / 10
}
}
}
for s := range g.strokes {
g.updateStroke(s)
if s.IsReleased() {
delete(g.strokes, s)
}
}
if ebiten.IsDrawingSkipped() {
return nil
}
drawHelp(screen)
draggingSprites := map[*Sprite]struct{}{}
for s := range g.strokes {
if sprite := s.DraggingObject().(*Sprite); sprite != nil {
draggingSprites[sprite] = struct{}{}
}
}
for _, s := range g.sprites {
if _, ok := draggingSprites[s]; ok {
continue
}
switch {
case s.charge > 0.:
s.image = positiveImage
case s.charge < 0.:
s.image = negativeImage
default:
s.image = neutralImage
}
s.Draw(screen, 0, 0, 1)
if s.chosen {
s.DrawStatistics(screen, fullScreenWidth*.01, fullScreenHeight*.05, 1)
}
if g.ChosenSprite != nil && g.ChosenSprite != s {
drawElectricalInformation(screen, g.ChosenSprite, s)
}
}
for s := range g.strokes {
dx, dy := s.PositionDiff()
if sprite := s.DraggingObject().(*Sprite); sprite != nil {
sprite.Draw(screen, dx, dy, 0.5)
}
}
return nil
}
func main() {
if err := ebiten.Run(theGame.update, fullScreenWidth, fullScreenHeight, 1, "Electrical Charges demonstration"); err != nil {
log.Fatal(err)
}
}