void FollowPathComponent::Update(float time) { if(m_delayed) { // If delayed, increment current delay time. Then check if it's ready to go on to the next // node. If it is, get the next node. m_currentDelayTime += time; if(m_currentDelayTime > m_nodes[m_nextNode].delay) { m_currentDelayTime = 0.0f; m_delayed = false; GetNextNode(); // Now get the next destination. } return; } if(m_nodes.size() > m_nextNode) { m_t += time; float t = m_t / m_nodes[m_nextNode].timeToReach; glm::vec3 nextPoint; glm::vec3 refPoint; if(m_reverse) { nextPoint = CalculateBezierPoint(t, m_nodes[m_nextNode+3].position, // p0 m_nodes[m_nextNode+2].position, // p1 m_nodes[m_nextNode+1].position, // p2 m_nodes[m_nextNode].position); // p3 refPoint = CalculateBezierPoint(t+0.05f, m_nodes[m_nextNode+3].position, // p0 m_nodes[m_nextNode+2].position, // p1 m_nodes[m_nextNode+1].position, // p2 m_nodes[m_nextNode].position); // p3 } else { nextPoint = CalculateBezierPoint(t, m_nodes[m_nextNode-3].position, // p0 m_nodes[m_nextNode-2].position, // p1 m_nodes[m_nextNode-1].position, // p2 m_nodes[m_nextNode].position); // p3 refPoint = CalculateBezierPoint(t+0.05f, m_nodes[m_nextNode-3].position, // p0 m_nodes[m_nextNode-2].position, // p1 m_nodes[m_nextNode-1].position, // p2 m_nodes[m_nextNode].position); // p3 } glm::vec3 a = GetParent().GetTransform().GetForward(); glm::vec3 b = refPoint - GetParent().GetPos(); float cosX = glm::dot(a, b); cosX = cosX / (glm::length(a) * glm::length(b)); float x = glm::acos(glm::max(glm::min(cosX, 1.0f), -1.0f)); // Not sure if need to convert cosX to radians. x = x * 10.0f; glm::vec3 rotationAxis = glm::normalize(glm::cross(a, b)); Frame transform = GetParent().GetTransform(); glm::vec3 currUp = glm::normalize(transform.GetUp()); glm::vec3 currForward = glm::normalize(transform.GetForward()); glm::vec3 newForward = glm::rotate(currForward, x, rotationAxis); transform.SetForward(glm::normalize(newForward)); glm::vec3 newUp = glm::rotate(currUp, x, rotationAxis); transform.SetUp(glm::normalize(newUp)); GetParent().SetTransform(transform); GetParent().SetPos(nextPoint); if(t >= 1.0f) { // Arrived at node. Now check if there is a delay on it before getting next destination. if(m_nodes[m_nextNode].delay > 0.0001f) { m_delayed = true; m_currentDelayTime = 0.0f; } else { GetNextNode(); } m_t = 0.0f; } } }