Implementing Projectile Motion in OpenGL

BANANAMAN

Resident Star Battle Expert.
Reaction score
150
Ok so apparently I have to finish this particular project that shows a projectile that follows the laws of projectile motion by July 11 and I'm having trouble figuring out how to go about arranging the logic flow.

So far I have a gravity component that works and a firing mechanism that sets the projectile's angle from the ground.

I'm missing it's X velocity,Y velocity before it reaches it's maximum height and the computation for max height.

I understand that the computation for it's X velocity,Y velocity is always done with respect to time but how would I go about wording the logic flow so it doesn't either stop halfway,doesn't compute at all or only computes once.
 

Attachments

  • ProjectileMotion.zip
    3.8 MB · Views: 919

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I'm not quite sure what the exact problem is, but maybe everything gets easier when you rearrange your code a bit.
You're using C++, yet your code looks like it C.

You should use classes and member variables so you can:
1) Get rid of a lot of these global variables
2) Turn code into smaller chunks
3) Hopefully get rid of #define macros :/

Code:
class Bullet{
 
public:
    Vector3 currentPosition;
    Vector3 currentVelocity;
 
    bool moving;
    bool fired;
    bool reloaded;
 
    void render(double timeStep){
        if(!fired) //We don't want anything to happen if bullet isn't fired yet
            return;
 
        if(moving){ //We only want to calculate the new position if the bullet is moving
            currentPosition += currentVelocity * timeStep;
            /* set currentVelocity with respect to air friction, gravity and magic */
 
            if(currentPosition.z <= 0.0){ //If the z-coordinate is 0 or less (= if it hit the ground)
                currentPosition.z = 0;
                moving = false; //We want it to stop moving
            }
        }
 
        //Draw
        glBegin(GL_TRIANGLE)
        glTranslate(currentPosition.x, currentPosition.y, currentPosition.z);
        DrawCube();
        glEnd();
    }
 
    //Constructor only sets everything to 0.
    Bullet()
        :    currentPosition(0,0,0),
            currentVelocity(0,0,0)
            moving(false),
            fired(false),
            reloaded(false),
    {}
};
 
Bullet g_bullet;
 
void main(){
    g_bullet.currentVelocity = Vector3(5,0,1); //Starting velocity
    g_bullet.fired = true; //Start the bullet.
}
 
void Render()
{
 
    g_bullet.render( 0.01 ); //Bullet travels 0.01 time units every frame.
 
    glutSwapBuffers();
}

Also, here a pastebin: http://pastebin.com/bGZbFZti
Because syntax highlighting is cool.

By the way, for an outside person it's pretty hard to make sense of variables like angle, angle2, rc, etc.
If you have to use global variables, you should give them a descriptive name and write a short comment about what it's used for. At least for the ones where it's not as obvious.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top