ichigo/engine/iso.go

32 lines
932 B
Go
Raw Normal View History

2021-08-31 15:16:56 +10:00
package engine
// Box describes an axis-aligned rectangular prism.
type Box struct {
X, Y, Z int // coordinate of the left-top-farthest corner
W, H, D int // width, height, depth
}
// IsoProjection translates an integer 3D coordinate into an integer 2D
// coordinate.
type IsoProjection struct {
ZX, ZY int
}
// Project projects a 3D coordinate into 2D.
// If ZX = 0, x is unchanged; similarly for ZY and y.
2021-09-01 15:57:02 +10:00
// Otherwise, x projects to x + z/ZX and y projects to y + z/ZY.
// Dividing is used because there's little reason for an isometric projection
2021-08-31 15:18:21 +10:00
// in a game to exaggerate the Z position, and integers are used to preserve
// "pixel perfect" calculation in case you are making the next Celeste.
2021-08-31 15:16:56 +10:00
func (π IsoProjection) Project(x, y, z int) (xp, yp int) {
2021-08-31 15:22:44 +10:00
// I'm using the π character because I'm a maths wanker
2021-08-31 15:16:56 +10:00
xp, yp = x, y
if π.ZX != 0 {
xp += z / π.ZX
}
if π.ZY != 0 {
yp += z / π.ZY
}
return xp, yp
}