-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.go
74 lines (64 loc) · 1.49 KB
/
camera.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
package pecel
import (
"fmt"
"math"
"github.com/hajimehoshi/ebiten/v2"
)
type Camera struct {
Width, Height float64
Viewport Vec2
Position Vec2
Rotation int
ZoomFactor int
}
func CreateCamera(width, height float64) *Camera {
camera := &Camera{
Width: width,
Height: height,
Viewport: Vec2{width, height},
}
return camera
}
func (c *Camera) String() string {
return fmt.Sprintf("T: %.1f, R: %d, S: %d", c.Position, c.Rotation, c.ZoomFactor)
}
func (c *Camera) viewportCenter() Vec2 {
return Vec2{
c.Viewport.X * 0.5,
c.Viewport.Y * 0.5,
}
}
func (c *Camera) worldMatrix() ebiten.GeoM {
m := ebiten.GeoM{}
m.Translate(-c.Position.X, -c.Position.Y)
// scale and rotate around center of image / screen
// m.Translate(-c.viewportCenter().X, -c.viewportCenter().Y)
m.Scale(
math.Pow(1.01, float64(c.ZoomFactor)),
math.Pow(1.01, float64(c.ZoomFactor)),
)
m.Rotate(float64(c.Rotation) * 2 * math.Pi / 360)
m.Translate(c.viewportCenter().X, c.viewportCenter().Y)
return m
}
func (c *Camera) Render(world, screen *ebiten.Image) {
screen.DrawImage(world, &ebiten.DrawImageOptions{
GeoM: c.worldMatrix(),
})
}
func (c *Camera) ScreenToWorld(pos Vec2) Vec2 {
inverseMatrix := c.worldMatrix()
if inverseMatrix.IsInvertible() {
inverseMatrix.Invert()
x, y := inverseMatrix.Apply(float64(pos.X), float64(pos.Y))
return Vec2{x, y}
} else {
return Vec2{0, 0}
}
}
func (c *Camera) Reset() {
c.Position.X = 0
c.Position.Y = 0
c.Rotation = 0
c.ZoomFactor = 0
}