通过 A* 找到路径

This commit is contained in:
kercylan98 2023-06-19 19:46:09 +08:00
parent 1eaa2a672c
commit 61cf79d172
1 changed files with 14 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package navmesh package navmesh
import ( import (
"github.com/kercylan98/minotaur/utils/astar"
"github.com/kercylan98/minotaur/utils/generic" "github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/utils/geometry" "github.com/kercylan98/minotaur/utils/geometry"
"github.com/kercylan98/minotaur/utils/maths" "github.com/kercylan98/minotaur/utils/maths"
@ -23,6 +24,10 @@ type NavMesh[V generic.SignedNumber] struct {
meshShrinkAmount V meshShrinkAmount V
} }
func (slf *NavMesh[V]) Neighbours(node *shape[V]) []*shape[V] {
return node.links
}
// Find 在网格中找到与给定点最近的点。如果该点已经在网格中,这将为您提供该点。如果点在网格之外,这将尝试将此点投影到网格中(直到给定的 maxDistance // Find 在网格中找到与给定点最近的点。如果该点已经在网格中,这将为您提供该点。如果点在网格之外,这将尝试将此点投影到网格中(直到给定的 maxDistance
func (slf *NavMesh[V]) Find(point geometry.Point[V], maxDistance V) (distance V, findPoint geometry.Point[V], findShape geometry.Shape[V]) { func (slf *NavMesh[V]) Find(point geometry.Point[V], maxDistance V) (distance V, findPoint geometry.Point[V], findShape geometry.Shape[V]) {
var minDistance = maxDistance var minDistance = maxDistance
@ -113,10 +118,17 @@ func (slf *NavMesh[V]) FindPath(start, end geometry.Point[V]) (result []geometry
return append(result, start, end) return append(result, start, end)
} }
path := astar.Find[*shape[V], V](slf, startShape, endShape, func(a, b *shape[V]) V {
return geometry.CalcDistance(geometry.DoublePointToCoordinate(a.centroid, b.centroid))
}, func(a, b *shape[V]) V {
return geometry.CalcDistance(geometry.DoublePointToCoordinate(a.centroid, b.centroid))
})
if len(path) == 0 {
return
} }
func (slf *NavMesh[V]) aStar() { path = append([]*shape[V]{startShape}, path...)
} }
func (slf *NavMesh[V]) generateLink() { func (slf *NavMesh[V]) generateLink() {