28 lines
1.3 KiB
Plaintext
28 lines
1.3 KiB
Plaintext
|
|
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.
|
|
}
|
|
}
|
|
}
|