Commit e96aae2d authored by Benjamin Castel's avatar Benjamin Castel
Browse files

feat: Enabled ugly (and maybe buggy) spaceship collision

parent f99d38b3
Loading
Loading
Loading
Loading
+5 −2
Changes for include/model/Entity.hpp: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -30,9 +30,12 @@ class Entity {
        void setScale(glm::vec3 scales);

        glm::mat4 get_box_model_matrix(std::unique_ptr<RenderObject> &renderObject) const;
        bool check_box_intersection(std::unique_ptr<RenderObject> &renderObjectThis,
        bool check_box_intersection(const std::unique_ptr<RenderObject> &renderObjectThis,
                                    const Entity &other,
                                    std::unique_ptr<RenderObject> &renderObjectOther) const;
                                    const std::unique_ptr<RenderObject> &renderObjectOther,
                                    const bool &movingObjects=false,
                                    const glm::vec3 &velocityThis=glm::vec3(0),
                                    const glm::vec3 &velocityOther=glm::vec3(0)) const;

    protected:
        void computeModelMatrix(); 
+4 −2
Changes for include/model/Spaceship.hpp: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -29,9 +29,11 @@ class Spaceship : public Entity {

        void update(const float &deltaTime) override;


        std::shared_ptr<Missile> generate_missile();

        // TODO : REMOVE AND REPLACE
        void thrust(const float &deltaTime);
        void thrust(const float &deltaTime,
                    const std::unique_ptr<RenderObject> &renderObjectThis,
                    const Entity &other,
                    const std::unique_ptr<RenderObject> &renderObjectOther);
};
+2 −2
Changes for src/GlApplication.cpp: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -280,7 +280,7 @@ void GlApplication::continuousKey(float 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);
        this->m_spaceshipPlayer1->thrust(deltaTime, this->m_models[ModelKeys::SPACESHIP_1], *this->m_entities[ModelKeys::SPACESHIP_2][0], this->m_models[ModelKeys::SPACESHIP_2]);
    }
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { // Spaceship 1 pitches down
        glm::vec3 xAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer1->get_x_axis();
@@ -317,7 +317,7 @@ void GlApplication::processPlayer1Input(GLFWwindow *window, float deltaTime) {
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);
        this->m_spaceshipPlayer2->thrust(deltaTime, this->m_models[ModelKeys::SPACESHIP_2], *this->m_entities[ModelKeys::SPACESHIP_1][0], this->m_models[ModelKeys::SPACESHIP_1]);
    }
    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();
+44 −2
Changes for src/model/Entity.cpp: 44 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -80,9 +80,12 @@ void Entity::computeModelMatrix() {
    this->m_modelMatrix = translateMatrix * rotationMatrix * scaleMatrix;
}

bool Entity::check_box_intersection(std::unique_ptr<RenderObject> &renderObjectThis,
bool Entity::check_box_intersection(const std::unique_ptr<RenderObject> &renderObjectThis,
                                    const Entity &other,
                                    std::unique_ptr<RenderObject> &renderObjectOther) const {
                                    const std::unique_ptr<RenderObject> &renderObjectOther,
                                    const bool &movingObjects,
                                    const glm::vec3 &velocityThis,
                                    const glm::vec3 &velocityOther) const {
    glm::vec3 boxDimsThis  = renderObjectThis->get_dimensions();
    glm::vec3 boxDimsOther = renderObjectOther->get_dimensions(); 
    
@@ -200,6 +203,45 @@ bool Entity::check_box_intersection(std::unique_ptr<RenderObject> &renderObjectT
        return false;
    }

    if (movingObjects) {
        glm::vec3 w = velocityOther - velocityThis;

        glm::vec3 alpha {
            glm::dot(w, axisThis[0]),
            glm::dot(w, axisThis[1]),
            glm::dot(w, axisThis[2])
        };
        glm::vec3 beta {
            glm::dot(w, axisOther[0]),
            glm::dot(w, axisOther[1]),
            glm::dot(w, axisOther[2])
        };

        if (extentsThis[1]*glm::length(alpha[2]) + extentsThis[2]*glm::length(alpha[1]) + extentsOther[0]*glm::length(cMatrix[1][0]*alpha[2] - cMatrix[2][0]*alpha[1]) + extentsOther[1]*glm::length(cMatrix[1][1]*alpha[2] - cMatrix[2][1]*alpha[1]) + extentsOther[2]*glm::length(cMatrix[1][2]*alpha[2] - cMatrix[2][2]*alpha[1]) < glm::dot(axisThis[0], glm::cross(w, centersDistance))) {
            return false;
        }

        if (extentsThis[0]*glm::length(alpha[2]) + extentsThis[2]*glm::length(alpha[0]) + extentsOther[0]*glm::length(cMatrix[0][0]*alpha[2] - cMatrix[2][0]*alpha[0]) + extentsOther[1]*glm::length(cMatrix[0][1]*alpha[2] - cMatrix[2][1]*alpha[0]) + extentsOther[2]*glm::length(cMatrix[0][2]*alpha[2] - cMatrix[2][2]*alpha[0]) < glm::dot(axisThis[1], glm::cross(w, centersDistance))) {
            return false;
        }

        if (extentsThis[0]*glm::length(alpha[1]) + extentsThis[1]*glm::length(alpha[0]) + extentsOther[0]*glm::length(cMatrix[0][0]*alpha[1] - cMatrix[1][0]*alpha[0]) + extentsOther[1]*glm::length(cMatrix[0][1]*alpha[1] - cMatrix[1][1]*alpha[0]) + extentsOther[2]*glm::length(cMatrix[0][2]*alpha[1] - cMatrix[1][2]*alpha[0]) < glm::dot(axisThis[2], glm::cross(w, centersDistance))) {
            return false;
        }

        if (extentsThis[0]*glm::length(cMatrix[0][1]*beta[2] - cMatrix[0][2]*beta[1]) + extentsThis[1]*glm::length(cMatrix[1][1]*beta[2] - cMatrix[1][2]*beta[1]) + extentsThis[2]*glm::length(cMatrix[2][1]*beta[2] - cMatrix[2][2]*beta[1]) + extentsOther[1]*glm::length(beta[2]) + extentsOther[2]*glm::length(beta[1]) < glm::dot(axisOther[0], glm::cross(w, centersDistance))) {
            return false;
        }

        if (extentsThis[0]*glm::length(cMatrix[0][0]*beta[2] - cMatrix[0][2]*beta[0]) + extentsThis[1]*glm::length(cMatrix[1][0]*beta[2] - cMatrix[1][2]*beta[0]) + extentsThis[2]*glm::length(cMatrix[2][0]*beta[2] - cMatrix[2][2]*beta[0]) + extentsOther[0]*glm::length(beta[2]) + extentsOther[2]*glm::length(beta[0]) < glm::dot(axisOther[1], glm::cross(w, centersDistance))) {
            return false;
        }

        if (extentsThis[0]*glm::length(cMatrix[0][0]*beta[1] - cMatrix[0][1]*beta[0]) + extentsThis[1]*glm::length(cMatrix[1][0]*beta[1] - cMatrix[1][1]*beta[0]) + extentsThis[2]*glm::length(cMatrix[2][0]*beta[1] - cMatrix[2][1]*beta[0]) + extentsOther[0]*glm::length(beta[2]) + extentsOther[1]*glm::length(beta[0]) < glm::dot(axisOther[2], glm::cross(w, centersDistance))) {
            return false;
        }
    }

    return true; // Everything failed
}

+6 −1
Changes for src/model/Spaceship.cpp: 6 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -28,9 +28,14 @@ void Spaceship::take_damage() {
}

// TODO : REMOVE AND REPLACE
void Spaceship::thrust(const float &deltaTime) {
void Spaceship::thrust(const float &deltaTime,
                       const std::unique_ptr<RenderObject> &renderObjectThis,
                       const Entity &other,
                       const std::unique_ptr<RenderObject> &renderObjectOther) {
    if (!Entity::check_box_intersection(renderObjectThis, other, renderObjectOther, true, normalize(this->m_localZ)*deltaTime)) {
        Entity::move(normalize(this->m_localZ)*deltaTime);
    }
}

void Spaceship::update(const float &deltaTime) {
    this->fireCooldown -= deltaTime;