Added a function to manage movement inside the maze.

Added goal and objective to the test maze.
This commit is contained in:
trotFunky 2019-11-04 00:12:43 +01:00
parent 1591f70a8d
commit 888e8bd7f5
4 changed files with 46 additions and 18 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3 KiB

After

Width:  |  Height:  |  Size: 3 KiB

View file

@ -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+1],
texture.image_data[data_index+2]}; 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);
}
}

View file

@ -7,9 +7,15 @@
#include <Math/Vectors.h> #include <Math/Vectors.h>
#include <DataHandling/Texture.h> #include <DataHandling/Texture.h>
#include <Camera.h>
#include "TileTypes.h"
/// Returns the colour of the pixel corresponding to the current world position, assuming the map starts at (0,0). /// 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); 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 #endif //LABYRINTHE_MOVEMENTMANAGER_H

View file

@ -2,13 +2,13 @@
// Created by trotFunky on 03/11/2019. // Created by trotFunky on 03/11/2019.
// //
#include "Engine.h" #include <Engine.h>
#include "DataHandling/Texture.h" #include "DataHandling/Texture.h"
#include "MovementManager.h" #include "MovementManager.h"
#include "TileTypes.h" #include "TileTypes.h"
static Texture map; 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 units_per_pixel = 2;
static constexpr int map_side = 32; static constexpr int map_side = 32;
static constexpr int corner_pos = (map_side*units_per_pixel); static constexpr int corner_pos = (map_side*units_per_pixel);
@ -21,38 +21,31 @@ void manage_inputs()
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
Vec3d horizontal_movement = OGLE::camera.get_gaze(); Vec3d translation{0,0,0};
horizontal_movement.y = 0;
horizontal_movement.normalize();
Vec3d previous_camera_position = OGLE::camera.get_eyepos();
// TODO : camera_translate function in MovementManager that handles both the correct translation and rollback. // TODO : camera_translate function in MovementManager that handles both the correct translation and rollback.
if (InputStatus::is_special_key_pressed(GLUT_KEY_RIGHT)) 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)) 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)) 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)) 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); Vec3i map_colour = current_map_colour(OGLE::camera.get_eyepos(), map, units_per_pixel);
if (map_colour == wall) if (map_colour == end)
{
// If we are going into a wall, revert
OGLE::camera.set_position(previous_camera_position);
}
else if (map_colour == end)
{ {
std::cout << "Success !" << std::endl; std::cout << "Success !" << std::endl;
glutDestroyWindow(glutGetWindow()); glutDestroyWindow(glutGetWindow());
@ -105,7 +98,7 @@ int main(int argc, char** argv)
{ {
OGLE::setup(argc,argv,display,"The Labyrinth !"); 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}); OGLE::camera.set_position({corner_pos/2,1,corner_pos/2});