feat: more or less fixed the parenting.

This commit is contained in:
Andrey Parhomenko 2023-12-28 02:31:43 +03:00
parent c4c17fbaec
commit 14f0db2d40
4 changed files with 80 additions and 17 deletions

View file

@ -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(),

View file

@ -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,
))
}

View file

@ -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)
}
}
}

View file

@ -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
}