From d4669cfdd036eba17354cb1c7fe636e1b7448e4a Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sun, 8 Aug 2021 14:09:28 -0400 Subject: [PATCH] physics test simulation --- physics/physics_test.go | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/physics/physics_test.go b/physics/physics_test.go index a7b4e9f..72a4a72 100644 --- a/physics/physics_test.go +++ b/physics/physics_test.go @@ -2,10 +2,20 @@ package physics_test import ( "testing" + "time" . "github.com/maaslalani/confetty/physics" ) +const fps = 60 + +func simulate(p *Physics, d time.Duration) { + frames := int(d.Seconds() * fps) + for i := 0; i < frames; i++ { + p.Update() + } +} + func TestNew(t *testing.T) { x := 8 y := 20 @@ -21,7 +31,6 @@ func TestNew(t *testing.T) { } func TestUpdate(t *testing.T) { - fps := 60 physics := New(Point{0, 0}, Vector{5, 5}, Vector(Gravity), float64(fps)) // coordinates is the location at which the object should be @@ -37,10 +46,7 @@ func TestUpdate(t *testing.T) { } for _, c := range coordinates { - // 1 second of simulation - for i := 0; i < fps; i++ { - physics.Update() - } + simulate(physics, time.Second) x := physics.PosX() y := physics.PosY() @@ -67,15 +73,10 @@ func TestDisplacement(t *testing.T) { {x: 0, y: 0, vel: Vector{1, 1}, d: 1.414213562373097}, } - fps := 60 - for _, tc := range tt { physics := New(Point{float64(tc.x), float64(tc.y)}, tc.vel, Vector{}, float64(fps)) - // 1 second of simulation - for i := 0; i < fps; i++ { - physics.Update() - } + simulate(physics, time.Second) if physics.Displacement() != tc.d { t.Log(physics.Displacement()) @@ -88,14 +89,10 @@ func TestReset(t *testing.T) { x := 5 y := 10 vel := Vector{20, 20} - fps := 60 physics := New(Point{float64(x), float64(y)}, vel, Vector(Gravity), float64(fps)) - // 1 second of simulation - for i := 0; i < fps; i++ { - physics.Update() - } + simulate(physics, time.Second) // store the current position after 1 second of simulation we will simulate 1 // second again and ensure that the object reaches the same position, we @@ -114,13 +111,10 @@ func TestReset(t *testing.T) { t.Fatal("expected y to be reset") } - // 1 second of simulation // here we are simply checking that the object reaches the same position // after being reset in the same amount of time. This ensures velocity and // acceleration were reset without checking them explicitly - for i := 0; i < fps; i++ { - physics.Update() - } + simulate(physics, time.Second) if physics.PosX() != cx { t.Fatal("expected simulation to be repeatable")