Working out peak of a curve
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Working out peak of a curve

  1. #1
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447

    Working out peak of a curve

    ok am making a flash game and need some help in writing a formula for it

    basically user will select power and angle
    character will then jump depending upon the values user entered

    i need however to figure out depending upon power, angle and a fixed value for gravity where the peak of a curve will be so that character then starts to fall again

    so that the characters x/y postion increases until they reach peak of curce and then start to decrease again

    what equation could i use to calculate this?

    v_Ln

  2. #2
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    to give you some idea what i mean look at attached image

    ok red line :
    user enters high power setting & low angle setting

    blue line :
    mid power setting mid angle setting

    green line :
    high power setting high angle setting

    you can see that depending upon the values entered by user shape of curce changes - now i will know the values for Power (P), Angle (A) and Gravity (G) but what equation could I use to figure out the peak at which the upward acceleration stops and the user then begins to fall down again?

    v_Ln

  3. #3
    Senior Member
    Join Date
    Oct 2002
    Posts
    1,130
    Wow. Long time since I took calculus. I'll try to answer, though.

    At the peak of a curve, an object is neither accelerating or decellerating (vertically). To find the rate of acceleration, you need to find the first derivative of the function which calculates the object's movement. This derivative, rather than plotting height as a function of time, will plot acceleration as a function of time.

    The peak of that curve will be where the first derivative crosses the x-axis, or where acceleration is equal to zero. The value of x at this point indicates the time at which this occurs.

    Now, as for how to incorporate that into a flash animation, I have no idea you may need some higher math libraries. I don't even know if this will help, but wanted to see how good my calculus skills still are.

    I think I'm right. Somebody correct me if I'm wrong.

    <edit>
    to give you some idea what i mean look at attached image
    What attached image?
    </edit>
    Government is like fire - a handy servant, but a dangerous master - George Washington
    Government is not reason, it is not eloquence - it is force. - George Washington.

    Join the UnError community!

  4. #4
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    heh image is now attached - lol soz for that &gt;.&lt;


    hmm wonder if I could use something like :

    Power (P) = 50
    Angle (A) = 45
    Gravity (G) = 5

    B = P-G
    C = A-G

    then make B & C equal to P & A
    and then add B & C onto characters current x + y postions

    they wil start out as positive values but as am constantly taking G away from them will eventually become negative values and character will start a downward arc......would just be a matter of getting a seemingly correct value for G so that the arc falls at aprox the right time.

    v_Ln

  5. #5
    Senior Member
    Join Date
    Oct 2002
    Posts
    1,130
    Hmmm...

    Unless you have wind or something similar (I'm thinking Scorched Earth-type game here?), the horizontal velocity will remain constant. So, I think you will need to calculate a horizontal velocity and a vertical velocity from the initial power and angle variables, like

    [vH = P cos A]; should give the horizontal velocity. This will remain constant.

    [vV = P sin A]; should give the initial vertical velocity, which will be reduced by G for every loop iteration.

    And the loop would look something like this:
    do {
    X = X + vH;
    Y = Y + vV;
    vV = vV - G
    } while Y &gt;= 0

    The top of the curve should then be where Y equals 0 (or shortly before it becomes less than zero). Maybe calculus isn't needed after all, but I don't think you'll get away without trigonometery.
    Government is like fire - a handy servant, but a dangerous master - George Washington
    Government is not reason, it is not eloquence - it is force. - George Washington.

    Join the UnError community!

  6. #6
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    ok code i came up with

    Code:
    //set inital variables
    onClipEvent(load) {
    	alt = 30;
    	speed = 30;
    	gravity = 2;
    }
    
    /next part runs every time frame loads (12fps)
    onClipEvent(enterFrame) {
    	Ymov = alt-gravity;
    	Xmov = speed-gravity;
    	
    	this._y -= Ymov;
    	
             //added next part in to stop pause looking as bad when equals 0
    	if (Xmov == 0) {
    		this._x += 3;
    	}
    	
    	if (Xmov &gt; 0) {
    		this._x += Xmov;
    	}
    	if (Xmov &lt; 0) {
    		this._x -= Xmov;
    	}
    	alt = Ymov;
    	speed = Xmov;
    }
    works kinda but gives a very unatural looking curve - i knew i should have listened more in maths.....to think i took bleedin advanced maths as well, seems like 100 yrs ago now

    v_Ln

  7. #7
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    btw thought would add as am doing this in flash it adds its own lil quirk to it

    the top left hand corner of screen is 0,0
    so as you move -&gt; x increases
    and as you move ^ y decreases - just to make things extra simple &gt;.&lt;

    v_Ln

  8. #8
    Senior Member
    Join Date
    Oct 2002
    Posts
    1,130
    I think causing X movement to be reduced by gravity would cause some problems. I imagine the downward side is steeper than the upward side, perhaps doubling back on itself?

    Discounting wind and air resistance (which I doubt you're adding in yet at this point), the horizontal velocity should remain constant. Something like Xmov = Xmov + speed might work better.

    Code:
    //set inital variables
    onClipEvent(load) {
    	alt = (whatever);
            Xpos = (whatever);
    	speed.horizontal = (power cos angle);
            speed.vertical = (power sin angle);
    	gravity = 2;
            TopReached == Flase;
    }
    
    /next part runs every time frame loads (12fps)
    onClipEvent(enterFrame) {
            /Set the initial position
            this._y == alt;
            this._x == Xpos;
    
            /determine how far to move
    	Ymov = speed.vertical;
    	Xmov = speed.horizontal;
    
            /move the object
    	this._y -= Ymov;
    	this._x += Xmov;
    
            /adjust vertical speed as a function of gravity
            speed.vertical -= gravity;
    	
            /has the top been reached?
            if (speed.vertical &lt;= 0 && !TopReached)
            {
              TopReached == True;
            }
    }
    Then checking the TopReached flag will tell you when the top of the curve has been reached.

    Does that make any sense?
    Government is like fire - a handy servant, but a dangerous master - George Washington
    Government is not reason, it is not eloquence - it is force. - George Washington.

    Join the UnError community!

  9. #9
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi


    If I understood your coordinate system correctly, it is

    (000,000) ------ (320,000)
    | .............................. |
    | .............................. |
    | .............................. |
    | .....x....................... |
    (000,200) ------ (320,200)


    Let's say, there is a guy waiting at (50,180),
    which is somewhere in the left bottom region.

    I would suggest as the three start-parameters:
    - velocity (v0) (instead of "power")
    - angle (alpha)
    - gravity (g)


    Then:
    -----
    I call startposition x0=50
    I call startposition y0=180

    Take a small "stepsize" (timestep) dt = 0.5 (or something like that. Adjust this for
    the correct "feeling").

    Code:
    repeat
      t=t+dt;
    	1. x coordinate: x(t) = x0 + v0*cos(alpha)*t
    	2. y coordinate: y(t) = y0  - [ x(t) - x0 ]*tan(alpha) + g/(2*v0*v0*cos(alpha)*cos(alpha))*[ x(t) - x0 ]*[ x(t) - x0 ]
     
    until (floorreached(y(t))) || (bumbed_in_a_wall(x(t))).


    You can test it as follows: If the floor is straight (no steps, ...), then the x(tmax) you have
    reached should be:

    x(tmax) = x0+v0*v0*sin(2*alpha)*sin(2*alpha)/g.

    The maximal y-height that can be reached is
    y_maximal = y0-v0*v0*sin(alpha)*sin(alpha)/ (2g).


    This should give you the correct parabolic motion. I hope, this was not too technical.
    I tried to adjust the coordinates to your y-reversed coordinate system

    Have fun!

    /edit: In case you want to incorporate the airflow, I could elaborate ( ) about it.
    In short: the additional force (acceleration) is proportional to the area of the
    object (person, ie. feetsize ) times the velocity squared.

    /edit2: a little error in the main formula corrected.
    Reasonable initial conditions (example)
    v0=10
    alpha=Pi/4
    g=1
    t=0..14

    Cheers
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  10. #10
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Just so you know, the coordinate system with 0,0 top left isn't really weird. It's the coordinate system used in all (AFAIK) computer graphics. Any graphics coding I've done has used that coordinate system anyhow.

    ac

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides