Basic entity tracking

This commit is contained in:
Tom
2020-09-19 15:59:03 -07:00
parent 7d1825b7e8
commit 7d122e2f8b
11 changed files with 357 additions and 158 deletions

View File

@ -10,6 +10,8 @@ type MinMax struct {
Min, Max float64
}
// Extends adjusts the bounds of the MinMax. A negative number will reduce the
// minimum bound, whereas a positive number will increase the maximum bound.
func (mm MinMax) Extend(delta float64) MinMax {
if delta < 0 {
return MinMax{
@ -24,6 +26,8 @@ func (mm MinMax) Extend(delta float64) MinMax {
}
}
// Contract reduces both the minimum and maximum bound by the provided amount,
// such that the difference between the bounds decreases for positive values.
func (mm MinMax) Contract(amt float64) MinMax {
return MinMax{
Min: mm.Min + amt,
@ -31,6 +35,8 @@ func (mm MinMax) Contract(amt float64) MinMax {
}
}
// Expand changes the minimum and maximum bounds by the provided amount, such
// that the difference between the bounds increases for positive values.
func (mm MinMax) Expand(amt float64) MinMax {
return MinMax{
Min: mm.Min - amt,
@ -38,6 +44,7 @@ func (mm MinMax) Expand(amt float64) MinMax {
}
}
// Offset adds the provided value to both the minimum and maximum value.
func (mm MinMax) Offset(amt float64) MinMax {
return MinMax{
Min: mm.Min + amt,
@ -51,6 +58,8 @@ type AABB struct {
Block world.BlockStatus
}
// Extend adjusts the minimum (for negative values) or maximum bounds (for
// positive values) by the provided scalar for each dimension.
func (bb AABB) Extend(dx, dy, dz float64) AABB {
return AABB{
X: bb.X.Extend(dx),
@ -60,6 +69,8 @@ func (bb AABB) Extend(dx, dy, dz float64) AABB {
}
}
// Contract reduces the difference between the min/max bounds (for positive
// values) for each dimension.
func (bb AABB) Contract(x, y, z float64) AABB {
return AABB{
X: bb.X.Contract(x),
@ -69,6 +80,8 @@ func (bb AABB) Contract(x, y, z float64) AABB {
}
}
// Expand increases both the minimum and maximum bounds by the provided amount
// (for positive values) for each dimension.
func (bb AABB) Expand(x, y, z float64) AABB {
return AABB{
X: bb.X.Expand(x),
@ -78,6 +91,8 @@ func (bb AABB) Expand(x, y, z float64) AABB {
}
}
// Offset moves both the minimum and maximum bound by the provided value for
// each dimension.
func (bb AABB) Offset(x, y, z float64) AABB {
return AABB{
X: bb.X.Offset(x),

View File

@ -2,7 +2,7 @@ package phy
// Inputs describes the desired movements of the player.
type Inputs struct {
Yaw, Pitch float32
Yaw, Pitch float64
ThrottleX, ThrottleZ float64
}

View File

@ -14,7 +14,8 @@ const (
playerHeight = 1.8
resetVel = 0.003
yawSpeed = 3.0
maxYawChange = 33
maxPitchChange = 22
gravity = 0.08
drag = 0.98
@ -87,7 +88,12 @@ func (s *State) surroundings(query AABB, w World) Surrounds {
return out
}
func (s *State) applyInputs(input Inputs, acceleration, inertia float64) {
func (s *State) applyLookInputs(input Inputs) {
errYaw := math.Min(math.Max(input.Yaw-s.Yaw, -maxYawChange), maxYawChange)
s.Yaw += errYaw
}
func (s *State) applyPosInputs(input Inputs, acceleration, inertia float64) {
speed := math.Sqrt(input.ThrottleX*input.ThrottleX + input.ThrottleZ*input.ThrottleZ)
if speed < 0.01 {
return
@ -111,7 +117,8 @@ func (s *State) Tick(input Inputs, w World) error {
inertia *= slipperiness
acceleration = 0.1 * (0.1627714 / (inertia * inertia * inertia))
}
s.applyInputs(input, acceleration, inertia)
s.applyLookInputs(input)
s.applyPosInputs(input, acceleration, inertia)
// Deadzone velocities when they get too low.
if math.Abs(s.Vel.X) < resetVel {