fix: fixed the Transform parenting MatrixForParenting. And yes it was just because of just one symbol in the code.

This commit is contained in:
Andrey Parhomenko 2024-01-21 16:34:23 +03:00
parent b7d7ab9bf9
commit 9203bd1e79
6 changed files with 46 additions and 39 deletions

View file

@ -40,10 +40,10 @@ func (d *Debug) Draw(c *Context) []gg.EVertex {
)) ))
keyStrs = append(keyStrs, fmt.Sprintf( keyStrs = append(keyStrs, fmt.Sprintf(
"relTriPos: %v", tri.Position(), "relTriPos: %v", relTri.Position(),
)) ))
keyStrs = append(keyStrs, fmt.Sprintf( keyStrs = append(keyStrs, fmt.Sprintf(
"absTriPos: %v", relTri.Position(), "absTriPos: %v", tri.Position(),
)) ))
keyStrs = append(keyStrs, fmt.Sprintf( keyStrs = append(keyStrs, fmt.Sprintf(
"absTriRot: %v", gg.Degree(tri.Rotation()), "absTriRot: %v", gg.Degree(tri.Rotation()),

View file

@ -41,7 +41,7 @@ func main() {
Width: 720, Width: 720,
Height: 480, Height: 480,
VSync: true, VSync: true,
Fullscreen: true, Fullscreen: false,
}) })
var err error var err error

View file

@ -32,10 +32,12 @@ func NewPlayer() *Player {
ret.Collidable = true ret.Collidable = true
ret.Resolvable = true ret.Resolvable = true
return ret return ret
} }
func (p *Player) Start(c *Context) { func (p *Player) Start(c *Context) {
p.Connect(rect)
} }
func (p *Player) Draw(c *Context) []gg.EVertex { func (p *Player) Draw(c *Context) []gg.EVertex {
@ -68,14 +70,6 @@ func (p *Player) Update(c *Context) {
gg.X(p.ScaleSpeed*dt), gg.X(p.ScaleSpeed*dt),
), ),
) )
case gg.KeyArrowUp:
cam.Move(gg.Y(-p.MoveSpeed * dt))
case gg.KeyArrowDown:
cam.Move(gg.Y(p.MoveSpeed * dt))
case gg.KeyArrowLeft:
cam.Move(gg.X(-p.MoveSpeed*dt))
case gg.KeyArrowRight:
cam.Move(gg.X(p.MoveSpeed * dt))
case gg.KeyW: case gg.KeyW:
p.Move(gg.Y(-p.MoveSpeed * dt *10)) p.Move(gg.Y(-p.MoveSpeed * dt *10))
walking = true walking = true
@ -123,8 +117,9 @@ func (p *Player) Update(c *Context) {
case gg.Key5: case gg.Key5:
pp := *p pp := *p
counter++ counter++
p.Spawned = true pp.Spawned = true
p.Collidable = false pp.Collidable = false
pp.Resolvable = false
c.Spawn(&pp) c.Spawn(&pp)
} }
} }

View file

@ -30,7 +30,18 @@ func (r *Rect) CollisionType() gg.CollisionType {
func (r *Rect) Update(c *Context) { func (r *Rect) Update(c *Context) {
//r.R += 0.3 * e.DT() //r.R += 0.3 * e.DT()
dt := c.Dt().Seconds()
//r.Position = c.AbsCursorPosition() //r.Position = c.AbsCursorPosition()
for _, v := range c.Keys() { switch v {
case gg.KeyArrowUp:
r.Move(gg.Y(-10* dt))
case gg.KeyArrowDown:
r.Move(gg.Y(10 * dt))
case gg.KeyArrowLeft:
r.Move(gg.X(-10*dt))
case gg.KeyArrowRight:
r.Move(gg.X(10* dt))
}}
} }
func (r *Rect) Event(c *Context) { func (r *Rect) Event(c *Context) {

View file

@ -49,16 +49,12 @@ func (t *Tri) Update(c *Context) {
} }
for _, key := range keys { switch key { for _, key := range keys { switch key {
case gg.KeyM: case gg.KeyM:
absPos := t.Position() t.Move(gg.Y(100*dt*d))
t.SetPosition(
absPos.Add(gg.V(0, 100*dt*d)),
)
case gg.KeyN: case gg.KeyN:
absPos := t.Position() t.Move(gg.X(100*dt*d))
t.SetPosition(
absPos.Add(gg.V(100*dt*d, 0)),
)
case gg.KeyV: case gg.KeyV:
t.Rotate(d * gg.Pi * 0.3 * dt) t.Rotate(d * gg.Pi * 0.3 * dt)
case gg.Key2 : case gg.Key2 :

View file

@ -74,11 +74,13 @@ func (t *Transform) SetRotation(rotation Float) {
// Set the absolute object scale. // Set the absolute object scale.
func (t *Transform) SetScale(scale Vector) { func (t *Transform) SetScale(scale Vector) {
t.dirty = true t.dirty = true
t.parentDirty = true
t.scale = scale t.scale = scale
} }
func (t *Transform) AddScale(add ...Vector) { func (t *Transform) AddScale(add ...Vector) {
t.dirty = true t.dirty = true
//t.parentDirty = true
t.scale = t.scale.Add(add...) t.scale = t.scale.Add(add...)
} }
@ -93,17 +95,13 @@ func (t *Transform) Abs() Transform {
return *t return *t
} }
ret := T() ret := Transform{}
ret.position = t.Position() ret.position = t.Position()
ret.rotation = t.Rotation() ret.rotation = t.Rotation()
ret.scale = t.Scale()
// Do not need that ret.around = t.Around()
// cause parent does not affect ret.dirty = true
// scaling. ret.parentDirty = true
// (Should think if scaling from parent
// can be used anyhow)
//ret.scale = t.Scale()
return ret return ret
} }
@ -141,8 +139,8 @@ func (t *Transform) Rotation() Float {
} }
func (t *Transform) Rotate(add Float) { func (t *Transform) Rotate(add Float) {
t.rotation += add
t.dirty = true t.dirty = true
t.rotation += add
} }
func (t *Transform) Around() Vector { func (t *Transform) Around() Vector {
return t.around return t.around
@ -157,11 +155,19 @@ func (t *Transform) Connected() bool {
// Connect the object to another one making // Connect the object to another one making
// it its parent. // it its parent.
func (t *Transform) Connect(parent Transformer) { func (t *Transform) Connect(parent Transformer) {
absPosition := t.Position() if parent == nil {
absRotation := t.Rotation() return
}
if t.parent != nil {
t.Disconnect()
}
position := t.Position()
rotation := t.Rotation()
t.parent = parent.GetTransform() t.parent = parent.GetTransform()
t.SetPosition(absPosition) t.SetPosition(position)
t.SetRotation(absRotation) t.SetRotation(rotation)
} }
// Disconnect from the parent. // Disconnect from the parent.
@ -175,15 +181,14 @@ func (t *Transform) Disconnect() {
// Return the matrix and the inverted one for parenting children. // Return the matrix and the inverted one for parenting children.
func (t *Transform) MatrixForParenting() (Matrix, Matrix) { func (t *Transform) MatrixForParenting() (Matrix, Matrix) {
var m, mi Matrix var m, mi Matrix
if t.parentDirty { if t.parentDirty {
m.Scale(t.scale.X, t.scale.Y) //m.Scale(t.scale.X, t.scale.Y)
m.Rotate(t.rotation) m.Rotate(t.rotation)
m.Translate(t.position.X, t.position.Y) m.Translate(t.position.X, t.position.Y)
t.parentMatrix = m t.parentMatrix = m
mi := m mi = m
mi.Invert() mi.Invert()
t.parentInverted = mi t.parentInverted = mi