Jump to content
  • Advertisement
Sign in to follow this  
Beginner_stage

Making 2d Objects move

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

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


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


Link to post
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 missle
fireMove+= somevalue

//Clear the screen, set up the camera etc.
...

//Draw the Missle
glPushMatrix();
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 again
glutSwapBuffers();
glutPostRedisplay();
}



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

Share this post


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

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!