# Simulate camera movement in two dimensions (OpenGL)

I've been told by the folks over at Stack Overflow that there's no way to move the camera in OpenGL. I'm developing a 2D RPG with LWJGL and need to know the proper way to simulate camera movement as though it is "following" my player. Remember, this game is in two dimensions, so rotation of the world is not a problem here.

And another, related question: If camera movement simulation involves moving the world and not the player, how would it be possible to ever implement multiplayer? I'm a bit of a noob at OpenGL, but I see it used in a handful of popular multiplayer Java games, and can't get my head around the concept of being unable to move the perspective (and thus being forced to move the world) and not having players interfere with each other's movement.

I've been told by the folks over at Stack Overflow that there's no way to move the camera in OpenGL

Well, technically speaking there is no camera in openGL, what openGL provides is a series of transformations that you need to provide in order to simulate the idea of the camera; so the camera is just transforming some objects relative to some mathematical space ( traditionally you use the inverse of rotation and translation matrix in case of 3D space not counting the projection here).

If camera movement simulation involves moving the world and not the player, how would it be possible to ever implement multiplayer?

As I said in the first part you need think of it as relative movement, you move the "whole world" relative to the camera space so for example if you move the world with (x=2, y=0), you can think of it as moving the camera with (x=-2, y=0). My advice is to use one notation and stick with it. Keep in mind that OpenGL doesn't directly deal with 2D, so you need to set up orthographic projection with fixed camera axis probably (right=x, up=y, forward=-z) and a translation where you need to fix the Z like for example (x,y, 10) and let the camera move in the xy plane.

In case of the player you need to understand that the camera does't matter here, you only move the player relative to the world space, hence the same concept above applies here you either move the player relative to the stage ( I used the term stage here instead of world to avoid confusion) or the stage relative to the player.

Regarding multiplayer games, which is something I haven't done before, I think that moving the player relative to the stage is more straight forward to deal with, but I don't have any concrete example.

[Edit Based on comments] To make the camera follow the player; given that your camera is at position (x,y) - I am going to drop the Z from the solution- and your player is at a fixed offset from your camera say (x+t, y+t), you need to make the camera move towards (x+t, y+t) there are different ways of doing this, I can think of:

• Use any kind of interpolation, a simple example might be linear interpolation, expressed in parameterized vector notation NewPos = camPos + t*( normalize(PlayerPos - camPos) ). Note that updating the values will depending on how you handle your animation and game update. You can also change the equation for smoother animation.

• Use elastic based camera, with the formula F=kx where F stands for Force, K constant based on the spring stiffness (you can choose as you see fit), and x can be used distance between the camera and the player. Note that F can be factored into F=m*a where m is mass(constant) and a is acceleration so you can modify the camera position using the calculated values. In general this will give you a spring like behavior but you need to multiply with some damping factor so it won't "bounce" forever.

• I forgot to mention that the easiest way to make your camera follow the player is to set the camera position to equal the player position every frame, but that won't simulate the following behavior but rather the camera will be fixed on the player position, and it might be annoying if the player has some sharp movements, since it will exactly match its position.

• You have helped a little by explaining that it wouldn't wreck the multiplayer possibility, but my primary question has yet to be answered. [I] need to know the proper way to simulate camera movement as though it is "following" my player. So although you have explained it's possible, you haven't done a very good job explaining the steps required to do so. If it helps, I'm using quads as objects, since everything is on a flat plane. – 2mac Sep 24 '13 at 15:51
• I edited the answer, I hope this helps. – concept3d Sep 24 '13 at 20:01