I've been trying to make a 2d platformer, and I can't figure out how to make the player move when a key is pressed. Can anyone provide either a way to do this, or a link to a page that says how to? (I'm using modern opengl and I don't wan't to use an external library for this, as I am trying to get practice with OpenGL)
Crossbones+ - Reputation: 7126
Posted 10 August 2014 - 03:49 AM
Character movement means to control the position an orientation of a game object. OpenGL is a graphics rendering API. So, movement of game objects has absolutely nothing to do with OpenGL. Instead, OpenGL can be used to render the current world state as is generated by other pieces of software.
Apart from that, your question is vague since you give very few details of how your world is organized at all. So here comes a coarse overview:
In principle you want to run the so-called game loop. At the beginning of a loop iteration you process player input. Such player input is e.g. "key W is pressed". Input processing means to translate such physical stuff into logical stuff. In the example this means "player avatar move forward". After input processing comes animation control (letting AI for NPCs aside). Animation control tries to perform the current command ("player avatar move forward"), but perhaps may not be able to do so because the avatar collides in the direction of interest, e.g. it has reached the top of the screen. If movement is possible, then animation control alters the current placement (i.e. position and orientation) and perhaps chooses another avatar picture or whatever. At the very last there comes rendering, using the given placement and other avatar state, and ... well ... renders it.
Members - Reputation: 547
Posted 10 August 2014 - 08:10 AM
Well you would render the player as its own draw call and translate it by its position. Alternatively, you could translate the world instead if you wanted the player to stay fixed on the screen. For key input, you're going to need to use a library most likely. I would suggest SDL or glut. Glut is a lot less involved, but SDL is a lot more open, for instance you can control the main loop, whereas in glut you can't. Theres plenty of documentation on either of these. Good luck!