From fde99ef76be2c989b7f6462dc4086bd37969b25a Mon Sep 17 00:00:00 2001 From: Alan Youngblood Date: Mon, 2 Aug 2021 17:46:06 -0400 Subject: [PATCH] added Nic's collision pseudocode --- design/collision-PseudoCode.txt | 27 +++++++++++++++++++++++++++ {src => design}/debug.txt | 0 2 files changed, 27 insertions(+) create mode 100644 design/collision-PseudoCode.txt rename {src => design}/debug.txt (100%) diff --git a/design/collision-PseudoCode.txt b/design/collision-PseudoCode.txt new file mode 100644 index 0000000..b3a7954 --- /dev/null +++ b/design/collision-PseudoCode.txt @@ -0,0 +1,27 @@ + +checkCollision(movingRect, moveVector, solidRect) +{ + // Need to make additional similar checks for each side + if (moveVector.x > 0) + { + if (movingRect.xMax < solidRect.xMin && + movingRect.xMax + moveVector.x > solidRect.xMin) + { + xHitRatio = (solidRect.xMin - moveingRect.xMax) / moveVector.x + } + } + // Also find hit ratio of y movement and see which one will hit first if either does. + if (xHitRatio < yHitRatio) + { + yHitPosMin = movingRect.yMin + moveVector.y * xHitRatio + yHitPosMax = movingRect.yMax + moveVector.y * xHitRatio + if (yHitPosMin < solidRect.yMax && + yHitPosMax > solidRect.yMin) + { + // We know here now where we should collide, however if we stop here the collision will feel 'sticky' + // rather than just applying the ratio to the movement vector, we want to instead slide along the collision + output.x = movingRect.xOrigin + moveVector.x * xHitRatio + output.y = movingRect.yOrigin + moveVector.y // Here is where we should split off a new movement vector to check collision again really, since it is a new direction to check, so return the hit position with a new smaller vector to 'finish' the movement. + } + } +} diff --git a/src/debug.txt b/design/debug.txt similarity index 100% rename from src/debug.txt rename to design/debug.txt