# OpenGL Simple 2D programming

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

## Recommended Posts

Hi, this is my first post here so I apologise if it's not perfect. I'm trying to write a small 2D game in OpenGL, however I'm having trouble with how glTranslatef works. Since whenever you create a new polygon you use glTranslatef, the new position becomes (0,0,0), which is fine if you want to create a polygon, tell it to act in a certain manner and then leave it, but I need to be able to act on that polygon, check for basic collisions with other polygons and move it around like a player. I've tried creating a polygon class which stores an origin as a vector (x,y,z) but I cant understand how to make it function correctly. Sorry if this is an unusual problem, but my previous experience is with code that lets you set a certain x,y position, based on how many pixels you want a player to move. Any help or links would be greatly appreciated.

##### Share on other sites
If I understand your problem correctly, you need the polygon's new coordinates after the transformation, but the OpenGL transformation functions just move the polygon to a new position but don't let you know what it is.

If that's your problem, you will either need to use unproject (which I know nothing about), or do all transformations in software, which is what I did because it's just as fast. (Quake does its transformations in software mode and works pretty fast on my Pentium 1, so why not?)

##### Share on other sites
Hi and welcome to the forums, Exomoto!

The first question is, are you using ortho mode? If not, start looking at it now, it'll help you a lot as you are working on a 2D game. Google 'glOrtho' with 'spec' and you should get all you need.

Quote:
 Original post by ExomotoSorry if this is an unusual problem, but my previous experience is with code that lets you set a certain x,y position, based on how many pixels you want a player to move.

You can do something like this every frame:
glOrtho (0, 640, 0, 480, -1, 1); // change 640 / 480 as you wishglMatrixMode (GL_MODELVIEW);glLoadIdentity ();draw_environment();glTranslatef (player.x, player.y, 0.0);draw_player();
You can test collisions with player.x and player.y.

##### Share on other sites
That would work, unless you want a polygon-based collision detection and you want your polygons to rotate.

##### Share on other sites
Quote:
 Original post by mrbigThat would work, unless you want a polygon-based collision detection and you want your polygons to rotate.

... which can also be done quite easily using simple trigonometry and keeping track of x, y and the angle of rotation [smile]

##### Share on other sites
Thanks for all the help. Most of the polygons in my game should be either octagons or rectangles, so I was just planning on using a pre-defined radius for the octagons and the x,y,height,width values for the rectangles for the collision detection. The octagons will rotate but the detection doesn't have to be perfect, just a circle area around them will suffice.

1. 1
2. 2
frob
17
3. 3
4. 4
5. 5

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004381

×