Skip to content

Commit

Permalink
Added movement
Browse files Browse the repository at this point in the history
  • Loading branch information
denis authored and denis committed Jul 31, 2024
1 parent e148fcb commit c2e226e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
11 changes: 7 additions & 4 deletions GUI/src/ImGuiCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,37 @@ void Prisma::ImGuiCamera::updateCamera(std::shared_ptr<Prisma::Camera> camera)
void Prisma::ImGuiCamera::keyboardUpdate(void* windowData)
{
auto window = (GLFWwindow*)windowData;
if (glfwGetKey(window, Prisma::KEY_C) == GLFW_PRESS) {
//Prisma::PrismaFunc()->closeWindow();
}

if (glfwGetKey(window, Prisma::KEY_DELETE) == GLFW_PRESS) {
if (m_currentSelect) {
m_currentSelect->parent()->removeChild(m_currentSelect->uuid());
m_currentSelect = nullptr;
}
}

if (glfwGetKey(window, Prisma::KEY_W) == GLFW_PRESS) {
m_position += m_front * m_velocity;
}

if (glfwGetKey(window, Prisma::KEY_A) == GLFW_PRESS) {
m_position -= glm::normalize(glm::cross(m_front, m_up)) * m_velocity;
}

if (glfwGetKey(window, Prisma::KEY_S) == GLFW_PRESS && (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) && !m_save) {
Prisma::Exporter::getInstance().exportScene();
m_save = true;
}else if (glfwGetKey(window, Prisma::KEY_S) == GLFW_PRESS && !(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)) {
m_position -= m_front * m_velocity;
}

if(glfwGetKey(window, Prisma::KEY_S) == GLFW_RELEASE || glfwGetKey(window, GLFW_KEY_LEFT_CONTROL == GLFW_RELEASE) || glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL == GLFW_RELEASE)) {
m_save = false;
}

if (glfwGetKey(window, Prisma::KEY_D) == GLFW_PRESS) {
m_position += glm::normalize(glm::cross(m_front, m_up)) * m_velocity;
}

if (glfwGetKey(window, Prisma::KEY_G) == GLFW_PRESS && !m_pressed) {
m_showMouse = !m_showMouse;
m_lock = !m_lock;
Expand Down Expand Up @@ -129,7 +132,7 @@ void Prisma::ImGuiCamera::mouseButtonCallback() {
x = x - m_constraints.minX;
x = x / m_constraints.scale;

y = y - m_constraints.minY;
//y = y - m_constraints.minY;
y = y / m_constraints.scale;
y = settings.height - y;

Expand Down
2 changes: 1 addition & 1 deletion GUI/src/ImguiDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void Prisma::ImguiDebug::start()
void Prisma::ImguiDebug::close()
{

m_imguiCamera.constraints({ m_translate * m_width / 2,0,m_translate * m_width / 2 + m_scale * m_width,m_height * m_scale,meshInfo.updateMesh(),ImGuizmo::IsOver(),m_scale });
m_imguiCamera.constraints({ m_translate * m_width / 2,m_initOffset+50,m_translate * m_width / 2 + m_scale * m_width,m_height * m_scale,meshInfo.updateMesh(),ImGuizmo::IsOver(),m_scale });
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (!m_run) {
Expand Down
15 changes: 14 additions & 1 deletion UserEngine/include/PlayerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class PlayerController {
public:
PlayerController();
PlayerController(std::shared_ptr<Prisma::Scene> scene);

void updateCamera();

Expand All @@ -20,6 +20,8 @@ class PlayerController {

std::shared_ptr<Prisma::CallbackHandler> callback();

void target(glm::vec3 target);

private:

void createCamera();
Expand All @@ -44,8 +46,19 @@ class PlayerController {

float m_lastY;

float m_distance;

GLFWwindow* m_window;

glm::vec3 m_target = glm::vec3(0.0f);

std::shared_ptr<Prisma::Scene> m_scene;

std::shared_ptr<Prisma::AnimatedMesh> m_animatedMesh;

bool m_hide = false;

bool m_press = false;

float m_baseVelocity = 10.0f;
};
69 changes: 58 additions & 11 deletions UserEngine/src/PlayerController.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
#include "../include/PlayerController.h"

PlayerController::PlayerController() {
PlayerController::PlayerController(std::shared_ptr<Prisma::Scene> scene) : m_scene{scene} {
Prisma::NodeHelper nodeHelper;

m_animatedMesh = std::dynamic_pointer_cast<Prisma::AnimatedMesh>(nodeHelper.find(m_scene->root, "MutantMesh")->children()[0]);

if (m_animatedMesh) {
auto animation = std::make_shared<Prisma::Animation>("../../../Resources/DefaultScene/animations/animation.gltf", m_animatedMesh);

auto animator = std::make_shared<Prisma::Animator>(animation);
m_animatedMesh->animator(animator);
}
createCamera();
}

void PlayerController::updateCamera()
{
m_velocity = 1 * 1.0f / (float)Prisma::Engine::getInstance().fps();
updateKeyboard();
void PlayerController::updateCamera() {
m_velocity = m_baseVelocity * 1.0f / (float)Prisma::Engine::getInstance().fps();
// Calculate the new position based on yaw, pitch, and distance from target
glm::vec3 offset;
offset.x = m_distance * cos(glm::radians(m_pitch)) * cos(glm::radians(m_yaw));
offset.y = m_distance * sin(glm::radians(m_pitch));
offset.z = m_distance * cos(glm::radians(m_pitch)) * sin(glm::radians(m_yaw));

m_position = m_target + offset;

// Update the camera view matrix
m_scene->camera->position(m_position);
m_scene->camera->center(m_position + m_front);
m_scene->camera->center(m_target);
m_scene->camera->up(m_up);
}


void PlayerController::updateKeyboard()
{

auto playerData = m_animatedMesh->parent()->parent()->matrix();

if (glfwGetKey(m_window, Prisma::KEY_W) == GLFW_PRESS) {
m_position += m_front * m_velocity;
playerData[3] -= glm::vec4(m_front * m_velocity, 0);
}

if (glfwGetKey(m_window, Prisma::KEY_A) == GLFW_PRESS) {
m_position -= glm::normalize(glm::cross(m_front, m_up)) * m_velocity;
playerData[3] += glm::vec4(glm::normalize(glm::cross(m_front, m_up)) * m_velocity, 0);
}

if (glfwGetKey(m_window, Prisma::KEY_S) == GLFW_PRESS) {
m_position -= m_front * m_velocity;
playerData[3] += glm::vec4(m_front * m_velocity, 0);
}

if (glfwGetKey(m_window, Prisma::KEY_D) == GLFW_PRESS) {
m_position += glm::normalize(glm::cross(m_front, m_up)) * m_velocity;
playerData[3] -= glm::vec4(glm::normalize(glm::cross(m_front, m_up)) * m_velocity, 0);
}

m_animatedMesh->parent()->parent()->matrix(playerData);
Prisma::CacheScene::getInstance().updateData(true);
if (glfwGetKey(m_window, Prisma::KEY_G) == GLFW_PRESS && !m_press) {
m_hide = !m_hide;
if (m_hide) {
Prisma::PrismaFunc::getInstance().hiddenMouse(m_hide);
}
else {
Prisma::PrismaFunc::getInstance().hiddenMouse(m_hide);
}

m_press = true;
}

if (glfwGetKey(m_window, Prisma::KEY_G) == GLFW_RELEASE) {
m_press = false;
}
}

Expand All @@ -35,17 +76,23 @@ void PlayerController::scene(std::shared_ptr<Prisma::Scene> scene) {
}

void PlayerController::update() {
target(m_animatedMesh->parent()->finalMatrix()[3]);
updateCamera();
updateKeyboard();
}

std::shared_ptr<Prisma::CallbackHandler> PlayerController::callback() {
return m_handler;
}

void PlayerController::target(glm::vec3 target) {
m_target = target;
}

void PlayerController::createCamera() {
m_window = Prisma::PrismaFunc::getInstance().window();
m_handler = std::make_shared<Prisma::CallbackHandler>();

m_distance = 10;
m_handler->mouse = [this](float x, float y) {
float xpos = static_cast<float>(x);
float ypos = static_cast<float>(y);
Expand Down
11 changes: 1 addition & 10 deletions UserEngine/src/UserEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ void UserEngine::start()

Prisma::NodeHelper nodeHelper;

auto animatedMesh = std::dynamic_pointer_cast<Prisma::AnimatedMesh>(nodeHelper.find(m_root->root, "MutantMesh")->children()[0]);

if (animatedMesh) {
auto animation = std::make_shared<Prisma::Animation>("../../../Resources/DefaultScene/animations/animation.gltf", animatedMesh);

auto animator = std::make_shared<Prisma::Animator>(animation);
animatedMesh->animator(animator);
}

nodeHelper.nodeIterator(m_root->root, [](auto mesh, auto parent) {
auto currentMesh = std::dynamic_pointer_cast<Prisma::Mesh>(mesh);
if (currentMesh) {
Expand All @@ -42,7 +33,7 @@ void UserEngine::start()

Prisma::Physics::getInstance().physicsWorld()->dynamicsWorld->setGravity(btVector3(0.0, -10.0, 0.0));

m_player = std::make_shared<PlayerController>();
m_player = std::make_shared<PlayerController>(m_root);

m_player->scene(m_root);
}
Expand Down

0 comments on commit c2e226e

Please sign in to comment.