Point3->Int3; new Float3 type

This commit is contained in:
Josh Deprez 2021-09-07 13:28:44 +10:00
parent 1e029903a6
commit 02d91f9d23
8 changed files with 116 additions and 44 deletions

View file

@ -17,13 +17,13 @@ func init() {
// Actor handles basic movement. // Actor handles basic movement.
type Actor struct { type Actor struct {
CollisionDomain string // id of component to look for colliders inside of CollisionDomain string // id of component to look for colliders inside of
Pos, Size Point3 Pos, Size Int3
xRem, yRem, zRem float64 xRem, yRem, zRem float64
game *Game game *Game
} }
func (a *Actor) CollidesAt(p Point3) bool { func (a *Actor) CollidesAt(p Int3) bool {
bounds := Box{Min: p, Max: p.Add(a.Size)} bounds := Box{Min: p, Max: p.Add(a.Size)}
for c := range a.game.Query(a.CollisionDomain, ColliderType) { for c := range a.game.Query(a.CollisionDomain, ColliderType) {
if c.(Collider).CollidesWith(bounds) { if c.(Collider).CollidesWith(bounds) {

View file

@ -4,7 +4,7 @@ import "image"
// Box describes an axis-aligned rectangular prism. // Box describes an axis-aligned rectangular prism.
type Box struct { type Box struct {
Min, Max Point3 Min, Max Int3
} }
// String returns a string representation of b like "(3,4,5)-(6,5,8)". // String returns a string representation of b like "(3,4,5)-(6,5,8)".
@ -32,7 +32,7 @@ func (b Box) Overlaps(c Box) bool {
} }
// Size returns b's width, height, and depth. // Size returns b's width, height, and depth.
func (b Box) Size() Point3 { func (b Box) Size() Int3 {
return b.Max.Sub(b.Min) return b.Max.Sub(b.Min)
} }

View file

@ -35,7 +35,7 @@ type Camera struct {
// PointAt points the camera at a particular centre point and zoom, but adjusts // PointAt points the camera at a particular centre point and zoom, but adjusts
// for the bounds of the child component (if available). // for the bounds of the child component (if available).
func (c *Camera) PointAt(centre Point3, zoom float64) { func (c *Camera) PointAt(centre Int3, zoom float64) {
// Special sauce: if Child has a BoundingRect, make some adjustments // Special sauce: if Child has a BoundingRect, make some adjustments
bnd, ok := c.Child.(Bounder) bnd, ok := c.Child.(Bounder)
if !ok { if !ok {

View file

@ -41,6 +41,7 @@ type Game struct {
Hidden Hidden
ScreenSize image.Point ScreenSize image.Point
Root interface{} // typically a *Scene or SceneRef though Root interface{} // typically a *Scene or SceneRef though
VoxelScale Float3
dbmu sync.RWMutex dbmu sync.RWMutex
byID map[string]Identifier // Named components by ID byID map[string]Identifier // Named components by ID

View file

@ -6,8 +6,8 @@ import "image"
type IntMatrix3 [3][3]int type IntMatrix3 [3][3]int
// Apply applies the matrix to a vector to obtain a transformed vector. // Apply applies the matrix to a vector to obtain a transformed vector.
func (a IntMatrix3) Apply(v Point3) Point3 { func (a IntMatrix3) Apply(v Int3) Int3 {
return Point3{ return Int3{
X: v.X*a[0][0] + v.Y*a[0][1] + v.Z*a[0][2], X: v.X*a[0][0] + v.Y*a[0][1] + v.Z*a[0][2],
Y: v.X*a[1][0] + v.Y*a[1][1] + v.Z*a[1][2], Y: v.X*a[1][0] + v.Y*a[1][1] + v.Z*a[1][2],
Z: v.X*a[2][0] + v.Y*a[2][1] + v.Z*a[2][2], Z: v.X*a[2][0] + v.Y*a[2][1] + v.Z*a[2][2],
@ -40,19 +40,19 @@ func (a IntMatrix3) Concat(b IntMatrix3) IntMatrix3 {
type IntMatrix3x4 [3][4]int type IntMatrix3x4 [3][4]int
// Apply applies the matrix to a vector to obtain a transformed vector. // Apply applies the matrix to a vector to obtain a transformed vector.
func (a IntMatrix3x4) Apply(v Point3) Point3 { func (a IntMatrix3x4) Apply(v Int3) Int3 {
return Point3{ return Int3{
X: v.X*a[0][0] + v.Y*a[0][1] + v.Z*a[0][2] + a[0][3], X: v.X*a[0][0] + v.Y*a[0][1] + v.Z*a[0][2] + a[0][3],
Y: v.X*a[1][0] + v.Y*a[1][1] + v.Z*a[1][2] + a[1][3], Y: v.X*a[1][0] + v.Y*a[1][1] + v.Z*a[1][2] + a[1][3],
Z: v.X*a[2][0] + v.Y*a[2][1] + v.Z*a[2][2] + a[2][3], Z: v.X*a[2][0] + v.Y*a[2][1] + v.Z*a[2][2] + a[2][3],
} }
} }
// IntMatrix2x3 implements a 2 row, 3 column matrix (as two Point3 row vectors). // IntMatrix2x3 implements a 2 row, 3 column matrix (as two row vectors).
type IntMatrix2x3 [2]Point3 type IntMatrix2x3 [2]Int3
// Apply applies the matrix to a vector to obtain a transformed vector. // Apply applies the matrix to a vector to obtain a transformed vector.
func (a IntMatrix2x3) Apply(v Point3) image.Point { func (a IntMatrix2x3) Apply(v Int3) image.Point {
return image.Point{ return image.Point{
X: v.Dot(a[0]), X: v.Dot(a[0]),
Y: v.Dot(a[1]), Y: v.Dot(a[1]),

View file

@ -1,77 +1,78 @@
package engine package engine
import ( import (
"fmt"
"image" "image"
"strconv" "strconv"
) )
// Point3 is a an element of int^3. // Int3 is a an element of int^3.
type Point3 struct { type Int3 struct {
X, Y, Z int X, Y, Z int
} }
// Pt3(x, y, z) is shorthand for Point3{x, y, z}. // Pt3(x, y, z) is shorthand for Int3{x, y, z}.
func Pt3(x, y, z int) Point3 { func Pt3(x, y, z int) Int3 {
return Point3{x, y, z} return Int3{x, y, z}
} }
// String returns a string representation of p like "(3,4,5)". // String returns a string representation of p like "(3,4,5)".
func (p Point3) String() string { func (p Int3) String() string {
return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + "," + strconv.Itoa(p.Z) + ")" return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + "," + strconv.Itoa(p.Z) + ")"
} }
// XY applies the Z-forgetting projection. (It returns just X and Y.) // XY applies the Z-forgetting projection. (It returns just X and Y.)
func (p Point3) XY() image.Point { func (p Int3) XY() image.Point {
return image.Point{p.X, p.Y} return image.Point{p.X, p.Y}
} }
// Add performs vector addition. // Add performs vector addition.
func (p Point3) Add(q Point3) Point3 { func (p Int3) Add(q Int3) Int3 {
return Point3{p.X + q.X, p.Y + q.Y, p.Z + q.Z} return Int3{p.X + q.X, p.Y + q.Y, p.Z + q.Z}
} }
// Sub performs vector subtraction. // Sub performs vector subtraction.
func (p Point3) Sub(q Point3) Point3 { func (p Int3) Sub(q Int3) Int3 {
return p.Add(q.Neg()) return p.Add(q.Neg())
} }
// CMul performs componentwise multiplication. // CMul performs componentwise multiplication.
func (p Point3) CMul(q Point3) Point3 { func (p Int3) CMul(q Int3) Int3 {
return Point3{p.X * q.X, p.Y * q.Y, p.Z * q.Z} return Int3{p.X * q.X, p.Y * q.Y, p.Z * q.Z}
} }
// Mul performs scalar multiplication. // Mul performs scalar multiplication.
func (p Point3) Mul(k int) Point3 { func (p Int3) Mul(k int) Int3 {
return Point3{p.X * k, p.Y * k, p.Z * k} return Int3{p.X * k, p.Y * k, p.Z * k}
} }
// CDiv performs componentwise division. // CDiv performs componentwise division.
func (p Point3) CDiv(q Point3) Point3 { func (p Int3) CDiv(q Int3) Int3 {
return Point3{p.X / q.X, p.Y / q.Y, p.Z / q.Z} return Int3{p.X / q.X, p.Y / q.Y, p.Z / q.Z}
} }
// Div performs scalar division by k. // Div performs scalar division by k.
func (p Point3) Div(k int) Point3 { func (p Int3) Div(k int) Int3 {
return Point3{p.X / k, p.Y / k, p.Z / k} return Int3{p.X / k, p.Y / k, p.Z / k}
} }
// Neg returns the vector pointing in the opposite direction. // Neg returns the vector pointing in the opposite direction.
func (p Point3) Neg() Point3 { func (p Int3) Neg() Int3 {
return Point3{-p.X, -p.Y, -p.Z} return Int3{-p.X, -p.Y, -p.Z}
} }
// Coord returns the components of the vector. // Coord returns the components of the vector.
func (p Point3) Coord() (x, y, z int) { func (p Int3) Coord() (x, y, z int) {
return p.X, p.Y, p.Z return p.X, p.Y, p.Z
} }
// Sign returns a sign vector. // Sign returns a sign vector.
func (p Point3) Sign() Point3 { func (p Int3) Sign() Int3 {
return Point3{sign(p.X), sign(p.Y), sign(p.Z)} return Int3{sign(p.X), sign(p.Y), sign(p.Z)}
} }
// Dot returns the dot product of the two vectors. // Dot returns the dot product of the two vectors.
func (p Point3) Dot(q Point3) int { func (p Int3) Dot(q Int3) int {
return p.X*q.X + p.Y*q.Y + p.Z*q.Z return p.X*q.X + p.Y*q.Y + p.Z*q.Z
} }
@ -84,3 +85,73 @@ func sign(m int) int {
} }
return 1 return 1
} }
func signf(m float64) float64 {
if m == 0 {
return 0
}
if m < 0 {
return -1
}
return 1
}
// Float3 is an element of float64^3.
type Float3 struct {
X, Y, Z float64
}
// String returns a string representation of p like "(3.0,4.0,5.0)".
func (p Float3) String() string {
return fmt.Sprintf("(%f,%f,%f)", p.X, p.Y, p.Z)
}
// Add performs vector addition.
func (p Float3) Add(q Float3) Float3 {
return Float3{p.X + q.X, p.Y + q.Y, p.Z + q.Z}
}
// Sub performs vector subtraction.
func (p Float3) Sub(q Float3) Float3 {
return p.Add(q.Neg())
}
// CMul performs componentwise multiplication.
func (p Float3) CMul(q Float3) Float3 {
return Float3{p.X * q.X, p.Y * q.Y, p.Z * q.Z}
}
// Mul performs scalar multiplication.
func (p Float3) Mul(k float64) Float3 {
return Float3{p.X * k, p.Y * k, p.Z * k}
}
// CDiv performs componentwise division.
func (p Float3) CDiv(q Float3) Float3 {
return Float3{p.X / q.X, p.Y / q.Y, p.Z / q.Z}
}
// Div performs scalar division by k.
func (p Float3) Div(k float64) Float3 {
return Float3{p.X / k, p.Y / k, p.Z / k}
}
// Neg returns the vector pointing in the opposite direction.
func (p Float3) Neg() Float3 {
return Float3{-p.X, -p.Y, -p.Z}
}
// Coord returns the components of the vector.
func (p Float3) Coord() (x, y, z float64) {
return p.X, p.Y, p.Z
}
// Sign returns a sign vector.
func (p Float3) Sign() Float3 {
return Float3{signf(p.X), signf(p.Y), signf(p.Z)}
}
// Dot returns the dot product of the two vectors.
func (p Float3) Dot(q Float3) float64 {
return p.X*q.X + p.Y*q.Y + p.Z*q.Z
}

View file

@ -33,12 +33,12 @@ type PrismMap struct {
Disabled Disabled
Hidden Hidden
Map map[Point3]*Prism // pos -> prism Map map[Int3]*Prism // pos -> prism
DrawOrderBias image.Point // dot with pos.XY() = bias value DrawOrderBias image.Point // dot with pos.XY() = bias value
DrawOffset image.Point // offset applies to whole map DrawOffset image.Point // offset applies to whole map
PosToDraw IntMatrix2x3 // p.pos -> drawspace (before offset and camera and ...) PosToDraw IntMatrix2x3 // p.pos -> drawspace (before offset and camera and ...)
PosToWorld IntMatrix3x4 // p.pos -> worldspace PosToWorld IntMatrix3x4 // p.pos -> worldspace
PrismSize Point3 // in worldspace PrismSize Int3 // in worldspace
Sheet Sheet Sheet Sheet
} }
@ -65,7 +65,7 @@ func (m *PrismMap) Transform(pt Transform) (tf Transform) {
type Prism struct { type Prism struct {
Cell int Cell int
pos Point3 pos Int3
pm *PrismMap pm *PrismMap
} }

View file

@ -8,7 +8,7 @@ type IntProjection image.Point
// //
// If π.X = 0, the x returned is p.X; similarly for π.Y and y. // If π.X = 0, the x returned is p.X; similarly for π.Y and y.
// Otherwise, x projects to x + z/π.X and y projects to y + z/π.Y. // Otherwise, x projects to x + z/π.X and y projects to y + z/π.Y.
func (π IntProjection) Project(p Point3) image.Point { func (π IntProjection) Project(p Int3) image.Point {
/* /*
I'm using the π character because I'm a maths wanker. I'm using the π character because I'm a maths wanker.