doodle/pkg/collision/actors_test.go

126 lines
1.9 KiB
Go
Raw Permalink Normal View History

2019-04-16 03:19:52 +00:00
package collision_test
import (
"testing"
2022-09-24 22:17:25 +00:00
"git.kirsle.net/SketchyMaze/doodle/pkg/collision"
"git.kirsle.net/go/render"
2019-04-16 03:19:52 +00:00
)
func TestActorCollision(t *testing.T) {
boxes := []render.Rect{
// 0: intersects with 1
// Expected intersection rect would be
// X,Y = 90,10
// X2,Y2 = 100,99
{
2019-04-16 03:19:52 +00:00
X: 0,
Y: 0,
W: 100,
H: 100,
},
// 1: intersects with 0
// Expected intersection rect would be
// X,Y = 90,10
// X2,Y2 = 100,99
{
2019-04-16 03:19:52 +00:00
X: 90,
Y: 10,
W: 100,
H: 100,
},
// 2: no intersection
{
2019-04-16 03:19:52 +00:00
X: 200,
Y: 200,
W: 32,
H: 32,
},
// 3: intersects with 4
// Expected intersection rect would be
// X,Y = 240,200
// X2,Y2 = 264,231
{
2019-04-16 03:19:52 +00:00
X: 233,
Y: 200,
W: 32,
H: 32,
},
// 4: intersects with 3
{
2019-04-16 03:19:52 +00:00
X: 240,
Y: 200,
W: 32,
H: 32,
},
// 5: completely contains 6 and intersects 7.
{
2019-04-16 03:19:52 +00:00
X: 300,
Y: 300,
W: 1000,
H: 600,
},
{
2019-04-16 03:19:52 +00:00
X: 450,
Y: 500,
W: 42,
H: 42,
},
{
2019-04-16 03:19:52 +00:00
X: 1200,
Y: 350,
W: 512,
H: 512,
},
}
assert := func(i int, result collision.BoxCollision, expectA, expectB int) {
if result.A != expectA || result.B != expectB {
2019-04-16 03:19:52 +00:00
t.Errorf(
"unexpected collision at index %d of BetweenBoxes() generator\n"+
"expected: (%d,%d)\n"+
" but got: (%d,%d)",
i,
expectA, expectB,
result.A, result.B,
2019-04-16 03:19:52 +00:00
)
}
}
var i int
for overlap := range collision.BetweenBoxes(boxes) {
a, b := overlap.A, overlap.B
2019-04-16 03:19:52 +00:00
// Ensure expected collisions happened.
switch i {
case 0:
assert(i, overlap, 0, 1)
case 1:
Improve Collision Detection: More Active w/ Actors * Improve the collision detection algorithm so that Actor OnCollide scripts get called more often WHILE an actor is moving, to prevent a fast-moving actor from zipping right through the "solid" hitbox and not giving the subject actor time to protest the movement. * It's implemented by adding a `Settled` boolean to the OnCollide event object. When the game is testing out movement, Settled=false to give the actor a chance to say "I'm solid!" and have the moving party be stopped early. * After all this is done, for any pair of actors still with overlapping hitboxes, OnCollide is called one last time with Settled=true. This is when the actor should run its actions (like publishing messages to other actors, changing state as in a trapdoor, etc.) * The new collision detection algorithm works as follows: * Stage 1 is the same as before, all mobile actors are moved and tested against level geometry. They record their Original and New position during this phase. * Stage 2 is where we re-run that movement but ping actors being intersected each step of the way. We trace the steps between Original and New position, test OnCollide handler, and if it returns false we move the mobile actor to the Last Good Position along the trace. * Stage 3 we run the final OnCollide(Settled=true) to let actors run actions they wanted to for their collide handler, WITHOUT spamming those actions during Stage 2. * This should now allow for tweaking of gravity speed and player speed without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
assert(i, overlap, 1, 0)
2019-04-16 03:19:52 +00:00
case 2:
Improve Collision Detection: More Active w/ Actors * Improve the collision detection algorithm so that Actor OnCollide scripts get called more often WHILE an actor is moving, to prevent a fast-moving actor from zipping right through the "solid" hitbox and not giving the subject actor time to protest the movement. * It's implemented by adding a `Settled` boolean to the OnCollide event object. When the game is testing out movement, Settled=false to give the actor a chance to say "I'm solid!" and have the moving party be stopped early. * After all this is done, for any pair of actors still with overlapping hitboxes, OnCollide is called one last time with Settled=true. This is when the actor should run its actions (like publishing messages to other actors, changing state as in a trapdoor, etc.) * The new collision detection algorithm works as follows: * Stage 1 is the same as before, all mobile actors are moved and tested against level geometry. They record their Original and New position during this phase. * Stage 2 is where we re-run that movement but ping actors being intersected each step of the way. We trace the steps between Original and New position, test OnCollide handler, and if it returns false we move the mobile actor to the Last Good Position along the trace. * Stage 3 we run the final OnCollide(Settled=true) to let actors run actions they wanted to for their collide handler, WITHOUT spamming those actions during Stage 2. * This should now allow for tweaking of gravity speed and player speed without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
assert(i, overlap, 3, 4)
2019-04-16 03:19:52 +00:00
case 3:
Improve Collision Detection: More Active w/ Actors * Improve the collision detection algorithm so that Actor OnCollide scripts get called more often WHILE an actor is moving, to prevent a fast-moving actor from zipping right through the "solid" hitbox and not giving the subject actor time to protest the movement. * It's implemented by adding a `Settled` boolean to the OnCollide event object. When the game is testing out movement, Settled=false to give the actor a chance to say "I'm solid!" and have the moving party be stopped early. * After all this is done, for any pair of actors still with overlapping hitboxes, OnCollide is called one last time with Settled=true. This is when the actor should run its actions (like publishing messages to other actors, changing state as in a trapdoor, etc.) * The new collision detection algorithm works as follows: * Stage 1 is the same as before, all mobile actors are moved and tested against level geometry. They record their Original and New position during this phase. * Stage 2 is where we re-run that movement but ping actors being intersected each step of the way. We trace the steps between Original and New position, test OnCollide handler, and if it returns false we move the mobile actor to the Last Good Position along the trace. * Stage 3 we run the final OnCollide(Settled=true) to let actors run actions they wanted to for their collide handler, WITHOUT spamming those actions during Stage 2. * This should now allow for tweaking of gravity speed and player speed without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
assert(i, overlap, 4, 3)
case 4:
assert(i, overlap, 5, 6)
case 5:
2019-04-16 03:19:52 +00:00
assert(i, overlap, 5, 7)
Improve Collision Detection: More Active w/ Actors * Improve the collision detection algorithm so that Actor OnCollide scripts get called more often WHILE an actor is moving, to prevent a fast-moving actor from zipping right through the "solid" hitbox and not giving the subject actor time to protest the movement. * It's implemented by adding a `Settled` boolean to the OnCollide event object. When the game is testing out movement, Settled=false to give the actor a chance to say "I'm solid!" and have the moving party be stopped early. * After all this is done, for any pair of actors still with overlapping hitboxes, OnCollide is called one last time with Settled=true. This is when the actor should run its actions (like publishing messages to other actors, changing state as in a trapdoor, etc.) * The new collision detection algorithm works as follows: * Stage 1 is the same as before, all mobile actors are moved and tested against level geometry. They record their Original and New position during this phase. * Stage 2 is where we re-run that movement but ping actors being intersected each step of the way. We trace the steps between Original and New position, test OnCollide handler, and if it returns false we move the mobile actor to the Last Good Position along the trace. * Stage 3 we run the final OnCollide(Settled=true) to let actors run actions they wanted to for their collide handler, WITHOUT spamming those actions during Stage 2. * This should now allow for tweaking of gravity speed and player speed without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
case 6:
assert(i, overlap, 6, 5)
case 7:
assert(i, overlap, 7, 5)
2019-04-16 03:19:52 +00:00
default:
t.Errorf("got unexpected collision result, index %d, tuple (%d,%d)",
i, a, b,
)
}
i++
}
}