Cannot go through walls anymore

Planar movement inside the maze
Fixed orientation
This commit is contained in:
Teo-CD 2019-11-03 21:21:39 +01:00
parent bbe9a7394c
commit 1591f70a8d
6 changed files with 92 additions and 51 deletions

View file

@ -19,7 +19,9 @@ find_package(GLUT REQUIRED)
add_executable(game add_executable(game
src/main.cpp) src/main.cpp
src/MovementManager.h
src/MovementManager.cpp src/TileTypes.h)
target_include_directories(game PRIVATE target_include_directories(game PRIVATE
${OPENGL_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 192 KiB

After

Width:  |  Height:  |  Size: 3 KiB

17
src/MovementManager.cpp Normal file
View file

@ -0,0 +1,17 @@
//
// Created by trotFunky on 03/11/2019.
//
#include "MovementManager.h"
Vec3i current_map_colour(const Vec3d& world_pos, const Texture& texture, const int& pixel_scale)
{
Vec2i map_pos{static_cast<int>(world_pos.x/pixel_scale),
static_cast<int>(world_pos.z/pixel_scale)};
unsigned int data_index = (map_pos.x * texture.height + map_pos.y)*(texture.color_bits/8);
return Vec3i{texture.image_data[data_index],
texture.image_data[data_index+1],
texture.image_data[data_index+2]};
}

15
src/MovementManager.h Normal file
View file

@ -0,0 +1,15 @@
//
// Created by trotFunky on 03/11/2019.
//
#ifndef LABYRINTHE_MOVEMENTMANAGER_H
#define LABYRINTHE_MOVEMENTMANAGER_H
#include <Math/Vectors.h>
#include <DataHandling/Texture.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);
#endif //LABYRINTHE_MOVEMENTMANAGER_H

15
src/TileTypes.h Normal file
View file

@ -0,0 +1,15 @@
//
// Created by trotFunky on 03/11/2019.
//
#ifndef LABYRINTHE_TILETYPES_H
#define LABYRINTHE_TILETYPES_H
#include "Math/Vectors.h"
static const Vec3i path{255,255,255};
static const Vec3i wall{0,0,0};
static const Vec3i end{0,255,0};
static const Vec3i objective{0,0,255};
#endif //LABYRINTHE_TILETYPES_H

View file

@ -4,8 +4,14 @@
#include "Engine.h" #include "Engine.h"
#include "DataHandling/Texture.h" #include "DataHandling/Texture.h"
#include "MovementManager.h"
#include "TileTypes.h"
static Texture map; static Texture map;
static constexpr float horizontal_speed = 0.025;
static constexpr int units_per_pixel = 2;
static constexpr int map_side = 32;
static constexpr int corner_pos = (map_side*units_per_pixel);
void manage_inputs() void manage_inputs()
{ {
@ -15,31 +21,42 @@ void manage_inputs()
exit(EXIT_SUCCESS); 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();
// 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, 0.1}); OGLE::camera.local_translate({0, 0, 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, -0.1}); OGLE::camera.local_translate({0, 0, -horizontal_speed});
}
if (InputStatus::is_key_pressed(' '))
{
OGLE::camera.local_translate({0, 0.1, 0});
}
if (InputStatus::is_special_key_pressed(GLUT_KEY_PAGE_DOWN))
{
OGLE::camera.local_translate({0, -0.1, 0});
} }
if (InputStatus::is_special_key_pressed(GLUT_KEY_UP)) if (InputStatus::is_special_key_pressed(GLUT_KEY_UP))
{ {
OGLE::camera.local_translate({0.1, 0, 0}); OGLE::camera.translate(horizontal_movement * horizontal_speed);
} }
if (InputStatus::is_special_key_pressed(GLUT_KEY_DOWN)) if (InputStatus::is_special_key_pressed(GLUT_KEY_DOWN))
{ {
OGLE::camera.local_translate({-0.1, 0, 0}); OGLE::camera.translate(horizontal_movement * -horizontal_speed);
}
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)
{
std::cout << "Success !" << std::endl;
glutDestroyWindow(glutGetWindow());
exit(EXIT_SUCCESS);
} }
Vec2i mouse_delta = InputStatus::get_mouse_delta(true); Vec2i mouse_delta = InputStatus::get_mouse_delta(true);
@ -55,56 +72,29 @@ void display()
{ {
manage_inputs(); manage_inputs();
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3f(0.5,0,0);
glVertex3i(-1,-10,-1);
glVertex3i(-1,10,-1);
glVertex3i(1,10,-1);
glVertex3i(1,-10,-1);
glColor3f(0.5,0,0);
glVertex3i(-1,-10,-1);
glVertex3i(-1,10,-1);
glVertex3i(-1,10,1);
glVertex3i(-1,-10,1);
glColor3f(0.5,0,0);
glVertex3i(1,-10,1);
glVertex3i(1,10,1);
glVertex3i(1,10,-1);
glVertex3i(1,-10,-1);
glColor3f(0.5,0,0);
glVertex3i(1,-10,1);
glVertex3i(1,10,1);
glVertex3i(-1,10,1);
glVertex3i(-1,-10,1);
glEnd();
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST); glEnable(GL_ALPHA_TEST);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glBindTexture(GL_TEXTURE_2D,map.opengl_id[0]); glBindTexture(GL_TEXTURE_2D,map.opengl_id[0]);
// Deactivate interpolation
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glColor4f(1.0f, 1.0f, 1.0f,1.00); glColor4f(1.0f, 1.0f, 1.0f,1.00);
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0,0); glTexCoord2f(0,0);
glVertex3i(0,0,0); glVertex3i(0,0,0);
glTexCoord2d(0,1); glTexCoord2d(1,0);
glVertex3i(0,0,256); glVertex3i(0,0,corner_pos);
glTexCoord2d(1,1); glTexCoord2d(1,1);
glVertex3i(256,0,256); glVertex3i(corner_pos,0,corner_pos);
glTexCoord2d(1,0); glTexCoord2d(0,1);
glVertex3i(256,0,0); glVertex3i(corner_pos,0,0);
glEnd(); glEnd();
glDisable(GL_ALPHA_TEST); glDisable(GL_ALPHA_TEST);
@ -117,6 +107,8 @@ int main(int argc, char** argv)
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});
glutMainLoop(); glutMainLoop();
return 0; return 0;
} }