doodle/pkg/physics/vector.go
Noah Petherbridge 08e65c32b5 Overhaul the Platformer Physics System
* Player character now experiences acceleration and friction when
  walking around the map!
* Actor position and movement had to be converted from int's
  (render.Point) to float64's to support fine-grained acceleration
  steps.
* Added "physics" package and physics.Vector to be a float64 counterpart
  for render.Point. Vector is used for uix.Actor.Position() for the sake
  of movement math. Vector is flattened back to a render.Point for
  collision purposes, since the levels and hitboxes are pixel-bound.
* Refactor the uix.Actor to no longer extend the doodads.Drawing (so it
  can have a Position that's a Vector instead of a Point). This broke
  some code that expected `.Doodad` to directly reference the
  Drawing.Doodad: now you had to refer to it as `a.Drawing.Doodad` which
  was ugly. Added convenience method .Doodad() for a shortcut.
* Moved functions like GetBoundingRect() from doodads package to
  collision, where it uses its own slimmer Actor interface for just the
  relevant methods it needs.
2020-04-04 21:00:32 -07:00

55 lines
982 B
Go

package physics
import (
"fmt"
"math"
"git.kirsle.net/go/render"
)
// Vector holds floating point values on an X and Y coordinate.
type Vector struct {
X float64
Y float64
}
// NewVector creates a Vector from X and Y values.
func NewVector(x, y float64) Vector {
return Vector{
X: x,
Y: y,
}
}
// VectorFromPoint converts a render.Point into a vector.
func VectorFromPoint(p render.Point) Vector {
return Vector{
X: float64(p.X),
Y: float64(p.Y),
}
}
// IsZero returns if the vector is zero.
func (v Vector) IsZero() bool {
return v.X == 0 && v.Y == 0
}
// Add to the vector.
func (v *Vector) Add(other Vector) {
v.X += other.X
v.Y += other.Y
}
// ToPoint converts the vector into a render.Point with integer coordinates.
func (v Vector) ToPoint() render.Point {
return render.Point{
X: int(math.Round(v.X)),
Y: int(math.Round(v.Y)),
}
}
// String encoding of the vector.
func (v Vector) String() string {
return fmt.Sprintf("%f,%f", v.X, v.Y)
}