From 14f0db2d4073e21d51013005123e907669c60a57 Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 28 Dec 2023 02:31:43 +0300 Subject: [PATCH] feat: more or less fixed the parenting. --- cmd/test/debug.go | 9 ++++++- cmd/test/player.go | 4 +-- cmd/test/trianlge.go | 20 ++++++++++++-- transform.go | 64 +++++++++++++++++++++++++++++++++++--------- 4 files changed, 80 insertions(+), 17 deletions(-) diff --git a/cmd/test/debug.go b/cmd/test/debug.go index af824ea..3bdd70b 100644 --- a/cmd/test/debug.go +++ b/cmd/test/debug.go @@ -22,7 +22,14 @@ func (d *Debug) Draw(c *Context) { "fps: %d", int(c.FPS()), )) keyStrs = append(keyStrs, fmt.Sprintf( - "absPlayerPos: %v", player.Position, + "relPlayerPos: %v", player.Position, + )) + keyStrs = append(keyStrs, fmt.Sprintf( + "absPlayerPos: %v", player.AbsPosition(), + )) + + keyStrs = append(keyStrs, fmt.Sprintf( + "relTriPos: %v", tri.Position, )) keyStrs = append(keyStrs, fmt.Sprintf( "absTriPos: %v", tri.AbsPosition(), diff --git a/cmd/test/player.go b/cmd/test/player.go index 4044671..49de557 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -19,7 +19,7 @@ func NewPlayer() *Player { ret.Transform = gg.T() fmt.Println("transform:", ret.Transform) //ret.Parent = rect - ret.Scale = gg.V(5, 5) + ret.Scale = gg.V2(1) // Around center. ret.Around = gg.V2(.5) ret.MoveSpeed = 90. @@ -136,7 +136,7 @@ func (p *Player) Event(c *gg.Context) { c.Camera.Position = pos.Sub(ec.Abs) case *gg.WheelChange : c.Camera.Scale = c.Camera.Scale.Add(gg.V2( - ec.Offset.Y * c.DT() * p.ScaleSpeed * 20, + ec.Offset.Y * c.DT() * p.ScaleSpeed * 40, )) } diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index 281b044..c143289 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -1,7 +1,7 @@ package main import "github.com/di4f/gg" -//import "fmt" +import "fmt" type Tri struct { gg.DrawablePolygon @@ -20,7 +20,7 @@ func NewTri() *Tri { gg.V(0, -10), }, } - ret.Color = gg.Color{gg.MaxColorV, gg.MaxColorV, 0, gg.MaxColorV} + ret.Color = gg.Rgba(1, 1, 0, 1) ret.Visible = true ret.Layer = TriangleL @@ -51,3 +51,19 @@ func (t *Tri) Update(c *Context) { ) } } + +func (t *Tri) Event(c *Context) { + switch e := c.Event.(type) { + case *gg.KeyDown : + if e.Key != gg.Key1 { + break + } + if t.Connected() { + fmt.Println("disconnecting:", tri.Transform) + t.Disconnect() + } else { + t.Connect(player) + fmt.Println("connecting:", tri.Transform) + } + } +} diff --git a/transform.go b/transform.go index 7d7b4f8..a408d75 100644 --- a/transform.go +++ b/transform.go @@ -55,26 +55,41 @@ func (t Transform) ScaledToY(y Float) Transform { } func (t *Transform) SetAbsPosition(absPosition Vector) { - m := t.Matrix() + if t.Parent == nil { + t.Position = absPosition + return + } + m := t.ParentMatrix() m.Invert() t.Position = absPosition.Apply(m) } // Get the absolute representation of the transform. func (t *Transform) Abs() Transform { - m := t.Matrix() - ret := Transform{} - ret.Position = t.Position.Apply(m) + if t.Parent == nil { + return *t + } + + ret := T() + ret.Position = t.AbsPosition() ret.Rotation = t.AbsRotation() + ret.Scale = t.AbsScale() + return ret } func (t *Transform) AbsPosition() Vector { - return t.Position.Apply(t.Matrix()) + if t.Parent == nil { + return t.Position + } + return t.Position.Apply(t.ParentMatrix()) } func (t *Transform) AbsScale() Vector { - return V2(0) + if t.Parent == nil { + return t.Scale + } + return V2(1) } func (t *Transform) AbsRotation() Float { @@ -84,11 +99,23 @@ func (t *Transform) AbsRotation() Float { return t.Rotation + t.Parent.GetTransform().AbsRotation() } +func (t *Transform) SetAbsRotation(rot Float) { + if t.Parent == nil { + t.Rotation = rot + } + t.Rotation -= t.Parent.GetTransform().AbsRotation() +} + func (t *Transform) Connected() bool { return t.Parent != nil } -func (t *Transform) Connect(p Transformer) { +func (t *Transform) Connect(parent Transformer) { + absPosition := t.AbsPosition() + absRotation := t.AbsRotation() + t.Parent = parent + t.SetAbsPosition(absPosition) + t.SetAbsRotation(absRotation) } func (t *Transform) Disconnect() { @@ -96,7 +123,22 @@ func (t *Transform) Disconnect() { return } *t = t.Abs() - t.Parent = nil +} + +func (t *Transform) ParentMatrix() *Matrix { + g := &Matrix{} + if t.Parent == nil { + return g + } + + p := t.Parent.GetTransform() + g = p.ParentMatrix() + + g.Scale(p.Scale.X, p.Scale.Y) + g.Rotate(p.Rotation) + g.Translate(p.Position.X, p.Position.Y) + + return g } // Returns the GeoM with corresponding @@ -117,10 +159,8 @@ func (t *Transform)Matrix() *Matrix { // And finally move to the absolute position. g.Translate(t.Position.X, t.Position.Y) - if t.Parent != nil { - m := t.Parent.GetTransform().Matrix() - g.Concat(*m) - } + m := t.ParentMatrix() + g.Concat(*m) return g }