place blocks

This commit is contained in:
2022-02-24 20:20:33 +08:00
parent 9195dd7c3f
commit 32b06810e2
17 changed files with 309 additions and 55 deletions

View File

@ -4,7 +4,7 @@ package itype
// This is the major way how complicated data is stored in blocks and entities.
//
// The empty interface should only hold these 4 types:
// - int
// - int64
// - float64
// - bool
// - string

View File

@ -62,6 +62,10 @@ func (d Direction) Opposite() Direction {
return 0
}
func (d Direction) String() string {
return DirectionName[d]
}
// DirectionIs returns X/Y/Z for 0/1/2, and +/- for #pos/neg.
func DirectionIs(index int, positive bool) Direction {
switch index {

View File

@ -1,6 +1,8 @@
package itype
import "sort"
import (
"sort"
)
// Recti is a 2D rectangle with int coordinates.
type Recti struct {
@ -113,6 +115,32 @@ func (b Boxd) Offsetv(x, y, z float64) Boxd {
}
}
// GrowEven grows the box on each axis by deltaSize,
// not moving its center.
func (b Boxd) GrowEven(deltaSize Vec3d) Boxd {
return Boxd{
OffX: b.OffX - deltaSize[0]/2,
OffY: b.OffY - deltaSize[1]/2,
OffZ: b.OffZ - deltaSize[2]/2,
SizeX: b.SizeX + deltaSize[0],
SizeY: b.SizeY + deltaSize[1],
SizeZ: b.SizeZ + deltaSize[2],
}
}
// GrowOrigin grows the box on each axis by deltaSize,
// not moving its origin.
func (b Boxd) GrowOrigin(deltaSize Vec3d) Boxd {
return Boxd{
OffX: b.OffX,
OffY: b.OffY,
OffZ: b.OffZ,
SizeX: b.SizeX + deltaSize[0],
SizeY: b.SizeY + deltaSize[1],
SizeZ: b.SizeZ + deltaSize[2],
}
}
func (b Boxd) MinPoint() Vec3d {
return Vec3d{b.OffX, b.OffY, b.OffZ}
}
@ -121,9 +149,9 @@ func (b Boxd) MaxPoint() Vec3d {
}
func (b Boxd) Contains(point Vec3d) bool {
return point[0] >= b.OffX && point[0] <= b.OffX+b.SizeX &&
point[1] >= b.OffY && point[1] <= b.OffY+b.SizeY &&
point[2] >= b.OffZ && point[2] <= b.OffZ+b.SizeZ
return point[0] > b.OffX && point[0] < b.OffX+b.SizeX &&
point[1] > b.OffY && point[1] < b.OffY+b.SizeY &&
point[2] > b.OffZ && point[2] < b.OffZ+b.SizeZ
}
func pointIntersect(n, m, p, q float64) (min, len float64) {
@ -157,7 +185,7 @@ func (box1 Boxd) Intersect(box2 Boxd) (ok bool, intersect Boxd) {
}
func between(val, min, max float64) bool {
return min <= val && val <= max
return min < val && val < max
}
// IntersectRay computes if a Box intersects with a ray.
@ -174,14 +202,14 @@ func (box Boxd) IntersectRay(start, dir Vec3d, length float64) (ok bool, hitface
for i := 0; i < 3; i++ {
if dir[i] > 0 {
t[i] = (min[i] - start[i]) / dir[i]
} else {
} else if dir[i] < 0 {
t[i] = (max[i] - start[i]) / dir[i]
}
}
maxi := 0
for i, ti := range t {
if ti > t[maxi] {
for i := range t {
if t[i] > t[maxi] {
maxi = i
}
}
@ -190,7 +218,7 @@ func (box Boxd) IntersectRay(start, dir Vec3d, length float64) (ok bool, hitface
pt := start.Add(dir.Multiply(t[maxi]))
o1 := (maxi + 1) % 3
o2 := (maxi + 1) % 3
o2 := (maxi + 2) % 3
if between(pt[o1], min[o1], max[o1]) && between(pt[o2], min[o2], max[o2]) {
ok = true
@ -201,5 +229,6 @@ func (box Boxd) IntersectRay(start, dir Vec3d, length float64) (ok bool, hitface
}
}
ok = false
return
}