Get Ascend* movements working reliably

This commit is contained in:
Tom
2020-09-23 15:23:47 -07:00
parent fcdf4bda87
commit fa6f9b72b3
3 changed files with 21 additions and 15 deletions

View File

@ -123,7 +123,7 @@ func (m Movement) BaseCost() float64 {
case TraverseNorth, TraverseSouth, TraverseEast, TraverseWest: case TraverseNorth, TraverseSouth, TraverseEast, TraverseWest:
return 1 return 1
case TraverseNorthWest, TraverseNorthEast, TraverseSouthWest, TraverseSouthEast: case TraverseNorthWest, TraverseNorthEast, TraverseSouthWest, TraverseSouthEast:
return 1.5 return 1.25
case DropNorth, DropSouth, DropEast, DropWest: case DropNorth, DropSouth, DropEast, DropWest:
return 2 return 2

View File

@ -8,6 +8,11 @@ import (
"github.com/beefsack/go-astar" "github.com/beefsack/go-astar"
) )
// Point represents a point in 3D space.
type Point struct {
X, Y, Z float64
}
type V3 struct { type V3 struct {
X, Y, Z int X, Y, Z int
} }
@ -62,7 +67,7 @@ func (t Tile) PathEstimatedCost(to astar.Pather) float64 {
func (t Tile) PathNeighbors() []astar.Pather { func (t Tile) PathNeighbors() []astar.Pather {
possibles := make([]astar.Pather, 0, 8) possibles := make([]astar.Pather, 0, 8)
if t.PathEstimatedCost(Tile{Pos: t.Nav.Start}) > 1200 { if c := t.PathEstimatedCost(Tile{Pos: t.Nav.Start}); c > 8000 {
return nil return nil
} }
@ -88,9 +93,9 @@ func (t Tile) PathNeighbors() []astar.Pather {
return possibles return possibles
} }
func (t Tile) Inputs(dX, dY, dZ float64) Inputs { func (t Tile) Inputs(deltaPos, vel Point) Inputs {
// Sufficient for simple movements. // Sufficient for simple movements.
at := math.Atan2(-dX, -dZ) at := math.Atan2(-deltaPos.X, -deltaPos.Z)
out := Inputs{ out := Inputs{
ThrottleX: math.Sin(at), ThrottleX: math.Sin(at),
ThrottleZ: math.Cos(at), ThrottleZ: math.Cos(at),
@ -98,7 +103,13 @@ func (t Tile) Inputs(dX, dY, dZ float64) Inputs {
switch t.Movement { switch t.Movement {
case AscendNorth, AscendSouth, AscendEast, AscendWest: case AscendNorth, AscendSouth, AscendEast, AscendWest:
out.Jump = math.Sqrt(dX*dX+dZ*dZ) < 1.75 dist2 := math.Sqrt(deltaPos.X*deltaPos.X + deltaPos.Z*deltaPos.Z)
out.Jump = dist2 < 1.75 && deltaPos.Y < -0.81
// Turn off the throttle if we get stuck on the jump.
if dist2 < 1 && deltaPos.Y < 0 && vel.Y == 0 {
out.ThrottleX, out.ThrottleZ = 0, 0
}
out.Yaw = 0 out.Yaw = 0
} }
return out return out

View File

@ -36,16 +36,11 @@ type World interface {
// Surrounds represents the blocks surrounding the player (Y, Z, X). // Surrounds represents the blocks surrounding the player (Y, Z, X).
type Surrounds []AABB type Surrounds []AABB
// Point represents a point in 3D space.
type Point struct {
X, Y, Z float64
}
// State tracks physics state. // State tracks physics state.
type State struct { type State struct {
// player state. // player state.
Pos Point Pos path.Point
Vel Point Vel path.Point
Yaw, Pitch float64 Yaw, Pitch float64
lastJump uint32 lastJump uint32
@ -61,9 +56,9 @@ type State struct {
} }
func (s *State) ServerPositionUpdate(player player.Pos, w World) error { func (s *State) ServerPositionUpdate(player player.Pos, w World) error {
s.Pos = Point{X: player.X, Y: player.Y, Z: player.Z} s.Pos = path.Point{X: player.X, Y: player.Y, Z: player.Z}
s.Yaw, s.Pitch = float64(player.Yaw), float64(player.Pitch) s.Yaw, s.Pitch = float64(player.Yaw), float64(player.Pitch)
s.Vel = Point{} s.Vel = path.Point{}
s.onGround, s.collision.vertical, s.collision.horizontal = false, false, false s.onGround, s.collision.vertical, s.collision.horizontal = false, false, false
s.Run = true s.Run = true
return nil return nil
@ -202,7 +197,7 @@ func (s *State) tickVelocity(input path.Inputs, w World) {
s.Vel.Z *= inertia s.Vel.Z *= inertia
} }
func (s *State) computeCollision(bb, query AABB, w World) (outBB AABB, outVel Point) { func (s *State) computeCollision(bb, query AABB, w World) (outBB AABB, outVel path.Point) {
surroundings := s.surroundings(query, w) surroundings := s.surroundings(query, w)
outVel = s.Vel outVel = s.Vel