gg/src/gx/transform.go

63 lines
1 KiB
Go
Raw Normal View History

2023-02-17 12:47:17 +03:00
package gx
2023-02-17 23:51:40 +03:00
import (
"github.com/hajimehoshi/ebiten/v2"
2023-02-18 19:35:38 +03:00
"math"
2023-02-17 23:51:40 +03:00
)
type Matrix = ebiten.GeoM
type Float = float64
2023-02-17 12:47:17 +03:00
type Vector struct {
X, Y Float
}
type Transform struct {
// Position, scale, rotate around.
P, S, RA Vector
// Rotation angle in radians.
R Float
}
func V(x, y Float) Vector {
return Vector{x, y}
}
2023-02-18 19:35:38 +03:00
// Returns the vector with the matrix applied
func (v Vector) Apply(m *Matrix) Vector {
x, y := m.Apply(v.X, v.Y)
return V(x, y)
}
// Returns the vector rotated by "a" angle in radians.
func (v Vector) Rotate(a Float) Vector {
m := &Matrix{}
m.Rotate(a)
return v.Apply(m)
}
// Returns the normalized vector.
func (v Vector) Norm() Vector {
l := math.Sqrt(v.X*v.X + v.Y*v.Y)
return V(v.X / l, v.Y / l)
}
2023-02-17 12:47:17 +03:00
func T() Transform {
ret := Transform{}
return ret
}
2023-02-17 23:51:40 +03:00
// Returns the GeoM with corresponding
// to the transfrom transformation
func (t Transform)Matrix(e *Engine) Matrix {
2023-02-17 23:51:40 +03:00
g := &Matrix{}
g.Scale(t.S.X, t.S.Y)
2023-02-18 02:03:28 +03:00
g.Translate(-t.RA.X, -t.RA.Y)
2023-02-17 23:51:40 +03:00
g.Rotate(t.R)
g.Translate(t.P.X, -t.P.Y)
2023-02-17 23:51:40 +03:00
return *g
}