Commit 4a50779d authored by Justine Martin's avatar Justine Martin 💖
Browse files

feat : Ajoute les controles pour le joueur 2

parent 3bd9c177
Loading
Loading
Loading
Loading
Compare 71336220 to a4d399b0
Changes for glitter: 1 added line, 1 removed line.
Original line number Diff line number Diff line
Subproject commit 7133622051149c9f690a90f388ce706bef8dd8f2
Subproject commit a4d399b0d9584f2cb8700873be7afe67ed8cc305
+3 −0
Changes for include/GlApplication.hpp: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -44,6 +44,9 @@ class GlApplication : public Application {
        void continuousKey(float deltaTime);
        void computeView(int player);

        void processPlayer1Input(GLFWwindow *window, float deltaTime);
        void processPlayer2Input(GLFWwindow *window, float deltaTime);

    private:
        std::map<ModelKeys, std::vector<std::shared_ptr<Entity>>> m_entities;  ///< objects to render
        std::map<ModelKeys, std::unique_ptr<RenderObject>> m_models;           ///< loaded models
+48 −13
Changes for src/GlApplication.cpp: 48 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ GlApplication::GlApplication(int windowWidth, int windowHeight)
void GlApplication::quickLog() const {
    std:: cout << "LOG: SpaceshipPlayer2 {x=[" << this->m_spaceshipPlayer2->get_x_axis().x << ", " << this->m_spaceshipPlayer2->get_x_axis().y << ", " << this->m_spaceshipPlayer2->get_x_axis().z << "]; y=[" <<
    this->m_spaceshipPlayer2->get_y_axis().x << ", " << this->m_spaceshipPlayer2->get_y_axis().y << ", " << this->m_spaceshipPlayer2->get_y_axis().z << "]; z=[" <<
    this->m_spaceshipPlayer2->get_z_axis().x << ", " << this->m_spaceshipPlayer2->get_z_axis().y << ", " << this->m_spaceshipPlayer2->get_z_axis().z << "]}";
    this->m_spaceshipPlayer2->get_z_axis().x << ", " << this->m_spaceshipPlayer2->get_z_axis().y << ", " << this->m_spaceshipPlayer2->get_z_axis().z << "]}" << std::endl;
} 

void GlApplication::loadModels() {
@@ -128,6 +128,7 @@ void GlApplication::computeView(int player)
void GlApplication::continuousKey(float deltaTime)
{
    GLFWwindow *window = glfwGetCurrentContext();

    const float pi = glm::pi<float>();
    if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
        m_eyeTheta += deltaTime * pi;
@@ -148,8 +149,12 @@ void GlApplication::continuousKey(float deltaTime)
        m_eyePhi -= deltaTime * pi;
    }

    float rads = glm::radians(20.0f*deltaTime);
    this->processPlayer1Input(window, deltaTime);
    this->processPlayer2Input(window, deltaTime);
}

void GlApplication::processPlayer1Input(GLFWwindow *window, float deltaTime) {
    float rads = glm::radians(20.0f*deltaTime);
    if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { // Spaceship 1 thrusters
        this->m_spaceshipPlayer1->thrust(deltaTime);
    }
@@ -157,13 +162,11 @@ void GlApplication::continuousKey(float deltaTime)
        glm::vec3 xAxis = (float)sin(rads/2) * this->m_spaceshipPlayer1->get_x_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos( rads/2), xAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS
        || glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) { // Spaceship 1 pitches up
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { // Spaceship 1 pitches up
        glm::vec3 xAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer1->get_x_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos(-rads/2), xAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS
        || glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) { // Spaceship 1 yaws left
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { // Spaceship 1 yaws left
        glm::vec3 zAxis = (float)sin( rads/2) * this->m_spaceshipPlayer1->get_z_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos( rads/2), zAxis));
    }
@@ -171,17 +174,44 @@ void GlApplication::continuousKey(float deltaTime)
        glm::vec3 zAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer1->get_z_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos(-rads/2), zAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) { // Spaceship 1 yaws left
    if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) { // Spaceship 1 yaws left
        glm::vec3 yAxis = (float)sin( rads/2) * this->m_spaceshipPlayer1->get_y_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos( rads/2), yAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) { // Spaceship 1 yaws right
    if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) { // Spaceship 1 yaws right
        glm::vec3 yAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer1->get_y_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos(-rads/2), yAxis));
    }
}

    if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
        this->quickLog();
void GlApplication::processPlayer2Input(GLFWwindow *window, float deltaTime) {
    float rads = glm::radians(20.0f*deltaTime);
    if (glfwGetKey(window, GLFW_KEY_KP_ENTER) == GLFW_PRESS) { // Spaceship 2 thrusters
        this->m_spaceshipPlayer2->thrust(deltaTime);
    }
    if (glfwGetKey(window, GLFW_KEY_KP_5) == GLFW_PRESS) { // Spaceship 2 pitches down
        glm::vec3 xAxis = (float)sin(rads/2) * this->m_spaceshipPlayer2->get_x_axis();
        this->m_spaceshipPlayer2->rotate(glm::quat(cos( rads/2), xAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_KP_8) == GLFW_PRESS) { // Spaceship 2 pitches up
        glm::vec3 xAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer2->get_x_axis();
        this->m_spaceshipPlayer2->rotate(glm::quat(cos(-rads/2), xAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_KP_4) == GLFW_PRESS) { // Spaceship 2 yaws left
        glm::vec3 zAxis = (float)sin( rads/2) * this->m_spaceshipPlayer2->get_z_axis();
        this->m_spaceshipPlayer2->rotate(glm::quat(cos( rads/2), zAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_KP_6) == GLFW_PRESS) { // Spaceship 2 yaws right
        glm::vec3 zAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer2->get_z_axis();
        this->m_spaceshipPlayer2->rotate(glm::quat(cos(-rads/2), zAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_KP_7) == GLFW_PRESS) { // Spaceship 2 yaws left
        glm::vec3 yAxis = (float)sin( rads/2) * this->m_spaceshipPlayer2->get_y_axis();
        this->m_spaceshipPlayer2->rotate(glm::quat(cos( rads/2), yAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_KP_9) == GLFW_PRESS) { // Spaceship 2 yaws right
        glm::vec3 yAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer2->get_y_axis();
        this->m_spaceshipPlayer2->rotate(glm::quat(cos(-rads/2), yAxis));
    }
}

@@ -190,20 +220,25 @@ void GlApplication::resize(GLFWwindow * window, int framebufferWidth, int frameb
    GlApplication & app = *static_cast<GlApplication *>(glfwGetWindowUserPointer(window));

    float aspect = framebufferWidth / float(framebufferHeight);
    app.m_proj = glm::perspective(90.f, aspect, 0.1f, 100.f);
    app.m_proj = glm::perspective(80.f, aspect, 0.1f, 100.f);

    glViewport(0, 0, framebufferWidth, framebufferHeight);
    
    app.setFramebufferSize(framebufferWidth, framebufferHeight);
}

void GlApplication::keyCallback(GLFWwindow * window, int key, int /*scancode*/, int action, int /*mods*/)
void GlApplication::keyCallback(GLFWwindow * window, int key, int scancode, int action, int mods)
{
    GlApplication & app = *static_cast<GlApplication *>(glfwGetWindowUserPointer(window));
    if(action == GLFW_PRESS) {
        switch (key) {
        case 'R':
            case GLFW_KEY_R:
                app.computeView(0);
                break;

            case GLFW_KEY_L:
                app.quickLog();
        }
    }    
}