Results 1 to 2 of 2

Thread: Rotating an Object on Key Press and following camera

  1. #1
    Newbie Newbie
    Join Date
    Aug 2016
    Posts
    2

    Rotating an Object on Key Press and following camera

    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 ..

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    112
    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.

    Code :
    			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:

Similar Threads

  1. Rotating an Object on Key Press and following camera
    By kumakun in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 08-04-2016, 04:01 AM
  2. Replies: 0
    Last Post: 10-28-2014, 08:51 PM
  3. Rotating the camera
    By Ehsan Nekooee in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 09-05-2009, 02:10 PM
  4. Rotating camera in 4pi.
    By brambilla in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 01-25-2009, 07:05 AM
  5. rotating and object without rotating its axis
    By scam in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 12-17-2003, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean