Jump to content
  • Advertisement
Sign in to follow this  
Sevenseven

2D Movement

This topic is 4688 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

I'm a little confused. I have some shapes drawn (2d). I want those shapes to start moving towards the center of the screen when a key is pressed. But I can't seen to do that. How is the translatef operation used? Are there any simple examples of it's usage? I can't seem to find a tutorial for 2d movement. Any assistance would be apreciated. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
glTranslate simply translates the current view matrix by some specified amount along each of the X/Y/Z axis. What you draw after that will be "moved".

I mentioned it in this thread though... you should probably visit NeHe's site to help you out with the basics of OpenGL.

Share this post


Link to post
Share on other sites
Here's a (very) brief overview of one way you might do it (this is also the standard way, more or less):

1. Set up your projection matrix (probably ortho if it's 2d only) so that you know what the bounds of the visible area are. (For example, you could go from 0,0 in one corner to 640,480 in the other.)

2. Give each object that you want to move 'x' and 'y' variables (or a 2d vector if you have a class for that).

3. Every frame, update the x and y (position) for each object based on whatever rules you wish. For example, you could make them move toward the center of the screen, as you mentioned.

4. Then render each of your objects. You render each object by setting the matrix mode to modelview, pushing the matrix, calling glTranslatef(object.x, object.y, 0.0f) or whatever, rendering the object, then popping the matrix. The function that actually renders the object should render it at the origin, more or less; glTranslate() will take care of positioning it correctly on screen.

If that's not enough info, you might post some of your code and explain how it's not working.

Share this post


Link to post
Share on other sites
for(int i = 0; i<20;i++){
glTranslatef(i,i,0);
for(int i = 0; i < 80; i++){
glColor3f (1.0, 1.0, 1.0);
drawCircle(color1x, color1y, 9); //color1x and color1y are arrays that contain the coordinates.
glFlush();
}
}

The circle is drawn, and then moved 20 pixels towards the center.
Couple of problems with this.
1. It just moves towards the right bottom corner. Not towards the center (original location varies). I think this can be resolved by taking the location of the shape and then calculating where the center is in relation to that.
2. When it moves, the circle is basically redrawn in the new position, leaving a trail effect.

So my questions are:
1. How is movement done after the shapes have been drawn? (start movement when a key is pressed).
2. How to get rid of the trail effect.

Thank you.

Share this post


Link to post
Share on other sites
OpenGL has nothing to do with key input. You'll need to either use DirectInput or messages (WM_KEYDOWN) if you're under Windows. SDL probably has some other kind of input method.

In order to calculate the "center" you will need to get the width and height and divide by two. As you increase the position of your "color1x" and "color1y" variables, check to see if they have passed the center point. If they have, clamp them.

I'd have to see a bit more code to understand exactly why you're getting trails, but odds are you're not clearing your color buffer before each frame (see glClear).

Good luck!

Share this post


Link to post
Share on other sites
thank you for the suggestions.
So would it be safe to assume that in order for me to move these shapes, some time after they have been drawn, I would have to redraw them, adjust the coordinates, clear the screen and redraw them again with the new coordinates? That kinda sounds silly, is there a better way to do this?

A little more explination on this, I have about alot of shapes drawn on load. After some time, when the user presses a key (I know how to handle that), the currently displayed shapes begin to move. Doing this the above discribed way seems to be an inefficient approach. Is there something better? I guess I just can't grasp the understanding of how I can translatef something after it has been drawn without redrawing it again. I hope all of this makes sense. I apreciate the assistance.

Share this post


Link to post
Share on other sites
Draw something, move it a little bit, clear and draw again. Do this really fast and that's what animation is all about! :)

If you don't want to clear the buffer (for whatever reason), you can draw your circles on top of the previous ones in black (or a background color), move them, then draw again. This is most likely slower than clearing the color buffer, though...

Perhaps it's not OpenGL, but the Windows GDI that might be more beneficial to you?

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!