...
This commit is contained in:
parent
308123f3d2
commit
421321cc62
10 changed files with 112 additions and 43 deletions
|
@ -35,16 +35,19 @@ func main() {
|
||||||
fmt.Println(l1.Crosses(l2))
|
fmt.Println(l1.Crosses(l2))
|
||||||
fmt.Println(l1.ContainsPoint(gx.Point{1, 4}))
|
fmt.Println(l1.ContainsPoint(gx.Point{1, 4}))
|
||||||
|
|
||||||
t := gx.Triangle{
|
t := gx.Rectangle{
|
||||||
gx.Point{0, 0},
|
Transform: gx.Transform{
|
||||||
gx.Point{0, 100},
|
S: gx.Vector{1, 1},
|
||||||
gx.Point{100, 0},
|
P: gx.Point{0, 200},
|
||||||
|
},
|
||||||
|
W: 100,
|
||||||
|
H: 200,
|
||||||
}
|
}
|
||||||
|
|
||||||
points := []gx.Point{
|
points := []gx.Point{
|
||||||
gx.Point{},
|
gx.Point{},
|
||||||
gx.Point{100, 0},
|
gx.Point{100, 0},
|
||||||
gx.Point{0, 100},
|
gx.Point{0, 99},
|
||||||
gx.Point{.1, .1},
|
gx.Point{.1, .1},
|
||||||
gx.Point{-1, -1},
|
gx.Point{-1, -1},
|
||||||
gx.Point{1, 1},
|
gx.Point{1, 1},
|
||||||
|
@ -53,9 +56,14 @@ func main() {
|
||||||
gx.Point{50, 1},
|
gx.Point{50, 1},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ts := t.Triangles()
|
||||||
|
t1 := ts[0]
|
||||||
|
t2 := ts[1]
|
||||||
|
fmt.Printf("Rectangle triangles:\n\t%v\n\t%v\n", t1, t2)
|
||||||
for _, p := range points {
|
for _, p := range points {
|
||||||
fmt.Println(t, p, t.ContainsPoint(p))
|
fmt.Println(p, t.ContainsPoint(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,8 @@ func (r *Rect) Update(e *gx.Engine) error {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
playerImg *gx.Image
|
playerImg *gx.Image
|
||||||
|
player *Player
|
||||||
|
rect *Rect
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPlayer() *Player {
|
func NewPlayer() *Player {
|
||||||
|
@ -79,6 +81,19 @@ func NewPlayer() *Player {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Player) Draw(e *gx.Engine, i *gx.Image) {
|
||||||
|
p.Sprite.Draw(e, i)
|
||||||
|
r := &gx.DrawableRectangle{
|
||||||
|
Rectangle: gx.Rectangle{
|
||||||
|
Transform: p.Transform,
|
||||||
|
W: 10,
|
||||||
|
H: 10,
|
||||||
|
},
|
||||||
|
Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV},
|
||||||
|
}
|
||||||
|
r.Draw(e, i)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Player) Start(e *gx.Engine, v ...any) {
|
func (p *Player) Start(e *gx.Engine, v ...any) {
|
||||||
fmt.Println("starting")
|
fmt.Println("starting")
|
||||||
c := e.Camera()
|
c := e.Camera()
|
||||||
|
@ -153,7 +168,14 @@ func (d *Debug) Draw(
|
||||||
for _, k := range e.Keys() {
|
for _, k := range e.Keys() {
|
||||||
keyStrs = append(keyStrs, k.String())
|
keyStrs = append(keyStrs, k.String())
|
||||||
}
|
}
|
||||||
e.DebugPrint(i, strings.Join(keyStrs, ", "))
|
|
||||||
|
if rect.ContainsPoint(player.P) {
|
||||||
|
keyStrs = append(keyStrs, "THIS IS SHIT")
|
||||||
|
}
|
||||||
|
|
||||||
|
e.DebugPrint(i,
|
||||||
|
strings.Join(keyStrs, ", "))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Debug) IsVisible() bool {return true}
|
func (d *Debug) IsVisible() bool {return true}
|
||||||
|
@ -173,8 +195,12 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
e.Add(0, NewPlayer())
|
player = NewPlayer()
|
||||||
|
rect = NewRect()
|
||||||
|
|
||||||
e.Add(1, &Debug{})
|
e.Add(1, &Debug{})
|
||||||
e.Add(-1, NewRect())
|
e.Add(0, player)
|
||||||
|
e.Add(-1, rect)
|
||||||
|
|
||||||
e.Run()
|
e.Run()
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,17 +14,13 @@ type Camera struct {
|
||||||
// need to calculate it each time for each object. )
|
// need to calculate it each time for each object. )
|
||||||
func (c *Camera)RealMatrix(
|
func (c *Camera)RealMatrix(
|
||||||
e *Engine,
|
e *Engine,
|
||||||
scale bool,
|
|
||||||
) Matrix {
|
) Matrix {
|
||||||
g := &Matrix{}
|
g := &Matrix{}
|
||||||
|
|
||||||
|
g.Scale(
|
||||||
if scale {
|
c.S.X,
|
||||||
g.Scale(
|
c.S.Y,
|
||||||
c.S.X,
|
)
|
||||||
c.S.Y,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
g.Translate(
|
g.Translate(
|
||||||
-c.P.X,
|
-c.P.X,
|
||||||
|
|
|
@ -37,6 +37,11 @@ func LoadImage(input io.Reader) (*Image, error) {
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewImage(w, h int) (*Image) {
|
||||||
|
return ebiten.NewImage(w, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (c Color) RGBA() (r, g, b, a uint32) {
|
func (c Color) RGBA() (r, g, b, a uint32) {
|
||||||
return uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A)
|
return uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
package gx
|
|
||||||
|
|
||||||
type Point Vector
|
|
||||||
|
|
|
@ -39,26 +39,24 @@ func (r Rectangle) Corners() []Point {
|
||||||
|
|
||||||
// Get 2 triangles that the rectangle consists of.
|
// Get 2 triangles that the rectangle consists of.
|
||||||
func (r Rectangle) Triangles() Triangles {
|
func (r Rectangle) Triangles() Triangles {
|
||||||
return Triangles{}
|
m := r.Matrix()
|
||||||
}
|
p1 := r.P.Apply(&m)
|
||||||
|
p2 := r.P.Add(Vector{r.W, 0}).Apply(&m)
|
||||||
/*func MustNewImage(w, h int) (*Image) {
|
p3 := r.P.Add(Vector{r.W, -r.H}).Apply(&m)
|
||||||
img, err := NewImage(w, h)
|
p4 := r.P.Add(Vector{0, -r.H}).Apply(&m)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return img
|
return Triangles{
|
||||||
}*/
|
Triangle{p1, p2, p3},
|
||||||
|
Triangle{p1, p4, p3},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether the rectangle contains the point.
|
||||||
func (r Rectangle) ContainsPoint(p Point) bool {
|
func (r Rectangle) ContainsPoint(p Point) bool {
|
||||||
return false
|
return r.Triangles().ContainsPoint(p)
|
||||||
}
|
|
||||||
|
|
||||||
func NewImage(w, h int) (*Image) {
|
|
||||||
return ebiten.NewImage(w, h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check whether the drawable rectangle should be drawn.
|
||||||
func (r *DrawableRectangle) IsVisible() bool {
|
func (r *DrawableRectangle) IsVisible() bool {
|
||||||
return r.Visible
|
return r.Visible
|
||||||
}
|
}
|
||||||
|
@ -77,8 +75,8 @@ func (r *DrawableRectangle) Draw(
|
||||||
t.S.X *= r.W
|
t.S.X *= r.W
|
||||||
t.S.Y *= r.H
|
t.S.Y *= r.H
|
||||||
|
|
||||||
m := t.Matrix(e)
|
m := t.Matrix()
|
||||||
rm := e.Camera().RealMatrix(e, true)
|
rm := e.Camera().RealMatrix(e)
|
||||||
|
|
||||||
m.Concat(rm)
|
m.Concat(rm)
|
||||||
|
|
||||||
|
@ -108,8 +106,8 @@ func (r *DrawableRectangle) Draw(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
rm := e.Camera().RealMatrix(e, true)
|
rm := e.Camera().RealMatrix(e)
|
||||||
m := t.Matrix(e)
|
m := t.Matrix()
|
||||||
m.Concat(rm)
|
m.Concat(rm)
|
||||||
|
|
||||||
// Drawing with shader.
|
// Drawing with shader.
|
||||||
|
|
|
@ -21,11 +21,10 @@ func (s *Sprite) Draw(
|
||||||
|
|
||||||
m := &Matrix{}
|
m := &Matrix{}
|
||||||
|
|
||||||
m.Concat(s.Matrix(e))
|
m.Concat(s.Matrix())
|
||||||
if !s.Floating {
|
if !s.Floating {
|
||||||
m.Concat(e.Camera().RealMatrix(
|
m.Concat(e.Camera().RealMatrix(
|
||||||
e,
|
e,
|
||||||
true,
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ func T() Transform {
|
||||||
|
|
||||||
// Returns the GeoM with corresponding
|
// Returns the GeoM with corresponding
|
||||||
// to the transfrom transformation
|
// to the transfrom transformation
|
||||||
func (t Transform)Matrix(e *Engine) Matrix {
|
func (t Transform)Matrix() Matrix {
|
||||||
g := &Matrix{}
|
g := &Matrix{}
|
||||||
|
|
||||||
g.Scale(t.S.X, t.S.Y)
|
g.Scale(t.S.X, t.S.Y)
|
||||||
|
|
|
@ -55,6 +55,16 @@ func (t Triangle) Sgn() Float {
|
||||||
(t[1].X - t[2].X) * (t[0].Y - t[2].Y)
|
(t[1].X - t[2].X) * (t[0].Y - t[2].Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ts Triangles) ContainsPoint(p Point) bool {
|
||||||
|
for _, t := range ts {
|
||||||
|
if t.ContainsPoint(p) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
//func (t Triangle)
|
//func (t Triangle)
|
||||||
/*
|
/*
|
||||||
func (r *DrawableRectangle) Draw(
|
func (r *DrawableRectangle) Draw(
|
||||||
|
|
|
@ -8,6 +8,9 @@ import (
|
||||||
type Vector struct {
|
type Vector struct {
|
||||||
X, Y Float
|
X, Y Float
|
||||||
}
|
}
|
||||||
|
type Point = Vector
|
||||||
|
|
||||||
|
type Vectors []Vector
|
||||||
|
|
||||||
func V(x, y Float) Vector {
|
func V(x, y Float) Vector {
|
||||||
return Vector{x, y}
|
return Vector{x, y}
|
||||||
|
@ -19,6 +22,34 @@ func (v Vector) Apply(m *Matrix) Vector {
|
||||||
return V(x, y)
|
return V(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds the vector to other one returning the result.
|
||||||
|
func (v Vector) Add(a ...Vector) Vector {
|
||||||
|
for _, r := range a {
|
||||||
|
v.X += r.X
|
||||||
|
v.Y += r.Y
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the subtraction of all the vectors from the current one.
|
||||||
|
func (v Vector) Sub(s ...Vector) Vector {
|
||||||
|
for _, r := range s {
|
||||||
|
v.X -= r.X
|
||||||
|
v.Y -= r.Y
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the negative version of the vector.
|
||||||
|
func (v Vector) Neg() Vector {
|
||||||
|
return Vector{
|
||||||
|
-v.X,
|
||||||
|
-v.Y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the vector rotated by "a" angle in radians.
|
// Returns the vector rotated by "a" angle in radians.
|
||||||
func (v Vector) Rotate(a Float) Vector {
|
func (v Vector) Rotate(a Float) Vector {
|
||||||
m := &Matrix{}
|
m := &Matrix{}
|
||||||
|
|
Loading…
Reference in a new issue