From 04b9c42e0aac478fea6b45ca8d29967c8398dd32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 6 Sep 2018 19:17:31 +0200 Subject: [PATCH] Improve movement --- raycastlib.h | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index ac5438a..a5577da 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -979,34 +979,51 @@ void moveCameraWithCollision(Camera *camera, Vector2D planeOffset, int16_t xSquareNew = divRoundDown(cornerNew.x,UNITS_PER_SQUARE); int16_t ySquareNew = divRoundDown(cornerNew.y,UNITS_PER_SQUARE); - int8_t xCollides = - xSquare != xSquareNew && - floorHeightFunc(xSquareNew,ySquare) > UNITS_PER_SQUARE; + Unit bottomLimit = camera->height - camera->collisionHeightBelow; + Unit topLimit = camera->height + camera->collisionHeightAbove; - int8_t yCollides = - ySquare != ySquareNew && - floorHeightFunc(xSquare,ySquareNew) > UNITS_PER_SQUARE; + int8_t xCollides; + int8_t yCollides; - #define collideHelper(dir)\ + #define collideHelper(dir,s1,s2)\ + if (dir##Square != dir##SquareNew)\ + {\ + Unit height = floorHeightFunc(xSquare##s1,ySquare##s2);\ + if (height > bottomLimit)\ + dir##Collides = true;\ + else if (ceilingHeightFunc != 0)\ + {\ + height = ceilingHeightFunc(xSquare##s1,ySquare##s2);\ + if (height < topLimit)\ + dir##Collides = true;\ + }\ + }\ + else\ + dir##Collides = false;\ + \ if (dir##Collides)\ cornerNew.dir = (dir##Square) * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2 +\ - dir##Dir * (UNITS_PER_SQUARE / 2) - dir##Dir; + dir##Dir * (UNITS_PER_SQUARE / 2) - dir##Dir;\ - collideHelper(x) - collideHelper(y) + collideHelper(x,New,) + collideHelper(y,,New) #undef collideHelper camera->position.x = cornerNew.x - xDir * camera->collisionRadius; camera->position.y = cornerNew.y - yDir * camera->collisionRadius; camera->height += heightOffset; + + xSquareNew = divRoundDown(camera->position.x,UNITS_PER_SQUARE); + ySquareNew = divRoundDown(camera->position.y,UNITS_PER_SQUARE); + + Unit floorHeightNew = floorHeightFunc(xSquareNew,ySquareNew); + Unit ceilingHeightNew = ceilingHeightFunc != 0 ? + ceilingHeightFunc(xSquareNew,ySquareNew) : UNIT_INFINITY; -/* camera->height = clamp(camera->height, - floorHeight2 + camera->collisionHeightBelow, - ceilHeight2 - camera->collisionHeightAbove); -*/ - + floorHeightNew + camera->collisionHeightBelow, + ceilingHeightNew - camera->collisionHeightAbove); } #endif