View Full Version : Rotating an Object on Key Press and following camera

08-03-2016, 09:34 PM
hey guys, i haven't idea how to coding rotating an object on key press and when object rotating camera follow an object rotating...
please help me, ..
thanks ..

08-03-2016, 11:45 PM
Basically, you load a rotation matrix with the angle you want to rotate through per frame. Then you multiply that by the world, or object, matrix of the object.

OpenGl doesn't really handle keyboard input. So, there is that. I use GLFW for things like keyboard input. Here's my code for rotating the camera when the left and right arrow keys are pressed.

if (OperatingSystem.Keyboard.KeyPressed == GLFW_KEY_RIGHT && OperatingSystem.Keyboard.ActionPressed != GLFW_RELEASE)
View = glm::rotate(glm::mat4(1.0f), 0.1f, glm::vec3(0.0f, 1.0f, 0.0f)) * View;
if (OperatingSystem.Keyboard.KeyPressed == GLFW_KEY_LEFT && OperatingSystem.Keyboard.ActionPressed != GLFW_RELEASE)
View = glm::rotate(glm::mat4(1.0f), -0.1f, glm::vec3(0.0f, 1.0f, 0.0f)) * View;

Technically, you want to multiply these times the amount of time since the last frame so that you get consistent animation regardless of the actual frame rate. But for this I did not do that. I am using GLM for the math. View is the camera's view matrix.

But moving objects and cameras is all about the matrices.

With GLFW you setup a callback function to handle the keyboard something like this:" glfwSetKeyCallback(MainWindow, Keyboard.glfw_OnKey);"

Then you define your callback function: