physics test simulation

This commit is contained in:
Maas Lalani 2021-08-08 14:09:28 -04:00
parent b52c6ee718
commit d4669cfdd0
No known key found for this signature in database
GPG Key ID: F53774FA051C052A

View File

@ -2,10 +2,20 @@ package physics_test
import ( import (
"testing" "testing"
"time"
. "github.com/maaslalani/confetty/physics" . "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) { func TestNew(t *testing.T) {
x := 8 x := 8
y := 20 y := 20
@ -21,7 +31,6 @@ func TestNew(t *testing.T) {
} }
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
fps := 60
physics := New(Point{0, 0}, Vector{5, 5}, Vector(Gravity), float64(fps)) physics := New(Point{0, 0}, Vector{5, 5}, Vector(Gravity), float64(fps))
// coordinates is the location at which the object should be // coordinates is the location at which the object should be
@ -37,10 +46,7 @@ func TestUpdate(t *testing.T) {
} }
for _, c := range coordinates { for _, c := range coordinates {
// 1 second of simulation simulate(physics, time.Second)
for i := 0; i < fps; i++ {
physics.Update()
}
x := physics.PosX() x := physics.PosX()
y := physics.PosY() y := physics.PosY()
@ -67,15 +73,10 @@ func TestDisplacement(t *testing.T) {
{x: 0, y: 0, vel: Vector{1, 1}, d: 1.414213562373097}, {x: 0, y: 0, vel: Vector{1, 1}, d: 1.414213562373097},
} }
fps := 60
for _, tc := range tt { for _, tc := range tt {
physics := New(Point{float64(tc.x), float64(tc.y)}, tc.vel, Vector{}, float64(fps)) physics := New(Point{float64(tc.x), float64(tc.y)}, tc.vel, Vector{}, float64(fps))
// 1 second of simulation simulate(physics, time.Second)
for i := 0; i < fps; i++ {
physics.Update()
}
if physics.Displacement() != tc.d { if physics.Displacement() != tc.d {
t.Log(physics.Displacement()) t.Log(physics.Displacement())
@ -88,14 +89,10 @@ func TestReset(t *testing.T) {
x := 5 x := 5
y := 10 y := 10
vel := Vector{20, 20} vel := Vector{20, 20}
fps := 60
physics := New(Point{float64(x), float64(y)}, vel, Vector(Gravity), float64(fps)) physics := New(Point{float64(x), float64(y)}, vel, Vector(Gravity), float64(fps))
// 1 second of simulation simulate(physics, time.Second)
for i := 0; i < fps; i++ {
physics.Update()
}
// store the current position after 1 second of simulation we will simulate 1 // 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 // 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") t.Fatal("expected y to be reset")
} }
// 1 second of simulation
// here we are simply checking that the object reaches the same position // 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 // after being reset in the same amount of time. This ensures velocity and
// acceleration were reset without checking them explicitly // acceleration were reset without checking them explicitly
for i := 0; i < fps; i++ { simulate(physics, time.Second)
physics.Update()
}
if physics.PosX() != cx { if physics.PosX() != cx {
t.Fatal("expected simulation to be repeatable") t.Fatal("expected simulation to be repeatable")