diff --git a/resources/Map.tga b/resources/Map.tga index 3dbea17..cc1e70e 100644 Binary files a/resources/Map.tga and b/resources/Map.tga differ diff --git a/src/MovementManager.cpp b/src/MovementManager.cpp index 752cb42..73f08ae 100644 --- a/src/MovementManager.cpp +++ b/src/MovementManager.cpp @@ -15,3 +15,32 @@ Vec3i current_map_colour(const Vec3d& world_pos, const Texture& texture, const i texture.image_data[data_index+1], texture.image_data[data_index+2]}; } + +void constrained_translation(const Vec3d& translation, Camera& camera, const Texture& map, const int& pixel_scale) +{ + Vec3d original_position = camera.get_eyepos(); + + Vec3d forward_direction = camera.get_gaze(); + forward_direction.y = 0; + forward_direction.normalize(); + + // Forward and up movements are global, disconnects with viewing pitch. + if (translation.x != 0 || translation.y != 0) + { + forward_direction = forward_direction * translation.x; + forward_direction.y = translation.y; + camera.translate(forward_direction); + } + + // Strafing movement is relative local. + if (translation.z != 0) + { + camera.local_translate({0,0,translation.z}); + } + + if (current_map_colour(camera.get_eyepos(), map, pixel_scale) == wall) + { + // If we are going into a wall, revert + camera.set_position(original_position); + } +} diff --git a/src/MovementManager.h b/src/MovementManager.h index 2735c12..f4c5b77 100644 --- a/src/MovementManager.h +++ b/src/MovementManager.h @@ -7,9 +7,15 @@ #include #include +#include + +#include "TileTypes.h" /// Returns the colour of the pixel corresponding to the current world position, assuming the map starts at (0,0). Vec3i current_map_colour(const Vec3d& world_pos, const Texture& texture, const int& pixel_scale); +/// Translates the camera inside the maze and check for collisions +/// \param translation Translation that will be executed in the horizontal plane, x forward, z strafe. +void constrained_translation(const Vec3d& translation, Camera& camera, const Texture& map, const int& pixel_scale); #endif //LABYRINTHE_MOVEMENTMANAGER_H diff --git a/src/main.cpp b/src/main.cpp index 2bda63a..e2b9849 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,13 +2,13 @@ // Created by trotFunky on 03/11/2019. // -#include "Engine.h" +#include #include "DataHandling/Texture.h" #include "MovementManager.h" #include "TileTypes.h" static Texture map; -static constexpr float horizontal_speed = 0.025; +static constexpr float horizontal_speed = 0.05; static constexpr int units_per_pixel = 2; static constexpr int map_side = 32; static constexpr int corner_pos = (map_side*units_per_pixel); @@ -21,38 +21,31 @@ void manage_inputs() exit(EXIT_SUCCESS); } - Vec3d horizontal_movement = OGLE::camera.get_gaze(); - horizontal_movement.y = 0; - horizontal_movement.normalize(); - - Vec3d previous_camera_position = OGLE::camera.get_eyepos(); + Vec3d translation{0,0,0}; // TODO : camera_translate function in MovementManager that handles both the correct translation and rollback. if (InputStatus::is_special_key_pressed(GLUT_KEY_RIGHT)) { - OGLE::camera.local_translate({0, 0, horizontal_speed}); + translation.z += horizontal_speed; } if (InputStatus::is_special_key_pressed(GLUT_KEY_LEFT)) { - OGLE::camera.local_translate({0, 0, -horizontal_speed}); + translation.z -= horizontal_speed; } if (InputStatus::is_special_key_pressed(GLUT_KEY_UP)) { - OGLE::camera.translate(horizontal_movement * horizontal_speed); + translation.x += horizontal_speed; } if (InputStatus::is_special_key_pressed(GLUT_KEY_DOWN)) { - OGLE::camera.translate(horizontal_movement * -horizontal_speed); + translation.x -= horizontal_speed; } + constrained_translation(translation,OGLE::camera,map,units_per_pixel); + Vec3i map_colour = current_map_colour(OGLE::camera.get_eyepos(), map, units_per_pixel); - if (map_colour == wall) - { - // If we are going into a wall, revert - OGLE::camera.set_position(previous_camera_position); - } - else if (map_colour == end) + if (map_colour == end) { std::cout << "Success !" << std::endl; glutDestroyWindow(glutGetWindow()); @@ -105,7 +98,7 @@ int main(int argc, char** argv) { OGLE::setup(argc,argv,display,"The Labyrinth !"); - map.load_rgb_tga("resources/map.tga"); + map.load_rgb_tga("resources/Map.tga"); OGLE::camera.set_position({corner_pos/2,1,corner_pos/2});