1. ## 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?

2. 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.

3. Does that mean that the x and y velocities will be the same? That doesn't sound right...

4. Whoops. The y velocity is v*sin( theta ).

5. Ah, got it. Thanks

6. 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

//...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?

7. 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

8. 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. yeah, bubba's solution sounds like it might be easier, but i know mine works.

10. 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. 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

12. 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;```

13. 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.

14. may i see it? I'd like to just mess around with someone else's code

15. 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