How to make the camera rotate with the player?
-
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?
-
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?
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?
-
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?
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()); -
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());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?
-
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?
Yes
-
Yes
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.