Thread: Projectile C++ and OpenGL

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Unhappy Projectile C++ and OpenGL

    Code:
    void Update(float dt) 
    	{
    		Vector3 acceleration;
    		Vector3 Vi, Vf, W;
    		Vi.set(5, 20, 10); 
    		Vf.set(400, 200, -900);
    
    		acceleration.set(0, -32.2, 0);				// acceleration (x,y,z)=(0,-2,0)
    		
    		vel = Add(vel, acceleration);			//velocity = velocity + acceleration
    		vel.scale(dt);							//velocity = velocity +a*dt 												
    		
    		W = Add(Vf, Vi);                 		//position = position + (1/2)*(v_f +v_i)*dt												
    		pos = Add(pos, W);						//updates position = position + vel
    		pos.scale(0.5);							//This multiplies pos by 0.5, i.e. W = 0.5 * W             
    		pos.scale(dt); 
    
    	}
    I'm working on a cannon that shoots a cannon ball in OpenGL. I get the ball to move in a line but no arc. The ball is suppose to shoot up and come back down. The two formulas I have to use are:

    velocity = velocity +a*dt
    position = position + (1/2)*(v_f +v_i)*dt
    (Note: try not to use 1/2 in C++, but rather use 0.5)

    You will use -32.2 for the second component of acceleration.

    In my code above I can't figure out why I don't have the ball going in parabolic form. So basically I just need some tips on the position formula. I'm basically confused with the formula and how it's suppose to do the parabolic. It looks like the code has everything in the formula but not sure if I'm taking the correct steps in the formula. If some one could break down the formula step by step that would be awesome!

    Do you add the position to (.5) then * (v_f + v_i) * (dt)?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    position = position + velocity*dt since, after all, velocity has the same relationship to position (rate of change) as acceleration does to velocity.

    I don't know what your v_f and v_i are supposed to be -- they lead me to think of "final velocity" and "initial velocity", but in this case you have no idea what the final velocity could possibly be.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    Yeah I was thinking the same thing just not sure how I would float the final velocity.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't know what the final velocity is. That's why you don't use that formula, but instead use position = position + velocity*dt. (If nothing else, you need to be using a changing value for velocity, otherwise you have no acceleration.)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    More cross-posting madness and selfishness
    Projectile OpenGL With C++ - C And C++ | Dream.In.Code

    READ THIS!!!!!
    How To Ask Questions The Smart Way

    Stop clogging up multiple forums with the same question at the same time. You're WASTING valuable help time by running around with the "me me me me me" attention grabbing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Salem View Post
    You're WASTING valuable help time by running around with the "me me me me me" attention grabbing.
    You're the only one looking for attention here. Get over yourself. They're completely unrelated, unaffiliated forums on different domains.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read how to ask a question, and stop assuming yours is the only question on the net worth answering!

    Actually, you're not even the OP, you're just a thread jumping troll that just likes to sound off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed