From 2dc30ce2526e7fb496d1acd58a8ea4e70368d701 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 9 Aug 2023 12:38:09 +0900 Subject: [PATCH] Change camera behavior to follow click/touch --- src/screen_trunner.cc | 57 ++++++++++++++++++++++++++++++------------- src/screen_trunner.h | 3 +-- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/screen_trunner.cc b/src/screen_trunner.cc index 45e9c9c..07b98fa 100644 --- a/src/screen_trunner.cc +++ b/src/screen_trunner.cc @@ -27,9 +27,8 @@ TRunnerScreen::TRunnerScreen(std::weak_ptr stack) TEMP_matrix(get_identity_matrix()), camera_pos{0.0F, 4.0F, 4.0F}, camera_target{0.0F, 0.0F, 0.0F}, + mouse_hit{0.0F, 0.0F, 0.0F}, pos_value(0.0F), - mouse_px(0), - mouse_pz(0), idx_hit(SURFACE_UNIT_WIDTH / 2 + (SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) { #ifndef NDEBUG @@ -177,9 +176,6 @@ bool TRunnerScreen::update(float dt) { if (pos_value > PI * 2.0F) { pos_value -= PI * 2.0F; } - camera_pos.x = std::sin(pos_value) * 4.0F; - camera_pos.z = std::cos(pos_value) * 4.0F; - camera_to_targets(dt); if (IsMouseButtonPressed(0)) { float press_x = GetTouchX(); @@ -205,30 +201,58 @@ bool TRunnerScreen::update(float dt) { Vector3 sw{xf - 0.5F, current.sw, zf + 0.5F}; Vector3 se{xf + 0.5F, current.se, zf + 0.5F}; - if (ray_collision_triangle(ray, nw, sw, ne).has_value()) { + if (auto collision = ray_collision_triangle(ray, nw, sw, ne); + collision.has_value()) { idx_hit = idx; #ifndef NDEBUG std::cout << "first: idx_hit set to " << idx_hit << std::endl; #endif + mouse_hit = collision.value(); + camera_target.x = xf; + camera_target.y = + (current.nw + current.ne + current.sw + current.se) / 4.0F; + camera_target.z = zf; + if (idx != SURFACE_UNIT_WIDTH / 2 + + (SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) { + camera_pos = + Vector3Add(Vector3Scale(Vector3Normalize(camera_target), 4.0F), + camera_target); + camera_pos.y += 4.0F; + } else { + camera_pos.x = 0.0F; + camera_pos.y = camera_target.y + 4.0F; + camera_pos.z = 0.0F; + } break; - } else if (ray_collision_triangle(ray, ne, sw, se).has_value()) { + } else if (auto collision = ray_collision_triangle(ray, ne, sw, se); + collision.has_value()) { idx_hit = idx; #ifndef NDEBUG std::cout << "second: idx_hit set to " << idx_hit << std::endl; #endif + mouse_hit = collision.value(); + camera_target.x = xf; + camera_target.y = + (current.nw + current.ne + current.sw + current.se) / 4.0F; + camera_target.z = zf; + if (idx != SURFACE_UNIT_WIDTH / 2 + + (SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) { + camera_pos = + Vector3Add(Vector3Scale(Vector3Normalize(camera_target), 4.0F), + camera_target); + camera_pos.y += 4.0F; + } else { + camera_pos.x = 0.0F; + camera_pos.y = camera_target.y + 4.0F; + camera_pos.z = 0.0F; + } break; } } - - if (!ray_to_xz_plane(ray, mouse_px, mouse_pz)) { - mouse_px = 0.0F; - mouse_pz = 0.0F; -#ifndef NDEBUG - std::cerr << "Ray intersected xz plane out of bounds!\n"; -#endif - } } + camera_to_targets(dt); + return false; } @@ -270,8 +294,7 @@ bool TRunnerScreen::draw() { // Vector3{0.0F, 0.0F, -4.0F}, 1.0F, WHITE); // TODO DEBUG - DrawLine3D(Vector3{0.0F, 3.0F, 0.0F}, Vector3{mouse_px, 0.0F, mouse_pz}, - BLACK); + DrawLine3D(Vector3{0.0F, 3.0F, 0.0F}, mouse_hit, BLACK); EndMode3D(); EndDrawing(); diff --git a/src/screen_trunner.h b/src/screen_trunner.h index abdbeb4..dd2db2b 100644 --- a/src/screen_trunner.h +++ b/src/screen_trunner.h @@ -63,9 +63,8 @@ class TRunnerScreen : public Screen { Matrix TEMP_matrix; Vector3 camera_pos; Vector3 camera_target; + Vector3 mouse_hit; float pos_value; - float mouse_px; - float mouse_pz; unsigned int idx_hit; void camera_to_targets(float dt);