Jump to content
  • Advertisement
Sign in to follow this  
Exomoto

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.

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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 Exomoto
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.

You can do something like this every frame:
glOrtho (0, 640, 0, 480, -1, 1); // change 640 / 480 as you wish
glMatrixMode (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 this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by mrbig
That 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 this post


Link to post
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.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!