From 93b28ccc04902bd0d33626dd2a7133f508bf8007 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Tue, 16 Jul 2019 21:07:38 -0700 Subject: [PATCH] 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. --- dev-assets/doodads/azulian/azulian-red.js | 8 ---- dev-assets/doodads/buttons/button.js | 4 ++ dev-assets/doodads/buttons/sticky.js | 4 ++ dev-assets/doodads/doors/electric-door.js | 7 ---- dev-assets/doodads/doors/locked-door.js | 13 ++---- dev-assets/doodads/switches/switch.js | 4 ++ dev-assets/doodads/trapdoors/trapdoor.js | 51 +++++++++++++++++------ 7 files changed, 54 insertions(+), 37 deletions(-) diff --git a/dev-assets/doodads/azulian/azulian-red.js b/dev-assets/doodads/azulian/azulian-red.js index 4ff17bc..a138408 100644 --- a/dev-assets/doodads/azulian/azulian-red.js +++ b/dev-assets/doodads/azulian/azulian-red.js @@ -11,24 +11,16 @@ function main() { Self.AddAnimation("walk-left", 100, ["red-wl1", "red-wl2", "red-wl3", "red-wl4"]); Self.AddAnimation("walk-right", 100, ["red-wr1", "red-wr2", "red-wr3", "red-wr4"]); - // var nextTurn = time.Add(time.Now(), 2500); - // Sample our X position every few frames and detect if we've hit a solid wall. var sampleTick = 0; var sampleRate = 5; var lastSampledX = 0; setInterval(function() { - // if (time.Now().After(nextTurn)) { - // direction = direction === "right" ? "left" : "right"; - // nextTurn = time.Add(time.Now(), 2500); - // } - if (sampleTick % sampleRate === 0) { var curX = Self.Position().X; var delta = Math.abs(curX - lastSampledX); if (delta < 5) { - log.Error("flip red azulian"); direction = direction === "right" ? "left" : "right"; } lastSampledX = curX; diff --git a/dev-assets/doodads/buttons/button.js b/dev-assets/doodads/buttons/button.js index 5ae0a17..7e48193 100644 --- a/dev-assets/doodads/buttons/button.js +++ b/dev-assets/doodads/buttons/button.js @@ -4,6 +4,10 @@ function main() { var timer = 0; Events.OnCollide(function(e) { + if (!e.Settled) { + return; + } + // Verify they've touched the button. if (e.Overlap.Y + e.Overlap.H < 24) { return; diff --git a/dev-assets/doodads/buttons/sticky.js b/dev-assets/doodads/buttons/sticky.js index bd29aee..ed4113d 100644 --- a/dev-assets/doodads/buttons/sticky.js +++ b/dev-assets/doodads/buttons/sticky.js @@ -13,6 +13,10 @@ function main() { }) Events.OnCollide(function(e) { + if (!e.Settled) { + return; + } + if (pressed) { return; } diff --git a/dev-assets/doodads/doors/electric-door.js b/dev-assets/doodads/doors/electric-door.js index 7ecdbdb..d424ba3 100644 --- a/dev-assets/doodads/doors/electric-door.js +++ b/dev-assets/doodads/doors/electric-door.js @@ -37,11 +37,4 @@ function main() { } } }); - Events.OnLeave(function() { - // if (opened) { - // Self.PlayAnimation("close", function() { - // opened = false; - // }); - // } - }) } diff --git a/dev-assets/doodads/doors/locked-door.js b/dev-assets/doodads/doors/locked-door.js index 28f6d70..ce91829 100644 --- a/dev-assets/doodads/doors/locked-door.js +++ b/dev-assets/doodads/doors/locked-door.js @@ -2,8 +2,6 @@ function main() { Self.AddAnimation("open", 0, [1]); var unlocked = false; - // Self.Canvas.SetBackground(RGBA(0, 255, 255, 100)); - // Map our door names to key names. var KeyMap = { "Blue Door": "Blue Key", @@ -12,8 +10,6 @@ function main() { "Yellow Door": "Yellow Key" } - // log.Warn("%s loaded!", Self.Doodad.Title); - // console.log("%s Setting hitbox", Self.Doodad.Title); Self.SetHitbox(16, 0, 32, 64); Events.OnCollide(function(e) { @@ -28,11 +24,10 @@ function main() { return false; } - unlocked = true; - Self.PlayAnimation("open", null); + if (e.Settled) { + unlocked = true; + Self.PlayAnimation("open", null); + } } }); - // Events.OnLeave(function(e) { - // console.log("%s has stopped touching %s", e, Self.Doodad.Title) - // }) } diff --git a/dev-assets/doodads/switches/switch.js b/dev-assets/doodads/switches/switch.js index 6a273ee..489aedf 100644 --- a/dev-assets/doodads/switches/switch.js +++ b/dev-assets/doodads/switches/switch.js @@ -14,6 +14,10 @@ function main() { }); Events.OnCollide(function(e) { + if (!e.Settled) { + return; + } + if (collide === false) { state = !state; Message.Publish("power", state); diff --git a/dev-assets/doodads/trapdoors/trapdoor.js b/dev-assets/doodads/trapdoors/trapdoor.js index 7c3571b..374c781 100644 --- a/dev-assets/doodads/trapdoors/trapdoor.js +++ b/dev-assets/doodads/trapdoors/trapdoor.js @@ -18,11 +18,11 @@ function main() { if (direction === "left") { Self.SetHitbox(48, 0, doodadSize, doodadSize); } else if (direction === "right") { - Self.SetHitbox(0, 0, thickness+4, doodadSize); + Self.SetHitbox(0, 0, thickness, doodadSize); } else if (direction === "up") { Self.SetHitbox(0, doodadSize - thickness, doodadSize, doodadSize); } else { // Down, default. - Self.SetHitbox(0, 0, 72, 6); + Self.SetHitbox(0, 0, doodadSize, thickness); } var animationSpeed = 100; @@ -46,18 +46,43 @@ function main() { // Is the actor colliding our solid part? if (e.InHitbox) { // Are they touching our opening side? - if (direction === "left" && (e.Overlap.X+e.Overlap.W) < (doodadSize-thickness)) { - return false; - } else if (direction === "right" && e.Overlap.X > 0) { - return false; - } else if (direction === "up" && (e.Overlap.Y+e.Overlap.H) < doodadSize) { - return false; - } else if (direction === "down" && e.Overlap.Y > 0) { - return false; - } else { - opened = true; - Self.PlayAnimation("open", null); + if (direction === "left") { + if (doodadSize - e.Overlap.X < thickness) { + // Touching the right edge, open the door. + opened = true; + Self.PlayAnimation("open", null); + return; + } + if (e.Overlap.W === doodadSize - thickness) { + return false; + } + } else if (direction === "right") { + if (e.Overlap.X > 0) { + return false; + } else if (e.Settled) { + opened = true; + Self.PlayAnimation("open", null); + } + } else if (direction === "up") { + if (doodadSize - e.Overlap.Y < thickness) { + // Touching the bottom edge, open the door. + opened = true; + Self.PlayAnimation("open", null); + return; + } + if (e.Overlap.H === doodadSize - thickness) { + return false; + } + } else if (direction === "down") { + if (e.Overlap.Y > 0) { + return false; + } else if (e.Settled) { + opened = true; + Self.PlayAnimation("open", null); + } } + + return true; } });