I am relatively new to OpenGL and 3D environments and am having trouble creating a rotation matrix that will rotate an arrow model so it 'points' in the direction that it is going. I am using GLM as a math library. My current approach is to predict the position the arrow will be at next frame and try to rotate the arrow to face that position.
double nextVelY = velocity.y - (gravity * (((glfwGetTime() + dt) - clock) / 1000.0)); glm::vec3 nextVelocity = velocity; nextVelocity.y = pos.y - 0.5 > -3 ? nextVelY : velocity.y; glm::vec3 lookat = glm::vec3((nextVelocity * glm::vec3(20) * glm::vec3(dt))); glm::vec3 pos2 = glm::vec3(0);
And then try to calculate angles between the current position and the predicted position
model = glm::translate(model, pos); model = glm::scale(model, glm::vec3(0.05)); float angleX = atan(nextVelocity.y / nextVelocity.z); float angleY = atan(nextVelocity.x / nextVelocity.z); float angleZ = atan(nextVelocity.x / nextVelocity.y); glm::vec3 zAxis = glm::normalize(lookat - pos2); glm::vec3 xAxis = glm::normalize(glm::cross(up, zAxis)); glm::vec3 yAxis = glm::cross(zAxis, yAxis); model = glm::rotate(model, angleX, xAxis); model = glm::rotate(model, angleY, yAxis); model = glm::rotate(model, angleZ, zAxis);
I'm not at all sure if this is a good way to try and achieve this. I've also tried using glm::lookAt
model = glm::transpose(glm::lookAt(pos2, lookat, glm::vec3(0, 1, 0)));
and various other methods I've found with no success.