From 7e3106eba0a0158a0f73f7e6d26dffaaed5cbcee Mon Sep 17 00:00:00 2001 From: matixezor Date: Mon, 27 Dec 2021 15:37:23 +0100 Subject: [PATCH] feat: add camera movement boundaries --- grafika_projekt/src/main.cpp | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index 4ea2f2d..5afb3e1 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -83,19 +83,45 @@ float skyboxVertices[] = { }; +bool isInBoundaries(glm::vec3 nextPosition) { + float boundary = 19.5f; + return nextPosition.z > -boundary && nextPosition.z < boundary && nextPosition.y > -boundary && nextPosition.y < boundary && nextPosition.x < boundary && nextPosition.x > -boundary; +} + + void keyboard(unsigned char key, int x, int y) { - float angleSpeed = 10.f; float moveSpeed = 0.1f; + glm::vec3 nextPosition; switch (key) { case 'z': cursorDiff.z -= angleSpeed; break; case 'x': cursorDiff.z += angleSpeed; break; - case 'w': cameraPos += cameraDir * moveSpeed; break; - case 's': cameraPos -= cameraDir * moveSpeed; break; - case 'd': cameraPos += cameraSide * moveSpeed; break; - case 'a': cameraPos -= cameraSide * moveSpeed; break; + case 'w': + nextPosition = cameraPos + (cameraDir * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 's': + nextPosition = cameraPos - (cameraDir * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 'd': + nextPosition = cameraPos + (cameraSide * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 'a': + nextPosition = cameraPos - (cameraSide * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; } } -- 2.20.1