From 02a6cc3e88e9d0d1196c67e5eda326a88ea99944 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sat, 18 Feb 2023 17:38:28 +0500 Subject: [PATCH] Started implementing cameras rotating point. --- src/cmd/test/main.go | 18 ++++++++++++++++-- src/gx/camera.go | 13 ++++++++++--- src/gx/main.go | 6 +++++- src/gx/sprite.go | 7 +++++-- src/gx/transform.go | 2 +- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index b318d4f..7181111 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -26,9 +26,8 @@ func NewPlayer() *Player { Sprite: &gx.Sprite{ Object: &gx.Object{ T: gx.Transform { - P: gx.Vector{100, 150}, S: gx.Vector{5, 5}, - RA: gx.Vector{200, 200}, + RA: gx.Vector{320, 240}, }, }, Image: playerImg, @@ -38,6 +37,9 @@ func NewPlayer() *Player { } } +func (p *Player) Start(e *gx.Engine) { +} + func (p *Player) Update(e *gx.Engine) error { dt := e.DT() c := e.Camera() @@ -76,6 +78,18 @@ func (p *Player) Update(e *gx.Engine) error { } else { c.Object.T.S.Y += gx.Pi * p.ScaleSpeed * dt } + case ebiten.KeyZ : + if e.KeyIsPressed(ebiten.KeyShift) { + c.Object.T.RA.X -= gx.Pi * p.MoveSpeed * dt + } else { + c.Object.T.RA.X += gx.Pi * p.MoveSpeed * dt + } + case ebiten.KeyX : + if e.KeyIsPressed(ebiten.KeyShift) { + c.Object.T.RA.Y -= gx.Pi * p.MoveSpeed * dt + } else { + c.Object.T.RA.Y += gx.Pi * p.MoveSpeed * dt + } }} return nil diff --git a/src/gx/camera.go b/src/gx/camera.go index 1957af5..69d20b2 100644 --- a/src/gx/camera.go +++ b/src/gx/camera.go @@ -18,7 +18,10 @@ const ( // position, scale and rotation to apply // it to the objects to get the real // transform to display on the screen. -func (c *Camera)Matrix(scale bool) Matrix { +func (c *Camera)RealMatrix( + e *Engine, + scale bool, +) Matrix { g := &Matrix{} // For rotating around. @@ -27,12 +30,16 @@ func (c *Camera)Matrix(scale bool) Matrix { c.Object.T.RA.Y, ) - g.Rotate(c.Object.T.R) - g.Translate( -c.Object.T.P.X, c.Object.T.P.Y, ) + g.Rotate(-c.Object.T.R) + + g.Translate( + c.Object.T.RA.X, + -c.Object.T.RA.Y, + ) if scale { g.Scale( diff --git a/src/gx/main.go b/src/gx/main.go index 393d380..8d6b5fb 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -33,6 +33,10 @@ func (e *Engine) Camera() *Camera { return e.camera } +func (e *Engine) SetCamera(c *Camera) { + e.camera = c +} + func (e *Engine) Keys() []Key { return e.keys } @@ -50,7 +54,6 @@ func New( Object: &Object{ T: Transform{ S: Vector{1, 1}, - RA: Vector{480, 320}, }, }, }, @@ -121,6 +124,7 @@ func (e *engine) Layout(ow, oh int) (int, int) { if e.wcfg.FixedSize { return e.wcfg.Width, e.wcfg.Height } + return ow, oh } diff --git a/src/gx/sprite.go b/src/gx/sprite.go index 62c4651..12dc19b 100644 --- a/src/gx/sprite.go +++ b/src/gx/sprite.go @@ -15,10 +15,13 @@ func (s *Sprite) Draw( i *Image, ) { op := &ebiten.DrawImageOptions{} - m := s.Object.T.Matrix() + m := s.Object.T.Matrix(e) if e.camera != nil { - m.Concat(e.camera.Matrix(true)) + m.Concat(e.camera.RealMatrix( + e, + true, + )) } op.GeoM = m diff --git a/src/gx/transform.go b/src/gx/transform.go index d9419f3..ca3165c 100644 --- a/src/gx/transform.go +++ b/src/gx/transform.go @@ -25,7 +25,7 @@ func T() Transform { // Returns the GeoM with corresponding // to the transfrom transformation -func (t Transform)Matrix() Matrix { +func (t Transform)Matrix(e *Engine) Matrix { g := &Matrix{} g.Scale(t.S.X, t.S.Y)