Public Group

# Help with projectile motion

This topic is 3834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I'm working on programming projectile motion, but am having trouble updating different points. I can calculate the different points, but am unable to logically think of a way to plot several points for traveling across the path of projectile motion. Can someone lend me a hand on how to update different points at different times? Basically what I have so far: double v1, v2, v3; double x1, x2, y1, y2, x3,y3; double vox1, vox2, vox3, voy1, voy2, voy3; double vfx1, vfx2, vfx3, vfy1, vfy2, vfy3; double ay, halftime1, halftime2, halftime3, time1, time2, time3; double distance1, distance2, distance3; double X, Y, current, next; //velocities v1 = 60; v2 = 45; v3 = 10; //acceleration in y direction ay = -9.8; //initial velocities in the x direction vox1 = 45*cos(v1); vox2 = 45*cos(v2); vox3 = 45*cos(v3); //initial velocities in the y direction voy1 = 45*sin(v1); voy2 = 45*sin(v2); voy3 = 45*sin(v3); //ending velocities in the x direction vfx1 = 0; vfx2 = 0; vfx3 = 0; //ending velocities in the y direction vfy1 = 0; vfy2 = 0; vfy3 = 0; //calculating the times halftime1 = (vfy1 - voy1) / ay; time1 = halftime1 * 2; halftime2 = (vfy2 - voy2) / ay; time2 = halftime2 * 2; halftime3 = (vfy3 - voy3) / ay; time3 = halftime3 *2; distance1 = vox1 * time1; distance2 = vox2 * time2; distance3 = vox3 * time3; //X = Xo + VoT + .5 A*T(squared) Thanks for looking! [Edited by - dementedxsmurf on July 18, 2008 9:26:51 PM]

##### Share on other sites
Enclose your source code in [ source ] ... [ /source ] (no spaces) to make it more readable to use. Here, I'll do it for you.

double v1, v2, v3;double x1, x2, y1, y2, x3,y3;double vox1, vox2, vox3, voy1, voy2, voy3;double vfx1, vfx2, vfx3, vfy1, vfy2, vfy3;double ay, halftime1, halftime2, halftime3, time1, time2, time3;double distance1, distance2, distance3;double X, Y, current, next;//velocitiesv1 = 60;v2 = 45;v3 = 10;//acceleration in y directionay = -9.8;//initial velocities in the x directionvox1 = 45*cos(v1);vox2 = 45*cos(v2);vox3 = 45*cos(v3);//initial velocities in the y directionvoy1 = 45*sin(v1);voy2 = 45*sin(v2);voy3 = 45*sin(v3);//ending velocities in the x directionvfx1 = 0;vfx2 = 0;vfx3 = 0;//ending velocities in the y directionvfy1 = 0;vfy2 = 0;vfy3 = 0;//calculating the timeshalftime1 = (vfy1 - voy1) / ay;time1 = halftime1 * 2;halftime2 = (vfy2 - voy2) / ay;time2 = halftime2 * 2;halftime3 = (vfy3 - voy3) / ay;time3 = halftime3 *2;distance1 = vox1 * time1;distance2 = vox2 * time2;distance3 = vox3 * time3;//X = Xo + VoT + .5 A*T(squared)

v1, 2, and 3 look more like speeds than velocities. Also, you document what they are when you define them, but I want to know what they are when you declare them.

"vox1 = 45*cos(v1);"
Huh? Now v1 has jumped from velocity to angle? This doesn't make any sense. My guess is 45 is the angle and v1 should by where 45 is. Although, the cos function in C++ uses radians, so you want (45 * PI) / 180 which equals 0.785398163.

You have the same problem with the y axis and sin.

I got thoroughly lost at "//calculating the times". You don't document what you're doing or quote equations that your using, except for X = Xo + VoT + .5 A*T(squared), but I don't even see that one there!

Now, this reminds me a HELL OF A LOT of my physics class, but your code isn't well documented enough and the variables are too cryptic. Two things that will help is

A) Create a vector class. A vector class can be used as a vector or point, but it encapsulates x and y so I don't have to read vox1, which makes no sense. Instead, you could have v1 and v2, which implies v1.x, v1.y, v2.x, and v2.y. But, this is still pretty cryptic.

B) Develop a better naming convention. But, seeing as I don't know what you're doing, I can't advise you on this.

Really, I don't know what you're doing. Is this for a real-time graphical program? Are you trying to get your computer to do your physics for you? What is your gaol application?

##### Share on other sites
I apologize for the inconvenience, I'm pretty new to this site so I'm not 100% sure on how to throw the code into the posts.

The code is supposed to be showing the projectile motions at different angles, 10,45, and 60. I know the variables are confusing and I apologize.

The v1, v2, and v3 are the three velocities.
The vox1, vox2, and vox3 are the velocities in the X direction. The formula is velocity * cos (theta)
The voy1, voy2, and voy3 are the velocities in the Y direction. The formula is velocity * sin (theta)
The halftime is supposed to be the amount of time it takes for the object to reach max height, and time is the total time it took to reach the end range.
The distances are how far the objects go, or the range.

The engine I'm using updates itself, and I'm trying to figure out how to plot or calculate the different points in time to correctly program the projectile motion.

Again, I'm sorry it was so confusing.

Thanks for assisting me.

##### Share on other sites
Why is the ending velocity in the x direction 0? There's no x acceleration to justify this.

Also, you have t four variables you never assign values. Why?

You can make your code more readable by having more descriptive comments, that may even quote the equation you're trying to use, and only try to solve for one thing at a time.

Quote:
 Original post by dementedxsmurfThe v1, v2, and v3 are the three velocities.

As I said, v1, 2, and 3 aren't velocities. A velocity has a speed and direction. These either have only an x, y, or speed, but you can't call them velocities.

You're using an engine? What engine?

"The code is supposed to be showing the projectile motions at different angles, 10,45, and 60."

Can you do this on pencil and paper? Yes?--Then what part are you having trouble translating to the computer? No?--Then what part are you not understanding?

I don't want to say anything more because I don't know how much you know and don't. Whether you've programmed much or are just starting out. Whether the problem is in your physics knowledge or what. I can get you the best help if I know what you need.

##### Share on other sites
I know the physics portion of it. I'm just starting out on programming, and don't know how to proceed. I should have all of the correct physics calculations, but I don't know how to actually type the code to program the correct path the projectile would take. I'm using a drawline function to program the path, and need it to calculate at different points in time along the path.
I'm using an update function that is sort of like a loop. I need to figure out how to update the program to calculate the x and y parts at different points in time to proceed.

##### Share on other sites
Now I can give some better advice:

You're drawing function should look something like this (pseudo code--I won't write it for you):
/* Input: velocity x and y at time zero. */void drawProjectile( v0x, v0y, ){    // The time    float t = 0;    // The coordinates of where the object is at different times.    float x[4];    float y[4];    // For each of the time intervals...    for( i = 0...3 )    {        t = whatever;        getCoords( t, v0x, v0y, x, y );    }    // Draw the lines and crap.}

Remember, that won't compile. I want you do write it, and if you can't, you're not ready and need to learn more. You might also find it's a bad solution for you, but I'm hoping it'll at least be helpful.

But, of coarse, I didn't tell you how to do the equation stuff. I think you should handle that in its own function (again, pseudo code):
/* This returns nothing! It updates the coordinates by reference. */void getCoords( t, v0x, v0y, x, y ){    // Do calculations.    // Because we're returning by reference,     // we only need to assign the variables to return them.    x = resaultX;    y = resaultY;}

Try working with that. If you still can't do it, this shows a lack of understanding in C++ or programming. This is fine and simply means you need to learn and practice more.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015500
×