Feature/movement character into develop #3

Merged
SemvdH merged 11 commits from feature/MovementCharacter into develop 2021-06-11 08:16:39 +00:00
13 changed files with 21883 additions and 60 deletions

21593
res/beeTwo.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,40 +1,44 @@
#include "collision_handler.h"
#include <iostream>
namespace collision
{
void CheckCollisions(std::vector<entities::CollisionEntity*>& entities)
void CheckCollisions(std::vector<std::shared_ptr<entities::CollisionEntity>> entities)
{
if (entities.size() == 2)
{
if (entities[0]->IsColliding(*entities[1]))
if (entities.size() < 2) { return; }
if (entities.size() == 2)
{
collision::Collision c = { *entities[0], *entities[1] };
entities[0]->OnCollide(c);
entities[1]->OnCollide(c);
if (entities[0]->IsColliding(*entities[1]))
{
collision::Collision c = { *entities[0], *entities[1] };
entities[0]->OnCollide(c);
entities[1]->OnCollide(c);
}
}
}
for (int i = 0; i < entities.size() - 2; i++)
{
entities::CollisionEntity* entity = entities[i];
for (int j = i + 1; i < entities.size() - 1; j++)
{
entities::CollisionEntity* entity2 = entities[j];
if (entity == entity2)
{
continue;
}
if (entity->IsColliding(*entity2))
{
collision::Collision c = { *entity, *entity2 };
entity->OnCollide(c);
entity2->OnCollide(c);
break;
}
}
}
for (int i = 0; i < entities.size() - 2; i++)
{
std::shared_ptr<entities::CollisionEntity> entity = entities[i];
for (int j = i + 1; j < entities.size() - 1; j++)
{
std::shared_ptr<entities::CollisionEntity> entity2 = entities[j];
if (entity == entity2)
{
continue;
}
if (entity->IsColliding(*entity2))
{
collision::Collision c = { *entity, *entity2 };
entity->OnCollide(c);
entity2->OnCollide(c);
break;
}
}
}
}
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <memory>
#include <vector>
#include "../entities/collision_entity.h"
#include "collision.h"
@@ -12,5 +13,5 @@ namespace collision
*
* @param entities: A list with all the collision entities.
*/
void CheckCollisions(std::vector<entities::CollisionEntity*>& entities);
void CheckCollisions(std::vector<std::shared_ptr<entities::CollisionEntity>> entities);
}

View File

@@ -1,4 +1,6 @@
#include "camera.h"
#include <iostream>
#include "../toolbox/toolbox.h"
namespace entities
{
@@ -7,9 +9,20 @@ namespace entities
rotation(rotation)
{}
void Camera::Follow(glm::vec3 follow_position) {
//set position to follow in front of the camera
follow_position.z += 100;
//set position to follow a bit lower
follow_position.y += 50;
//move position from original position to given position with smoothing
position = toolbox::Lerp(position, follow_position, 0.1);
}
void Camera::Move(GLFWwindow* window)
{
float movement_speed = 0;
float up_down_speed = 0;
float side_speed = 0;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
@@ -23,30 +36,25 @@ namespace entities
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
rotation.y += ROT_SPEED;
side_speed += SPEED;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
rotation.y -= ROT_SPEED;
side_speed -= SPEED;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
//rotation.x -= ROT_SPEED;
position.y += 5;
up_down_speed += UP_SPEED;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
//rotation.x += ROT_SPEED;
position.y -= 5;
up_down_speed -= UP_SPEED;
}
float dx = glm::cos(glm::radians(rotation.y + 90)) * movement_speed;
float dz = glm::sin(glm::radians(rotation.y + 90)) * movement_speed;
position.x += dx;
position.z += dz;
position.x += side_speed;
position.z += movement_speed;
position.y += up_down_speed;
}
}

View File

@@ -13,13 +13,14 @@ namespace entities
{
private:
// The movement speed of the camera
const float SPEED = 2.0f;
const float ROT_SPEED = 1.0f;
const float SPEED = 0.5f;
const float UP_SPEED = 1.0f;
glm::vec3 position;
glm::vec3 rotation;
public:
Camera(const ::glm::vec3& position, const ::glm::vec3& rotation);
/*
@@ -28,6 +29,13 @@ namespace entities
* @param window: The OpenGL window
*/
void Move(GLFWwindow* window);
/*
* @brief follows the given position with smoothing
*
* @param follow_position the position of the object the camera has to follow
*/
void Follow(glm::vec3 follow_position);
inline glm::vec3 GetPosition() const{ return position; }
inline glm::vec3 GetRotation() const{ return rotation; }

View File

@@ -14,7 +14,9 @@ namespace entities
{
if (on_collide != nullptr)
{
on_collide(collision);
on_collide(collision);
}
}

View File

@@ -2,6 +2,8 @@
#include "entity.h"
#include "../collision/collision.h"
#include <memory>
#include <functional>
namespace entities
{
@@ -16,7 +18,8 @@ namespace entities
glm::vec3 min_xyz;
glm::vec3 max_xyz;
void (*on_collide)(const collision::Collision& collision);
//void (*on_collide)(const collision::Collision& collision);
std::function<void(const collision::Collision&)> on_collide;
public:
CollisionEntity(const models::TexturedModel& model, const glm::vec3& position, const glm::vec3& rotation,
@@ -52,7 +55,7 @@ namespace entities
*
* @param function: A function pointer to a function with the collision behaviour
*/
void SetCollisionBehaviour(void (*function)(const collision::Collision& collision))
void SetCollisionBehaviour(std::function<void(const collision::Collision&)> function)
{ if (function != nullptr) { on_collide = function; } }
protected:

View File

@@ -0,0 +1,100 @@
#include "main_character.h"
#include "../models/Model.h"
#include <iostream>
#include "entity.h"
#include"../renderEngine/Renderer.h"
#include"../renderEngine/obj_loader.h"
#include"../renderEngine/loader.h"
namespace entities
{
float movement_speed;
float down_speed;
float side_speed;
bool is_playing;
MainCharacter::MainCharacter(const models::TexturedModel& model, const glm::vec3& position,
const glm::vec3& rotation, float scale, const collision::Box& bounding_box)
: CollisionEntity(model, position, rotation, scale, bounding_box)
{
is_playing = true;
}
void MainCharacter::Move(GLFWwindow* window)
{
if (is_playing) {
movement_speed = -0.5f; //Forward speed adjustment, bee is moving at a standard speedrate
down_speed = -1.0f; //Down speed adjustment, downspeed is difference between down_speed and UP_SPEED
side_speed = 0; //Side speed adjustment
//For gameplay with use of keyboard keys: W, A, S, D
//W: Go forward
//A: Go left
//S: Go backwards
//D: Go right
//TODO Implement CV actions
SetRotation(glm::vec3(0, 90, 0));
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
movement_speed -= SIDE_SPEED;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
movement_speed += SIDE_SPEED;
}
//top right
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
{
side_speed += SIDE_SPEED;
down_speed += UP_SPEED;
}
//right
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
side_speed += SIDE_SPEED;
}
//top left
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
{
down_speed += UP_SPEED;
side_speed -= SIDE_SPEED;
}
//left
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
side_speed -= SIDE_SPEED;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
down_speed += UP_SPEED;
SetRotation(glm::vec3(10, 90, 0));
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
down_speed -= UP_SPEED;
}
}
IncreasePosition(glm::vec3(side_speed, down_speed, movement_speed));
//Use only for binding bee to house, such that it doesn't go outside of the room.
//TODO delete when boundingbox is implemented!
if (position.x > 190) position.x = 190;
else if (position.x < -190) position.x = -190;
if (position.y > 350) position.y = 350;
else if (position.y < -40) position.y = -40;
//Move player bounding box according to the position on screen
MoveCollisionBox();
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
{
is_playing = true;
}
}
void MainCharacter::OnCollide(const collision::Collision& collision) {
down_speed = -2.0f;
movement_speed = 0.0f;
is_playing = false;
std::cout << "collision" << std::endl;
}
}

View File

@@ -0,0 +1,38 @@
#pragma once
#include "collision_entity.h"
#include "../shaders/entity_shader.h"
namespace entities
{
/*
* This class contains the information about the player model
*/
class MainCharacter : public CollisionEntity {
const float SIDE_SPEED = 0.8f; //Standard movement speed for left/right movement
const float UP_SPEED = 2.0f; //Standard movement speed for up movement
public:
/*
* @brief: Constructor for the main character model
*
* @param model: Model to load in as the player model
* @param position: Position of the model inside the game window
* @param rotation: Rotation of the model inside the game window
* @param scale: Size of the model
* @param bounding_box: Collision box around the player model
*/
MainCharacter(const models::TexturedModel& model, const glm::vec3& position,
const glm::vec3& rotation, float scale, const collision::Box& bounding_box);
/*
* @brief: A function to move the character inside the window
*
* @param window: The game window
*
* @return: Vector with the adjusted side_speed, down_speed, and movement_speed
*/
void Move(GLFWwindow* window);
void OnCollide(const collision::Collision& collision) override;
};
}

View File

@@ -1,8 +1,11 @@
#include <iostream>
#include <memory>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "in_Game_Scene.h"
#include "startup_Scene.h"
#include "../entities/main_character.h"
#include "../collision/collision_handler.h"
#include "../gui/gui_interactable.h"
#include "../models/model.h"
#include "../renderEngine/loader.h"
@@ -26,12 +29,13 @@
namespace scene
{
std::shared_ptr<entities::MainCharacter>main_character;
std::deque<entities::Light> lights;
std::vector<std::shared_ptr<entities::CollisionEntity>> collision_entities;
entities::HouseGenerator* house_generator;
std::deque<std::shared_ptr<entities::Entity>> house_models;
std::deque<entities::Light> lights;
std::deque<std::shared_ptr<entities::Entity>> trees;
models::RawModel raw_model;
models::RawModel raw_model, raw_model_char;
models::ModelTexture texture;
shaders::EntityShader* shader;
shaders::GuiShader* gui_shader;
@@ -51,6 +55,18 @@ namespace scene
gui_shader = new shaders::GuiShader();
gui_shader->Init();
}
/**
* temporary!!!!
* just to make some bounding boxes
*/
collision::Box create_bounding_box(glm::vec3 size, glm::vec3 pos, int scale) {
collision::Box box = collision::Box();
box.size.x = size.z* scale;
box.size.y = size.y* scale;
box.size.z = size.x* scale;
box.center_pos = pos;
return box;
}
In_Game_Scene::~In_Game_Scene()
{
@@ -91,10 +107,21 @@ namespace scene
house_models.insert(house_models.end(), furniture.begin(), furniture.end());
}
scene::Scenes scene::In_Game_Scene::start(GLFWwindow* window)
{
texture = { render_engine::loader::LoadTexture("res/Texture.png") };
texture.shine_damper = 10;
texture.reflectivity = 0;
raw_model_char = render_engine::LoadObjModel("res/beeTwo.obj");
models::TexturedModel model_char = { raw_model_char, texture };
collision::Box char_box = create_bounding_box(raw_model_char.model_size, glm::vec3(0, 0, 0), 1);
main_character = std::make_shared<entities::MainCharacter>(model_char, glm::vec3(0, -50, -100), glm::vec3(0, 90, 0), 5, char_box);
collision_entities.push_back(main_character);
house_generator = new entities::HouseGenerator();
// load the first few house models
for (int i = 0; i <= UPCOMING_MODEL_AMOUNT; i++)
@@ -102,7 +129,7 @@ namespace scene
load_chunk(i);
}
lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); // sun
lights.push_back(entities::Light(glm::vec3(0, 1000, 7000), glm::vec3(5, 5, 5))); // sun
lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f)));
lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f)));
@@ -146,11 +173,8 @@ namespace scene
{
render_engine::renderer::Render(model_entity, *shader);
}
for (std::shared_ptr<entities::Entity> tree_entity : trees)
{
render_engine::renderer::Render(tree_entity, *shader);
}
render_engine::renderer::Render(*main_character, *shader);
// Render GUI items
render_engine::renderer::Render(guis, *gui_shader);
@@ -161,7 +185,12 @@ namespace scene
void scene::In_Game_Scene::update(GLFWwindow* window)
{
camera.Move(window);
//camera.Move(window);
main_character->Move(window);
//std::cout << "x get: " << movement.x << "\ny get: " << movement.y << "\nz get: " << movement.z << "\n";
camera.Follow(main_character->GetPosition());
// calculate where the next house model should be loaded
static int last_model_pos = 0;
@@ -174,7 +203,7 @@ namespace scene
}
// remember the position at which the new model was added
last_model_pos = model_pos;
collision::CheckCollisions(collision_entities);
update_hand_detection();
}

View File

@@ -32,7 +32,20 @@ namespace toolbox
matrix = glm::translate(matrix, negative_cam_pos);
return matrix;
}
float Lerp(float from, float to, float amount)
{
return from + amount * (to - from);
}
glm::vec3 Lerp(glm::vec3 from, glm::vec3 to, float amount)
{
glm::vec3 final;
final.x = Lerp(from.x, to.x, amount);
final.y = Lerp(from.y, to.y, amount);
final.z = Lerp(from.z, to.z, amount);
return final;
}
int Random(const int min, const int max)
{
static bool first = true;

View File

@@ -48,6 +48,28 @@ namespace toolbox
glm::mat4 CreateViewMatrix(entities::Camera& camera);
/*
* @biref go to one coordinate to another with smooting
*
* @param from one coordinate of the start
* @param to one coordinate of where to go
* @param amount the amount of smoothing (lower is smoother)
*
* @return coordinate of where to go
*/
float Lerp(float from, float to, float amount);
/*
* @biref go from one position to another with smoothing
*
* @param from position of the start
* @param to position of where to go
* @param amount the amount of smoothing (lower is smoother)
*
* @return position of where to go
*/
glm::vec3 Lerp(glm::vec3 from, glm::vec3 to, float amount);
/*
* @brief: This function will return a value between min and max
*
* @param min: The min value

View File

@@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\collision\collision_handler.cpp" />
<ClCompile Include="src\entities\main_character.cpp" />
<ClCompile Include="src\entities\house_generator.cpp" />
<ClCompile Include="src\computervision\calibration\HandCalibrator.cpp" />
<ClCompile Include="src\computervision\HandDetectRegion.cpp" />
@@ -47,6 +48,7 @@
<ItemGroup>
<ClInclude Include="src\collision\collision.h" />
<ClInclude Include="src\collision\collision_handler.h" />
<ClInclude Include="src\entities\main_character.h" />
<ClInclude Include="src\entities\house_generator.h" />
<ClInclude Include="src\computervision\calibration\HandCalibrator.h" />
<ClInclude Include="src\computervision\calibration\StaticSkinTreshold.h" />