feat: moge groups in hyperspace

This commit is contained in:
Ilia Denisov
2026-01-18 22:38:11 +02:00
parent 741a5f726b
commit bd9db26ef4
12 changed files with 273 additions and 20 deletions
+67 -2
View File
@@ -3,6 +3,72 @@ package util
import "math"
func ShortDistance(w, h uint32, x1, y1, x2, y2 float64) float64 {
return math.Hypot(deltas(w, h, x1, y1, x2, y2))
}
func NextTravelCoord(w, h uint32, x1, y1, x2, y2, delta float64) (float64, float64, bool) {
deltaX, deltaY := deltas(w, h, x1, y1, x2, y2)
distance := math.Hypot(deltaX, deltaY)
if distance <= delta {
return x2, y2, true
}
// TODO: refactor - remove extra vars
xa := 0.
ya := 0.
xb := deltaX
yb := deltaY
d := distance
d2 := delta
xc := xa - (d2*(xa-xb))/d
yc := ya - (d2*(ya-yb))/d
// ---
var tx, ty float64
if math.Abs(x2-x1) > float64(w/2) {
// moving across X boundary
if x2 < x1 {
// moving across higher border
tx = math.Mod(x1+xc, float64(w))
} else {
// moving across lower border
tx = x1 - xc
if tx < 0 {
tx = float64(w) + tx
}
}
} else {
if x2 < x1 {
tx = x1 - xc
} else {
tx = x1 + xc
}
}
if math.Abs(y2-y1) > float64(h/2) {
// moving across Y boundary
if y2 < y1 {
// moving across higher border
ty = math.Mod(y1+yc, float64(h))
} else {
// moving across lower border
ty = y1 - yc
if ty < 0 {
ty = float64(h) + ty
}
}
} else {
if y2 < y1 {
ty = y1 - yc
} else {
ty = y1 + yc
}
}
return tx, ty, false
}
func deltas(w, h uint32, x1, y1, x2, y2 float64) (float64, float64) {
dx := math.Abs(x2 - x1)
dy := math.Abs(y2 - y1)
if dx > float64(w/2) {
@@ -11,6 +77,5 @@ func ShortDistance(w, h uint32, x1, y1, x2, y2 float64) float64 {
if dy > float64(h/2) {
dy = float64(h) - dy
}
return math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
return dx, dy
}
+29
View File
@@ -25,3 +25,32 @@ func TestShortDistance(t *testing.T) {
})
}
}
func TestNextTravelCoord(t *testing.T) {
for i, tc := range []struct {
w, h uint32
ox, oy, dx, dy, delta float64
tx, ty float64
arrived bool
}{
{w: 10, h: 10, ox: 0.0, oy: 0.0, dx: 2.0, dy: 0.0, delta: 1.0, tx: 1.0, ty: 0.0, arrived: false},
{w: 10, h: 10, ox: 0.0, oy: 0.0, dx: 0.0, dy: 2.0, delta: 1.0, tx: 0.0, ty: 1.0, arrived: false},
{w: 10, h: 10, ox: 1.0, oy: 1.0, dx: 9.0, dy: 1.0, delta: 1.0, tx: 0.0, ty: 1.0, arrived: false},
{w: 10, h: 10, ox: 1.0, oy: 9.5, dx: 1.0, dy: 1.0, delta: 1.0, tx: 1.0, ty: 0.5, arrived: false},
{w: 10, h: 10, ox: 1.0, oy: 1.0, dx: 5.0, dy: 5.0, delta: 2.0, tx: 2.414, ty: 2.414, arrived: false},
{w: 10, h: 10, ox: 1.0, oy: 1.0, dx: 9.0, dy: 9.0, delta: 2.0, tx: 9.586, ty: 9.586, arrived: false},
{w: 10, h: 10, ox: 5.0, oy: 5.0, dx: 9.0, dy: 9.0, delta: 6.0, tx: 9.0, ty: 9.0, arrived: true},
{w: 10, h: 10, ox: 6.0, oy: 6.0, dx: 10.0, dy: 10.0, delta: 6.0, tx: 10.0, ty: 10.0, arrived: true},
{w: 10, h: 10, ox: 1.0, oy: 2.0, dx: 7.0, dy: 8.0, delta: 6.0, tx: 7.0, ty: 8.0, arrived: true},
} {
t.Run(fmt.Sprint(i), func(t *testing.T) {
tx, ty, arrived := util.NextTravelCoord(tc.w, tc.h, tc.ox, tc.oy, tc.dx, tc.dy, tc.delta)
assert.Equal(t, tc.arrived, arrived)
assert.Equal(t, tc.tx, number.Fixed3(tx))
assert.Equal(t, tc.ty, number.Fixed3(ty))
})
}
}