Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to make the camera rotate with the player?

How to make the camera rotate with the player?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LittleJackFlash
    wrote on last edited by
    #1

    I currently have the following code -

    gluLookAtf(player.position.getX(),player.position.getY() + 15,player.position.getZ() + 20,
    player.position.getX() ,player.position.getY(),player.position.getZ() ,
    0.0, 1.0, 0.0);

    How can I make the player and the camera rotate together?

    P 1 Reply Last reply
    0
    • L LittleJackFlash

      I currently have the following code -

      gluLookAtf(player.position.getX(),player.position.getY() + 15,player.position.getZ() + 20,
      player.position.getX() ,player.position.getY(),player.position.getZ() ,
      0.0, 1.0, 0.0);

      How can I make the player and the camera rotate together?

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      By keeping the position/rotation of your camera tied to that of your player with fixed delta rotation/movement maybe with some attenuation on some of the components of the world position of the camera if the movement of the player is ugly. Its a different story how to use this camera position/rotation in the renderer but that should be trivial if you have some basic knowledge in the area. You have just grabbed out an opengl call from somewhere, noone will be able to tell what is wrong in your code but it can be anything based on the kind of this question. Considering that you are mixing the update of your object positions with renderer code your software design isn't good for sure. Your game code should look like the following: - a set/graph of object that describe the state of your game at any given time. This can be for example a set of world object with position/rotation values and attributes like "dead", "visible". - an infinite loop that does the following:

        \* updates the state of your game objects (for example by stepping their position/rotation using physics simulation, you have to update the state of dependent objects (like your camera) only after its dependency objects (player) are updated.
        \* OPTIONALLY render the state of your game (the world objects/entities with their position and other state variables) and draw the "hud"/score and other info on the screen as the result of rendering the state of your "game rules" object. Here you setup your renderer camera based on the world position/rotation of your camera object in your game state data that has already been updated based on its dependency object: the player object.
      

      Note that your game should work even without calling the global "render" function in your game, the game should work without a renderer just by printing out the state of your objects to the console at any time if your game code design is good! So what is the problem?

      L 1 Reply Last reply
      0
      • P pasztorpisti

        By keeping the position/rotation of your camera tied to that of your player with fixed delta rotation/movement maybe with some attenuation on some of the components of the world position of the camera if the movement of the player is ugly. Its a different story how to use this camera position/rotation in the renderer but that should be trivial if you have some basic knowledge in the area. You have just grabbed out an opengl call from somewhere, noone will be able to tell what is wrong in your code but it can be anything based on the kind of this question. Considering that you are mixing the update of your object positions with renderer code your software design isn't good for sure. Your game code should look like the following: - a set/graph of object that describe the state of your game at any given time. This can be for example a set of world object with position/rotation values and attributes like "dead", "visible". - an infinite loop that does the following:

          \* updates the state of your game objects (for example by stepping their position/rotation using physics simulation, you have to update the state of dependent objects (like your camera) only after its dependency objects (player) are updated.
          \* OPTIONALLY render the state of your game (the world objects/entities with their position and other state variables) and draw the "hud"/score and other info on the screen as the result of rendering the state of your "game rules" object. Here you setup your renderer camera based on the world position/rotation of your camera object in your game state data that has already been updated based on its dependency object: the player object.
        

        Note that your game should work even without calling the global "render" function in your game, the game should work without a renderer just by printing out the state of your objects to the console at any time if your game code design is good! So what is the problem?

        L Offline
        L Offline
        LittleJackFlash
        wrote on last edited by
        #3

        Hello sorry I wasn't very clear, basically at the moment I have a camera that following the player in the 4 direction i.e moving forward, backwards and strafing right and left, I was aiming to be able to rotate the camera around the player in order to me able to see the rest of the world around him. yet when I tried adjusting the glrotate i got the camera to kinda rotate but the longer to hold either left or right the further away from the player the camera becomes.

        //Bind Camera to Player
        gluLookAtf(player.position.getX() + player.xrot ,player.position.getY() + 15, player.position.getZ() + 20,
        player.position.getX() ,player.position.getY(),player.position.getZ() ,
        0.0, 1.0, 0.0);

        glPushMatrix();
        glRotatef(xrot, 0.0, 0.1, 0.0);
        glTranslatef(pos.getX(), pos.getY(), pos.getZ());

        J 1 Reply Last reply
        0
        • L LittleJackFlash

          Hello sorry I wasn't very clear, basically at the moment I have a camera that following the player in the 4 direction i.e moving forward, backwards and strafing right and left, I was aiming to be able to rotate the camera around the player in order to me able to see the rest of the world around him. yet when I tried adjusting the glrotate i got the camera to kinda rotate but the longer to hold either left or right the further away from the player the camera becomes.

          //Bind Camera to Player
          gluLookAtf(player.position.getX() + player.xrot ,player.position.getY() + 15, player.position.getZ() + 20,
          player.position.getX() ,player.position.getY(),player.position.getZ() ,
          0.0, 1.0, 0.0);

          glPushMatrix();
          glRotatef(xrot, 0.0, 0.1, 0.0);
          glTranslatef(pos.getX(), pos.getY(), pos.getZ());

          J Offline
          J Offline
          Jonathan Davies
          wrote on last edited by
          #4

          In a robotics context, at this point I'd ensure I was using a work (player) co-ordinate system rather than a global (the game area) coordinate system or a tool (camera) coordinate system. Is your coordinate system based on the player at this point to make rotation easier? or if not can you switch to one that is? Also are you using a polar coordinate system, to rotate around the player?

          L 1 Reply Last reply
          0
          • J Jonathan Davies

            In a robotics context, at this point I'd ensure I was using a work (player) co-ordinate system rather than a global (the game area) coordinate system or a tool (camera) coordinate system. Is your coordinate system based on the player at this point to make rotation easier? or if not can you switch to one that is? Also are you using a polar coordinate system, to rotate around the player?

            L Offline
            L Offline
            LittleJackFlash
            wrote on last edited by
            #5

            Yes

            J 1 Reply Last reply
            0
            • L LittleJackFlash

              Yes

              J Offline
              J Offline
              Jonathan Davies
              wrote on last edited by
              #6

              Sounds like you need to apply two transformations then, one for the player to rotate them and one to rotate the camera position around the same point. That's about the limit of my gl recall I'm afraid.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups