Angle/Trajectory

This is a discussion on Angle/Trajectory within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I've been trying to figure out how to calculate a trajectory based on angle and speed, like you might do ...

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Question Angle/Trajectory

    I've been trying to figure out how to calculate a trajectory based on angle and speed, like you might do for artillery or something...

    From what I've read so far, you can calculate x and y positions like this:

    y = y_velocity*time - 9.8*t^2/2
    x = x_velocity*time

    Is that correct?

    Where I'm lost is how to get the x and y velocities based on the angle to the ground and the force that is applied...

    Anyone have any ideas, or is there a better way to do this?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Assuming that you are on flat ground, then the x velocity is v*cos( theta ), and the y velocity is v*sin( theta ), where v is the overall velocity, and theta is the angle.
    Last edited by XSquared; 12-14-2003 at 02:43 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Does that mean that the x and y velocities will be the same? That doesn't sound right...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Whoops. The y velocity is v*sin( theta ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ah, got it. Thanks
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    A bit of a bump, sorry, but I tried to translate this into code, and it seemed best to put this in the original thread. Heres where I've got:
    Code:
    float y_v;
    float x_v;
    float v=20
    float Start;
    float theta=45
    float y_v_0=0; 
    
    
    //...get start time
    Start = GetTickCount();
    
    
    //...calculate x and y initial velocity
    x_v=v*cos(theta*3.14/180);  //cos and sin take radians
    y_v_0=v*sin(theta*3.14/180);//degree->radian= theta*pi/180
    
    
    //...Move the object (this section is being called over and over in a loop)
    float t=(GetTickCount()-Start)/CLOCKS_PER_SEC; //basing it on time
    y_v=y_v_0-9.8*t;
    BallX=x_v*t;
    BallY-=y_v;
    When I run the program, my ball (that's the object) moves in a parabola, but the angle looks more like 80+ degrees than 45 degrees...Any idea where I'm going wrong?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    firstly i don't know what clocks per sec is

    secondly the distance travelled taking acceleration due to gravity into account is the average velocity for a time period multiplied by the time passed. Basically, change that -9.8t to -4.9t^2, the t^2 in this case is the time passed since the last time you moved the ball.

    9.8t gives the instantaneous acceleration but not the distance travelled.

    It works like this:

    distance travelled = Vav * time

    Vav = (speed1 + speed2) / 2

    speed1 = 9.8t (assuming t is still the time OF THE OLD FRAME)
    speed2 = 9.8(t+deltat), delta t is the time that passed this frame

    it ends up being equivalent to 4.9t^2, as I stated above.

    EDIT: Just so you know, this is how I do it in my 3D computer game, and it works perfectly
    Last edited by Silvercord; 12-16-2003 at 07:17 PM.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Just increase your y velocity by a set amount - it does not have to be exactly what gravity is or you could figure out what gravity's acceleration would be in relation to your world and your desired framerate.

  9. #9
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    yeah, bubba's solution sounds like it might be easier, but i know mine works.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Yeah yours does....but I got that idea from Andre Lamothe and he has a point. Who cares what gravity really is...as long as it looks real enough no one will know.

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well in this case I want to keep it consistent with gravity...The only way I've been able to get my path to look right (not sure if it is truly correct) was like this:
    Code:
    float Dt = t;
    float O_y_v = y_v;
    t=(GetTickCount()-Start)/CLOCKS_PER_SEC;
    y_v=y_v_0-9.8*t;
    float d = ((O_y_v+y_v)/2)*(t-Dt); //Old y vel + y vel / 2 == average
    //d = vt and new_time - old_time = total_time 
    //d = (old_y_vel + new_y_vel)/2 * (new_time-old_time)
    BallX=x_v*t;
    BallY-=d;
    Only problem is it doesn't move at the correct speed...I don't understand! Now what am I doing wrong?

    Edit: Ah, I think I understand the problem...acceleration is 9.8 m/s^2, and I am assuming a pixel is a meter...

    Ah, my brain hurts...there must be a way to speed it up without changing the actual path...no matter, I'll think of it eventually...Thanks, guys, and be sure to correct me if I'm wrong
    Last edited by JaWiB; 12-16-2003 at 09:40 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Jawib, I think all you need to do is figure out a conversion from world units (in your computer program), to feet. I've got a macro called FEET, so when I want 32 feet I do FEET(32), and that is what just calculates the conversion from feet to world units. Otherwise I'm looking at your code more closely, it's hard to read.

    Yeah yours does....but I got that idea from Andre Lamothe and he has a point. Who cares what gravity really is...as long as it looks real enough no one will know.
    Okay, I thought you were referring to something different

    EDIT:
    Jawib, I think I found some errors in your code. I re-wrote it. Here's what I updated, it shows what i was trying to explain, and I tried commenting for you. You may need to change 9.8 to something that offers a conversion from meters to arbitrary world units in your program
    Code:
    //discard for now
    //y_v=y_v_0-9.8*t; 
    //float d = ((O_y_v+y_v)/2)*(t-Dt); //Old y vel + y vel / 2 == average
    //d = vt and new_time - old_time = total_time 
    //d = (old_y_vel + new_y_vel)/2 * (new_time-old_time)
    //float O_y_v = y_v; //old y vel
    
    float oldt = t; //this is the 'old time' from last frame
    
    t=(GetTickCount()-Start)/CLOCKS_PER_SEC; //total # seconds passed since start time 
    float	deltat = t-oldt; //time now minus time last frame 
    float	d = ((9.8*oldt) + (9.8*(oldt+deltat))) / 2; //average velocity 
    d *= deltat; //average velocity multiplied by time, I put this on a second line to make it more readable
    BallX=x_v*t;
    BallY-=d;
    Last edited by Silvercord; 12-17-2003 at 05:46 AM.

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well what I had worked...all I had to do was make time move faster:
    Code:
    t=(GetTickCount()-Start)/CLOCKS_PER_SEC*Speed; //I set speed to 6.5
    I'm pretty sure it was accurate too...I made it so I can change the velocity and angle from within the program and it looked right

    Anyway, maybe I'll try your method when I get home.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    may i see it? I'd like to just mess around with someone else's code

  15. #15
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well if you want...I'm not the best at writing beautiful code though...Anyways I was using my own tga file loader class and you will have to change the path/file name if you want the object to show up. I didn't include my "ball" image, since it sucks
    Attached Files Attached Files
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21