From af71811731197f77b26061ba8d39d1fef783b9f0 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Thu, 11 Jul 2024 18:05:37 +0300 Subject: [PATCH 1/2] fixed 2D to 3D camera direction offset calculation --- util.cpp | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/util.cpp b/util.cpp index 2b7a846..330937e 100755 --- a/util.cpp +++ b/util.cpp @@ -265,20 +265,34 @@ namespace battleship{ } Vector3 screenToSpace(Vector2 pos){ - Root *root = Root::getSingleton(); - Camera *cam = root->getCamera(); - Vector3 dir = cam->getDirection(), up = cam->getUp(); - Vector3 camPos = cam->getPosition(); - mat4 view = lookAt(vec3(camPos.x, camPos.y, camPos.z), vec3(camPos.x + dir.x, camPos.y + dir.y, camPos.z + dir.z), vec3(up.x, up.y, up.z)); - - float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(), nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane(); - mat4 proj = perspective(radians(fov), width / height, nearPlane, farPlane); + GameManager *gm = GameManager::getSingleton(); + float midWidth = .5 * gm->getWidth(), midHeight = .5 * gm->getHeight(), horOffset, vertOffset; + bool left, up; - mat4 mat = proj * view; - float w = (mat * vec4(0, 0, 0, 1)).w; - vec4 ndcPos = vec4(pos.x * 2.0 / width - 1.0, pos.y * -(2.0 / height) + 1.0, -1, 1); - vec4 spacePos = inverse(mat) * ndcPos; - spacePos = vec4(spacePos.x / spacePos.w, spacePos.y / spacePos.w, spacePos.z / spacePos.w, spacePos.w); - return Vector3(spacePos.x, spacePos.y, spacePos.z); + if(pos.x < midWidth){ + left = true; + horOffset = pos.x / midWidth - 1; + } + else{ + left = false; + horOffset = (pos.x - midWidth) / midWidth; + } + + if(pos.y < midHeight){ + up = true; + vertOffset = 1 - pos.y / midHeight; + } + else{ + up = false; + vertOffset = -(pos.y - midHeight) / midHeight; + } + + Camera *cam = Root::getSingleton()->getCamera(); + float near = cam->getNearPlane(); + float tg = tan(radians(cam->getFov()) / 2), ar = midWidth / midHeight; + float camHeight = near * tg, camWidth = camHeight * ar; + Vector3 posOffset = (cam->getLeft() * camWidth * horOffset + cam->getUp() * camHeight * vertOffset + cam->getDirection() * near); + + return cam->getPosition() + posOffset; } } From acd2450e565d47f783449979c38bb91a5c5e9d2d Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 20 Jul 2024 18:24:28 +0300 Subject: [PATCH 2/2] using lookAt arguments with perpendicular view and up vectors --- inGameAppState.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inGameAppState.cpp b/inGameAppState.cpp index d0678de..c69a406 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -80,7 +80,7 @@ namespace battleship{ Camera *cam = Root::getSingleton()->getCamera(); cam->setPosition(Map::getSingleton()->getSpawnPoint(playerId - 1) + Vector3(1, 1, 1) * configData::CAMERA_DISTANCE); - cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); + cam->lookAt(Vector3(0, -1, 1).norm(), Vector3(0, 1, 1).norm()); mainPlayer = Game::getSingleton()->getPlayer(playerId);