Use Ebitens coordinate system for showing.

This commit is contained in:
Andrey Parhomenko 2023-05-30 22:35:49 +03:00
parent 421321cc62
commit bc2b5809c5
7 changed files with 38 additions and 53 deletions

View file

@ -37,11 +37,11 @@ func main() {
t := gx.Rectangle{
Transform: gx.Transform{
S: gx.Vector{1, 1},
S: gx.Vector{100, 200},
P: gx.Point{0, 200},
RA: gx.Point{0, 0},
R: 0,
},
W: 100,
H: 200,
}
points := []gx.Point{

View file

@ -26,9 +26,12 @@ type Rect struct {
func NewRect() *Rect {
return &Rect{&gx.DrawableRectangle{
Rectangle: gx.Rectangle{
Transform: gx.T(),
W: 200,
H: 400,
Transform: gx.Transform{
S: gx.Vector{
X: 200,
Y: 400,
},
},
},
Color: gx.Color{
gx.MaxColorV,
@ -85,9 +88,10 @@ 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,
Transform: gx.Transform{
P: player.P,
S: gx.Vector{10, 10},
},
},
Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV},
}
@ -97,7 +101,7 @@ func (p *Player) Draw(e *gx.Engine, i *gx.Image) {
func (p *Player) Start(e *gx.Engine, v ...any) {
fmt.Println("starting")
c := e.Camera()
c.RA = gx.V(360, -240)
c.RA = gx.V(360, 240)
}
func (p *Player) Update(e *gx.Engine) error {

View file

@ -16,22 +16,10 @@ func (c *Camera)RealMatrix(
e *Engine,
) Matrix {
g := &Matrix{}
g.Scale(
c.S.X,
c.S.Y,
)
g.Translate(
-c.P.X,
c.P.Y,
)
g.Rotate(-c.R)
g.Translate(
c.RA.X,
-c.RA.Y,
)
g.Scale(c.S.X, c.S.Y)
g.Translate(-c.P.X, -c.P.Y)
g.Rotate(c.R)
g.Translate(c.RA.X, c.RA.Y)
return *g
}

View file

@ -2,16 +2,7 @@ package gx
// The structure represents any circles.
type Circle struct {
// Position.
P Point
// Radius.
R Float
Transform
}
func (c Circle) ColliderSimplify() Rectangle {
return Rectangle{
W: c.R * 2,
H: c.R * 2,
}
}

View file

@ -5,7 +5,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/surdeus/godat/src/sparsex"
"github.com/surdeus/godat/src/poolx"
//"fmt"
"fmt"
"time"
)
@ -56,6 +56,9 @@ func (e *Engine) Keys() []Key {
func New(
cfg *WindowConfig,
) *Engine {
w := Float(cfg.Width)
h := Float(cfg.Height)
fmt.Println("res:", w, h)
return &Engine{
wcfg: cfg,
layers: sparsex.New[
@ -65,6 +68,7 @@ func New(
camera: &Camera{
Transform: Transform{
S: Vector{1, 1},
RA: V(w/2, h/2),
},
},
updaters: poolx.New[Updater](),

View file

@ -12,10 +12,8 @@ type Rectangle struct {
// Position of up left corner
// and the point to
// rotate around(relatively of position, not absolute).
// Scale represent width and height.
Transform
// Width and height.
W, H Float
}
// The type describes rectangle that can be drawn.
@ -40,10 +38,13 @@ func (r Rectangle) Corners() []Point {
// Get 2 triangles that the rectangle consists of.
func (r Rectangle) Triangles() Triangles {
m := r.Matrix()
p1 := r.P.Apply(&m)
p2 := r.P.Add(Vector{r.W, 0}).Apply(&m)
p3 := r.P.Add(Vector{r.W, -r.H}).Apply(&m)
p4 := r.P.Add(Vector{0, -r.H}).Apply(&m)
p1 := V(0, 0).Apply(&m)
p2 := V(1, 0).Apply(&m)
p3 := V(1, -1).Apply(&m)
p4 := V(0, -1).Apply(&m)
//fmt.Println("in:", p1, p2, p3, p4)
return Triangles{
Triangle{p1, p2, p3},
@ -72,9 +73,6 @@ func (r *DrawableRectangle) Draw(
img := NewImage(1, 1)
img.Set(0, 0, r.Color)
t.S.X *= r.W
t.S.Y *= r.H
m := t.Matrix()
rm := e.Camera().RealMatrix(e)
@ -89,21 +87,21 @@ func (r *DrawableRectangle) Draw(
// Use the Color as base image if no is provided.
var did bool
//var did bool
if r.Images[0] == nil {
r.Images[0] = NewImage(1, 1)
r.Images[0].Set(0, 0, r.Color)
did = true
//did = true
}
w, h := r.Images[0].Size()
if !did {
/*if !did {
t.S.X /= Float(w)
t.S.Y /= Float(h)
t.S.X *= r.W
t.S.Y *= r.H
}
}*/
rm := e.Camera().RealMatrix(e)

View file

@ -21,14 +21,14 @@ func T() Transform {
}
// Returns the GeoM with corresponding
// to the transfrom transformation
// to the transfrom transformation.
func (t Transform)Matrix() Matrix {
g := &Matrix{}
g.Scale(t.S.X, t.S.Y)
g.Translate(-t.RA.X, -t.RA.Y)
g.Rotate(t.R)
g.Translate(t.P.X, -t.P.Y)
g.Translate(t.P.X, t.P.Y)
return *g
}