Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: opengl game

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    29

    opengl game

    I am working on a space invaders game I have rendered the cannon, shields, aliens and bullets to the screen. I am unsure of how to get the aliens to move left and right across the screen.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    716
    Your question is very vague, so will necessarily be the answer: Presumably you somewhere have some variables for each alien that hold its current position. Each frame you need to update those positions and render a new image with everything that moved now at those updated positions. If you've hardcoded the positions of everything the first thing you need to change is store the positions in variables.

    This is more of a general programming/software engineering question, as such a general programming forum may be a better place to ask.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    29
    here is some of my rendering code
    Code :
    	//alienone
    	glEnable(GL_TEXTURE_2D);
    	glBindTexture(GL_TEXTURE_2D, _textureId_four);
     
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glColor3f(1.0f, 1.0f, 1.0f);
     
    	for(float i=-8.0f; i<=6.0f; i+=2.0f)
    	{
    	glBegin(GL_QUADS);
    	glTexCoord2f(0.0f,1.0f);
    	glVertex3f(i,8.0f, 0.0f);
    	glTexCoord2f(0.0f,0.0f);
    	glVertex3f(i+2.0f,8.0f, 0.0f);
    	glTexCoord2f(1.0f,0.0f);
    	glVertex3f(i+2.0f,7.0f, 0.0f);
    	glTexCoord2f(1.0f,1.0f);
    	glVertex3f(i,7.0f, 0.0f);
    	glEnd();
    	}

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    716
    Please try to ask concrete questions, do not just dump some code on people and have them figure out what the heck the problem is - see Forum Posting Guidelines for suggestions how to ask good questions.
    I've already mentioned that instead of hardcoded positions you should use variables. You could for example do something like this:

    Code :
    struct Alien
    {
        float poxX;
        float posY;
    };
     
    void drawAlien(Alien* a)
    {
        // draw a quad at a->posX, a->posY (lower left corner)
        glBegin(GL_QUADS);
        glVertex3f(a->posX, a->poxY, 0.f);
     
        glVertex3f(a->posX + 2.f, a->poxY, 0.f);
     
        glVertex3f(a->posX + 2.f, a->poxY + 2.f, 0.f);
     
        glVertex3f(a->posX, a->poxY + 2.f, 0.f);
        glEnd();
    }
     
    void moveAlien(Alien* a)
    {
       // calculate new position for alien a and update a->posX, a->posY
    }

    And for each alien in your game you allocate a variable of type Alien. In your render function you call drawAlien for each of them to draw them at their location. At the beginning of each frame you also call moveAlien for each one and calculate the position where they should be in this frame (probably based on the aliens movement direction and speed and how much time has elapsed since the last frame).

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    29
    sorry I will try to ask better questions

  6. #6
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    174
    The next thing (I would do) would be to put your alien drawing code into a subroutine, something like 'Draw_Alien'. Then call that subroutine from another higher level routine. It would look something like this -

    Code :
    void Display_Scene (float x_aln, float y_aln, float x_can, float y_can)
    {
        glPushMatrix ();
           glTranslatef (x_aln, y_aln, 0.0);
           Draw_Alien ();
        glPopMatrix ();
     
        glPushMatrix ();
           glTranslatef (x_can, y_can, 0.0);
           Draw_Cannon ();
        glPopMatrix ();
     
        etc.......
    }
    The Push and Pop matrix routines cause the glTranslates to only affect the drawing routines directly below them. Without them the glTranslate function that moves the alien would also move the cannon, which you wouldn't want. It is up to you to figure out what values to assign to x_aln, y_can, etc. This could be done computationally or in response to a GUI element or an input device (like the mouse). The motion variables could all be global, in which case you wouldn't have to call Display_Scene with any arguments. But this is not recommended.
    Last edited by Carmine; 07-26-2013 at 02:45 PM.

  7. #7
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    29
    cool thanks for all the help, my cannon is able to move form side to side using the arrow keys.

  8. #8
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    29
    basically I want to draw a sprite and erase it and redraw a different sprite and then erase it and then draw the previous sprite and have all the sprites to move to the right.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •