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.
This commit is contained in:
Noah 2019-07-16 21:07:38 -07:00
parent 1c5cfa4ddb
commit 93b28ccc04
7 changed files with 54 additions and 37 deletions

View File

@ -11,24 +11,16 @@ function main() {
Self.AddAnimation("walk-left", 100, ["red-wl1", "red-wl2", "red-wl3", "red-wl4"]); 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"]); 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. // Sample our X position every few frames and detect if we've hit a solid wall.
var sampleTick = 0; var sampleTick = 0;
var sampleRate = 5; var sampleRate = 5;
var lastSampledX = 0; var lastSampledX = 0;
setInterval(function() { setInterval(function() {
// if (time.Now().After(nextTurn)) {
// direction = direction === "right" ? "left" : "right";
// nextTurn = time.Add(time.Now(), 2500);
// }
if (sampleTick % sampleRate === 0) { if (sampleTick % sampleRate === 0) {
var curX = Self.Position().X; var curX = Self.Position().X;
var delta = Math.abs(curX - lastSampledX); var delta = Math.abs(curX - lastSampledX);
if (delta < 5) { if (delta < 5) {
log.Error("flip red azulian");
direction = direction === "right" ? "left" : "right"; direction = direction === "right" ? "left" : "right";
} }
lastSampledX = curX; lastSampledX = curX;

View File

@ -4,6 +4,10 @@ function main() {
var timer = 0; var timer = 0;
Events.OnCollide(function(e) { Events.OnCollide(function(e) {
if (!e.Settled) {
return;
}
// Verify they've touched the button. // Verify they've touched the button.
if (e.Overlap.Y + e.Overlap.H < 24) { if (e.Overlap.Y + e.Overlap.H < 24) {
return; return;

View File

@ -13,6 +13,10 @@ function main() {
}) })
Events.OnCollide(function(e) { Events.OnCollide(function(e) {
if (!e.Settled) {
return;
}
if (pressed) { if (pressed) {
return; return;
} }

View File

@ -37,11 +37,4 @@ function main() {
} }
} }
}); });
Events.OnLeave(function() {
// if (opened) {
// Self.PlayAnimation("close", function() {
// opened = false;
// });
// }
})
} }

View File

@ -2,8 +2,6 @@ function main() {
Self.AddAnimation("open", 0, [1]); Self.AddAnimation("open", 0, [1]);
var unlocked = false; var unlocked = false;
// Self.Canvas.SetBackground(RGBA(0, 255, 255, 100));
// Map our door names to key names. // Map our door names to key names.
var KeyMap = { var KeyMap = {
"Blue Door": "Blue Key", "Blue Door": "Blue Key",
@ -12,8 +10,6 @@ function main() {
"Yellow Door": "Yellow Key" "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); Self.SetHitbox(16, 0, 32, 64);
Events.OnCollide(function(e) { Events.OnCollide(function(e) {
@ -28,11 +24,10 @@ function main() {
return false; return false;
} }
if (e.Settled) {
unlocked = true; unlocked = true;
Self.PlayAnimation("open", null); Self.PlayAnimation("open", null);
} }
}
}); });
// Events.OnLeave(function(e) {
// console.log("%s has stopped touching %s", e, Self.Doodad.Title)
// })
} }

View File

@ -14,6 +14,10 @@ function main() {
}); });
Events.OnCollide(function(e) { Events.OnCollide(function(e) {
if (!e.Settled) {
return;
}
if (collide === false) { if (collide === false) {
state = !state; state = !state;
Message.Publish("power", state); Message.Publish("power", state);

View File

@ -18,11 +18,11 @@ function main() {
if (direction === "left") { if (direction === "left") {
Self.SetHitbox(48, 0, doodadSize, doodadSize); Self.SetHitbox(48, 0, doodadSize, doodadSize);
} else if (direction === "right") { } else if (direction === "right") {
Self.SetHitbox(0, 0, thickness+4, doodadSize); Self.SetHitbox(0, 0, thickness, doodadSize);
} else if (direction === "up") { } else if (direction === "up") {
Self.SetHitbox(0, doodadSize - thickness, doodadSize, doodadSize); Self.SetHitbox(0, doodadSize - thickness, doodadSize, doodadSize);
} else { // Down, default. } else { // Down, default.
Self.SetHitbox(0, 0, 72, 6); Self.SetHitbox(0, 0, doodadSize, thickness);
} }
var animationSpeed = 100; var animationSpeed = 100;
@ -46,18 +46,43 @@ function main() {
// Is the actor colliding our solid part? // Is the actor colliding our solid part?
if (e.InHitbox) { if (e.InHitbox) {
// Are they touching our opening side? // Are they touching our opening side?
if (direction === "left" && (e.Overlap.X+e.Overlap.W) < (doodadSize-thickness)) { 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; return false;
} else if (direction === "right" && e.Overlap.X > 0) { }
} else if (direction === "right") {
if (e.Overlap.X > 0) {
return false; return false;
} else if (direction === "up" && (e.Overlap.Y+e.Overlap.H) < doodadSize) { } else if (e.Settled) {
return false;
} else if (direction === "down" && e.Overlap.Y > 0) {
return false;
} else {
opened = true; opened = true;
Self.PlayAnimation("open", null); 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;
} }
}); });