# Making 2d Objects move

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

## Recommended Posts

Hey every1, I am a beginner at this so bare with me. I am trying to make a triangle shoot smaller triangles (as a start for a space invader game) I get the first triangle (the ship) to move right and left, but i cant make the smaller triangles (the missiles) to move up. You shoot with space and all that appears is a little triangle above my first triangle. I think i need something like lifetime and movement, tho i got no idea how. It is not in any of the lessons on how to make 2d objects move only rotate. if (keys[VK_SPACE]) //Shoots with SpaceBar { glTranslatef(0.0f, fireMove, -6.0f) ; glBegin(GL_TRIANGLES) ; glVertex3f(0.0,0.4,0.0) ; glVertex3f(-0.03,0.3,0.0) ; glVertex3f(0.03,0.3,0.0) ; } This is what ive used so it would shoot, and ive tried a fireMove += 0.005f but still no movement on it. i have declared the fireMove at the top the same way i declared the xMove (for making the ship go left and right, wich it does perfectly) If any1 have a solution or an idea on how to do this id appreciate it thanks for taking the time all

##### Share on other sites
I would try keeping track of all the triangles postions and velocities, and render them at that position. Something like this:

class Triangle{public:   void update( float dt )   {      x += dx*dt;      y += dy*dt;   }   void draw( )   {      glBegin(GL_TRIANGLES) ;         glVertex3f( x, y+0.4, 0.0 );         glVertex3f( x-.03, y+0.3, 0.0 );         glVertex3f( x+.03, y+0.3, 0.0 );      glEnd( );   }private:   GLfloat x, y, dx, dy;};

I would have gone in more detail, but I have to go [smile]

##### Share on other sites
You have to update the position of your 'missle' every time your display-function gets called.

So your display-function should look something like this:

Display(){//Update Position of misslefireMove+= somevalue//Clear the screen, set up the camera etc....//Draw the MissleglPushMatrix();glTranslatef(0.0f, fireMove, -6.0f) ;glBegin(GL_TRIANGLES) ;glVertex3f(0.0,0.4,0.0) ;glVertex3f(-0.03,0.3,0.0) ;glVertex3f(0.03,0.3,0.0) ;glPopMatrix();//Draw other stuff...//Swap Buffers and call display-function againglutSwapBuffers();glutPostRedisplay();}

(edit:) ok, so i was too late and less informative...

##### Share on other sites
Gee i really wish we had the first batch of the new lessons online now, they would have explained it in more detail exactly how to move stuff around in the best way (specifically lesson 3 and 4).

well anyway, you need an array that holds the height of every shot and possibly also the side to side position, as you want to be able to move that to.

then for each frame and triangle you need to do something like this.

array[...]+=firemovespeed*deltaTime;

where deltatime is derived from is up to the rest of the framework, but it is the time in seconds it took to render the last frame.

use the push and pop matrix commands frequently to make sure everything moves by itself and not relative to something (it would be bad if the bullets moved with the ship after they have been fired).

• 10
• 48
• 12
• 10
• 10
• ### Forum Statistics

• Total Topics
631383
• Total Posts
2999681
×