doodle/pkg/physics/vector_test.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

54 lines
1.1 KiB
Go

package physics_test
import (
"testing"
"git.kirsle.net/apps/doodle/pkg/physics"
"git.kirsle.net/go/render"
)
// Test converting points to vectors and back again.
func TestVectorPoint(t *testing.T) {
var tests = []struct {
In render.Point
Mid physics.Vector
Add physics.Vector
Out render.Point
}{
{
In: render.NewPoint(102, 102),
Mid: physics.NewVector(102.0, 102.0),
Out: render.NewPoint(102, 102),
},
{
In: render.NewPoint(64, 128),
Mid: physics.NewVector(64.0, 128.0),
Add: physics.NewVector(0.4, 0.6),
Out: render.NewPoint(64, 129),
},
}
for _, test := range tests {
// Convert point to vector.
v := physics.VectorFromPoint(test.In)
if v != test.Mid {
t.Errorf("Unexpected Vector from Point(%s): wanted %s, got %s",
test.In, test.Mid, v,
)
continue
}
// Add other vector.
v.Add(test.Add)
// Verify output point rounded down correctly.
out := v.ToPoint()
if out != test.Out {
t.Errorf("Unexpected output vector from Point(%s) -> V(%s) + V(%s): wanted %s, got %s",
test.In, test.Mid, test.Add, test.Out, out,
)
continue
}
}
}