From a89db46fe7444ab85757a899eaaddbe2ee8088a6 Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 11 May 2023 12:28:34 +0300 Subject: [PATCH] Added the structures I am gonna need to implement colliders and more. --- src/gx/collider-simplifier.go | 12 ------------ src/gx/collider.go | 32 ++++++++++++++++++++++++++++++ src/gx/img.go | 2 -- src/gx/line-segment.go | 7 +++++++ src/gx/main.go | 4 ++++ src/gx/matrix.go | 9 +++++++++ src/gx/misc.go | 3 +++ src/gx/point.go | 4 ++++ src/gx/ray.go | 7 +++++++ src/gx/transform.go | 37 ++++------------------------------- src/gx/vector.go | 33 +++++++++++++++++++++++++++++++ 11 files changed, 103 insertions(+), 47 deletions(-) delete mode 100644 src/gx/collider-simplifier.go create mode 100644 src/gx/collider.go create mode 100644 src/gx/line-segment.go create mode 100644 src/gx/matrix.go create mode 100644 src/gx/misc.go create mode 100644 src/gx/point.go create mode 100644 src/gx/ray.go create mode 100644 src/gx/vector.go diff --git a/src/gx/collider-simplifier.go b/src/gx/collider-simplifier.go deleted file mode 100644 index fa7b686..0000000 --- a/src/gx/collider-simplifier.go +++ /dev/null @@ -1,12 +0,0 @@ -package gx - -// Implementing the interface lets -// the engine to work faster about -// collisions because it first checks -// if the the bigger rectangles that -// contain more complicated structure -// do collide. -type ColliderSimplifier interface { - ColliderSimplify() Rect -} - diff --git a/src/gx/collider.go b/src/gx/collider.go new file mode 100644 index 0000000..216e2aa --- /dev/null +++ b/src/gx/collider.go @@ -0,0 +1,32 @@ +package gx + +// Implementing the interface lets +// the engine to work faster about +// collisions because it first checks +// if the the bigger collider that +// contain more complicated structure +// do collide. +type ColliderSimplifier interface { + ColliderSimplify() Rect +} + +// The structure represents all +// information on collision. +type Collision struct { + Other Collider +} + +// Every collider has to implement +// collision with every other type of collider +// for optimization. Not good for custom colliders +// but is fast. +type Collider interface { + Collides(Collider) *Collision +} + +// happening collision getting the Collision as +// argument. +type CollideEventer interface { + Collide(*Collision) +} + diff --git a/src/gx/img.go b/src/gx/img.go index 5197278..1e4b3c4 100644 --- a/src/gx/img.go +++ b/src/gx/img.go @@ -8,8 +8,6 @@ import ( ) type Image = ebiten.Image -type Rectangle = image.Rectangle -type Point = image.Point // The interface describes anything that can be // drawn. It will be drew corresponding to diff --git a/src/gx/line-segment.go b/src/gx/line-segment.go new file mode 100644 index 0000000..1da91f8 --- /dev/null +++ b/src/gx/line-segment.go @@ -0,0 +1,7 @@ +package gx + +type LineSegment struct { + P Point + V Vector +} + diff --git a/src/gx/main.go b/src/gx/main.go index e7e55e6..bb3093c 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -9,6 +9,10 @@ import ( "time" ) +// The type is used in all Engine interactions +// where you need floating values. +type Float = float64 + // The type represents order of drawing. type Layer int diff --git a/src/gx/matrix.go b/src/gx/matrix.go new file mode 100644 index 0000000..53f059d --- /dev/null +++ b/src/gx/matrix.go @@ -0,0 +1,9 @@ +package gx + +import ( + "github.com/hajimehoshi/ebiten/v2" + //"math" +) + +type Matrix = ebiten.GeoM + diff --git a/src/gx/misc.go b/src/gx/misc.go new file mode 100644 index 0000000..ca27035 --- /dev/null +++ b/src/gx/misc.go @@ -0,0 +1,3 @@ +package gx + + diff --git a/src/gx/point.go b/src/gx/point.go new file mode 100644 index 0000000..704e26c --- /dev/null +++ b/src/gx/point.go @@ -0,0 +1,4 @@ +package gx + +type Point Vector + diff --git a/src/gx/ray.go b/src/gx/ray.go new file mode 100644 index 0000000..772d928 --- /dev/null +++ b/src/gx/ray.go @@ -0,0 +1,7 @@ +package gx + +type Ray struct { + P Point + V Vector +} + diff --git a/src/gx/transform.go b/src/gx/transform.go index c322b6b..27986c6 100644 --- a/src/gx/transform.go +++ b/src/gx/transform.go @@ -1,47 +1,18 @@ package gx import ( - "github.com/hajimehoshi/ebiten/v2" - "math" + //"github.com/hajimehoshi/ebiten/v2" + //"math" ) -type Matrix = ebiten.GeoM -type Float = float64 - -type Vector struct { - X, Y Float -} - type Transform struct { - // Position, scale, rotate around. + // Position, scale, rotate around(relatively of position, not absolute). P, S, RA Vector // Rotation angle in radians. R Float } -func V(x, y Float) Vector { - return Vector{x, y} -} - -// Returns the vector with the matrix applied -func (v Vector) Apply(m *Matrix) Vector { - x, y := m.Apply(v.X, v.Y) - return V(x, y) -} - -// Returns the vector rotated by "a" angle in radians. -func (v Vector) Rotate(a Float) Vector { - m := &Matrix{} - m.Rotate(a) - return v.Apply(m) -} - -// Returns the normalized vector. -func (v Vector) Norm() Vector { - l := math.Sqrt(v.X*v.X + v.Y*v.Y) - return V(v.X / l, v.Y / l) -} - +// Returns empty Transform. func T() Transform { ret := Transform{} return ret diff --git a/src/gx/vector.go b/src/gx/vector.go new file mode 100644 index 0000000..8ecc726 --- /dev/null +++ b/src/gx/vector.go @@ -0,0 +1,33 @@ +package gx + +import ( + //"github.com/hajimehoshi/ebiten/v2" + "math" +) + +type Vector struct { + X, Y Float +} + +func V(x, y Float) Vector { + return Vector{x, y} +} + +// Returns the vector with the matrix applied +func (v Vector) Apply(m *Matrix) Vector { + x, y := m.Apply(v.X, v.Y) + return V(x, y) +} + +// Returns the vector rotated by "a" angle in radians. +func (v Vector) Rotate(a Float) Vector { + m := &Matrix{} + m.Rotate(a) + return v.Apply(m) +} + +// Returns the normalized vector. +func (v Vector) Norm() Vector { + l := math.Sqrt(v.X*v.X + v.Y*v.Y) + return V(v.X / l, v.Y / l) +}