ICT290 / src / scene / AIController / MovingEntity.cpp
MovingEntity.cpp
Raw
#include "MovingEntity.h"

glm::vec2 MovingEntity::getVelocityVec() const {
    return m_VelocityVec;
}

glm::vec2 MovingEntity::getHeadingVec() const {
    return m_HeadingVec;
}

glm::vec2 MovingEntity::getSideVec() const {
    return glm::vec2{-m_HeadingVec.y, m_HeadingVec.x};
}

float MovingEntity::getMass() const {
    return m_mass;
}

float MovingEntity::getMaxSpeed() const {
    return m_MaxSpeed;
}

float MovingEntity::getMaxForce() const {
    return m_MaxForce;
}

float MovingEntity::getMaxTurnRate() const {
    return m_MaxTurnRate;
}

void MovingEntity::setVelocityVec(const glm::vec2 velocity) {
    m_VelocityVec = velocity;
}

void MovingEntity::setHeadingVec(const glm::vec2 heading) {
    m_HeadingVec = heading;
}

void MovingEntity::setMass(const float mass) {
    m_mass = mass;
}

void MovingEntity::setMaxSpeed(const float speed) {
    m_MaxSpeed = speed;
}

void MovingEntity::setMaxForce(const float force) {
    m_MaxForce = force;
}

void MovingEntity::setMaxTurnRate(const float turn) {
    m_MaxTurnRate = turn;
}

float MovingEntity::getSpeed() {
    return glm::length(m_VelocityVec);
}

bool MovingEntity::rotateHeadingToTarget(glm::vec2 target) {
    bool facingTarget = false;
    // get normalised vector to target
    glm::vec2 normalToTarget = target - getPosition();
    normalToTarget = glm::normalize(normalToTarget);

    // calc angle from this position to target
    float angle = glm::acos(glm::dot(getHeadingVec(), normalToTarget));
    if (angle < 0.00001) {
        facingTarget = true;
    } else {
        m_HeadingVec = normalToTarget;

        if (angle > m_MaxTurnRate) {
            angle = m_MaxTurnRate;
        }

        m_HeadingVec = m_HeadingVec * glm::orientate2(angle * M_PI / 180);
        m_VelocityVec = m_VelocityVec * glm::orientate2(angle * M_PI / 180);
    }
    return facingTarget;
}