[ADD] made camera follow the character with lerp

This commit is contained in:
Nathalie Seen
2021-06-08 10:55:48 +02:00
parent 86383aace5
commit edbbfb136a
6 changed files with 35 additions and 4 deletions

View File

@@ -1,4 +1,6 @@
#include "camera.h"
#include <iostream>
#include "../toolbox/toolbox.h"
namespace entities
{
@@ -8,7 +10,9 @@ namespace entities
{}
void Camera::Follow(glm::vec3 follow_position) {
position.z = follow_position.z + 200;
follow_position.z += 100;
follow_position.y += 50;
position = toolbox::Lerp(position, follow_position, 0.1);
}
void Camera::Move(GLFWwindow* window)
@@ -41,9 +45,14 @@ namespace entities
{
up_down_speed += UP_SPEED;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
up_down_speed -= UP_SPEED;
}
position.x += side_speed;
position.z += movement_speed;
position.y += up_down_speed;
std::cout <<"x= " << position.x <<"\ny= " << position.y << "\nz= " << position.z << "\n";
}
}

View File

@@ -15,7 +15,7 @@ namespace entities
glm::vec3 main_character::move(GLFWwindow* window)
{
float movement_speed = -1.0f;
float up_down_speed = -0.4f;
float up_down_speed = -0.2f;
float side_speed = 0;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
@@ -47,6 +47,11 @@ namespace entities
up_down_speed -= UP_SPEED;
}
IncreasePosition(glm::vec3(side_speed, up_down_speed, movement_speed));
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;
MoveCollisionBox();
return glm::vec3(side_speed, up_down_speed, movement_speed );
}
}

View File

@@ -7,7 +7,7 @@ namespace entities
{
class main_character : public CollisionEntity {
const float SPEED = 1.0f;
const float UP_SPEED = 1.0f;
const float UP_SPEED = 0.6f;
public:
main_character(const models::TexturedModel& model, const glm::vec3& position,
const glm::vec3& rotation, float scale, const collision::Box& bounding_box);

View File

@@ -28,7 +28,7 @@ namespace scene
models::ModelTexture texture;
shaders::EntityShader* shader;
shaders::GuiShader* gui_shader;
entities::Camera camera(glm::vec3(0, 100, 0), glm::vec3(0, 0, 0));
entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
std::vector<gui::GuiTexture*> guis;
models::TexturedModel model;

View File

@@ -31,4 +31,17 @@ 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;
}
}

View File

@@ -46,4 +46,8 @@ namespace toolbox
* @return: The view matrix
*/
glm::mat4 CreateViewMatrix(entities::Camera& camera);
float Lerp(float from, float to, float amount);
glm::vec3 Lerp(glm::vec3 from, glm::vec3 to, float amount);
}