Advertisement Jump to content
Sign in to follow this  

2D OpenGL

This topic is 4166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm wondering if anyone knows of any game specific OpenGL tutorials for writing 2D games with OpenGL. I specifically want to create a platform game like the ones from the early 90's. So I need to know about checking for platforms and stuff like that. Has anyone here coded anything like this in OpenGL. I remember a few years back nehe did his own 2D platform game but for some reason it vanished from the site. I would have loved to see the source code for that demo. Steve

Share this post

Link to post
Share on other sites
i haven't done exactly that, but 3D or 2D is all the same it's just that 2D has one axis locked.
just write a small physics engine and your halfway there, i am sure there are tons of tutorials out there, just google "game physics tutorials"

Share this post

Link to post
Share on other sites

just use an orthogonal projection matrix and for physics you can use one of the physics-engines out there like ode or physx. You must remember to lock one axis. If you want to use your own 2D-physic-engine, look at that code and tutorial:

I just found a tutorial for ODE 2D:

Have fun


[Edited by - Forlom on August 5, 2007 7:49:23 AM]

Share this post

Link to post
Share on other sites

Thanks for the replies. I know all about orthongonal projection and only drawing the sprites in the same plane. I am more specifically interested in 2D collision detection with the ground and platforms etc for a side scrolling platform game. Sprite to Sprite collision detection I already know about - its fairly easy. Anyone know how I'd go about this?? :)


Share this post

Link to post
Share on other sites
Sounds like you are nearly there if you have the sprite collisions working! Scrolling maps and platforms are mere extensions to sprite collisions in general.

Honestly, you need the concept (the hard part), not exactly OpenlGL or DX way of doing it (that part is rather easy!)

I'll see if I can give a stab at explaining it a bit. Forgive me if I sway around a bit. It sounds like you already have a lot of the basics, so if I assume too much, please ask questions.

I'm gonna do something basic, using a grid system matrix with 2 dimensions - X and Y.

Let's say the grid is 10 tiles wide by 10 tiles high to keep it simple. Size really doesn't matter.

Start position is center matrix: 5,5

There are walls around the edge of the map:
0,0 to 0,9
0,0 to 9,0
0,9 to 9,9
9,0 to 9,9.

Yeah, a nice plain box!

Each tile is 32px X 32px
Keeping it simple, the Avatar is also 32px x 32px

Let's use the Bounding Box method of collision detection (for those unfamiliar, its a box that surronds the sprite, or tile drawn to check collisions with).

You start at the X,Y location where the avatar is located originally. You game loop is designed something like:


Move Map
Move Avatar - will check collisions against map and various sprites
Move Baddies - will check collisions with map and various sprites
Draw Map
While drawing layers, see which layers to draw baddies and avatar

Loop until exit

The basic idea is you check collisions with the map tiles located on screen against the avatar. To check map tiles such as walls, platforms, etc. use a value for the image. IE: A wall image has a value of 10, water has a value of 5, etc. When you loop through the various tiles, do a quick check of which tile value is assigned, then apply the collision detection. Since you are working on a platformer, use gravity. Make sure you time control it all too, otherwise unexpected results will happen (jerkiness, etc)

I'd suggest that you perform collision checks with avatar and baddies in each baddie move routine. I think that keeps it simpler. You loop through the baddies once vs many times. You have to loop once to check their move, why not check the collisions to the Avatar in that loop? It save s a cycle or two :) Its up to you ultimately, just my two pence.

So basically when the collision occurs with the map and sprite / avatar, you will perform the reaction to that collision. Mostly a stop in movement or a bounce, etc.

Back to the start and wall locations in my example. As you progress across the tiles (we'll say the cross-able tiles are value 0), you'll check the collision if the value is not 0. Once you reach the wall, the value is 10. Then collision will occur and you execute the code that says to make the avatar (or sprite baddie) to stop moving in that direction.

Here's pseudo(ish) code for the Bounding Box:

If (box1_X1 > box2_X2) or (box1_X2 < box2_X1) or (box1_Y1 > box2_Y2) or (box1_Y2 < box2_Y1) then Collision occurs

I am writing a 4-way scroller with multi-layers. It uses the principles I described here. Its a bit more involved than this I described, but this is the basic flow to it all.

You can see it in action here:

I hope this helps!

Enjoy your game development!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!