Jump to content
  • Advertisement
Sign in to follow this  

Projectile motion in SDL

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, How do I create a projectile motion in SDL? I heard people use the physics equation to calculate the velocity and distance, using gravity=9.8, but how do I use it in SDL? Because the physics is using distance (meters) while SDL is using coordinate. Any suggestions? Thank you

Share this post

Link to post
Share on other sites
You really wouldn't need to emulate things percisely based on real life physics if you want simple projectile motion or something that at least looks like it. SDL will only blit things in whole integers as pixels, so don't bother trying to put something at 9.8 pixels.

Instead, you'd have to follow the general idea of how projectiles fall. I myself like to have a gravity variable and then a variable representing an object's y coordinate velocity. Remember that gravity is always a constant force acting in one direction. So assume you throw this object up in your game, the velocity variable would overpower the gravity variable, going upwards in your intended direction. Now how you make it come down is up to you.

You could either have the velocity variable decrease, have the gravity variable increase, or do both. However, realize that it should be slowly incremented/decremented. This gives you a cheap pseudo gravity that would have to be tweaked a couple of times to make sure it looks like how you want it. To make this more accurate, you should include time in the calculation, because gravity is a consistent force, after so many seconds passed, you will always have fallen by a certain measurement.

Personally from my previous game experiences, I haven't know a single magic formula that does it for me, but playing around with how fast an object falls has been of the best results, more so in frame by frame situations as opposed time itself in milliseconds. Hope that at least gives you an idea.

Share this post

Link to post
Share on other sites
Hi, I adopted your idea and tried it on my program.

It works but I kind of have a calculation problem.

Please refer to part of my code below:

case SDL_MOUSEBUTTONDOWN://mouse pressed
x2=p_event->button.x; //coordinate of the destination of the object
x1=food->getX(); //get coordinate of the original object

angle=180-angle; //get an acute angle

vertical=0.0005; //a figure i get after several of tries
u=0.5; //initial velocity
vx=fabs(u*cos(angle)); //calculate the horizontal and vertical velocity




When I run my program, the projectile works but the direction of the line is not accurate.
For example when I click on the top right of the screen, the object flies to bottom right. I wonder if it is caused by the calculation of angle.

Can anybody tell me why?

Share this post

Link to post
Share on other sites

shouldnt that read


Also you shouldnt need this line:

angle=180-angle; //get an acute angle

As atan2 should return the correct angle between the two points, this should effectively give you the wrong angle.

after this set the velocity correctly using trig:

vx = cos(angle) * speed;
vy = sin(angle) * speed;

and each frame you need to modify vy by gravity in order to simulate the force:

vy += gravity;

That is the most basic way, any more realistic simulations and your going to have to refer to the old physics text book and look up the laws of motion.

One little thing aswell, this isnt technically "creating projectile motion in SDL" you are simply simulating motion, and intending to use SDL to render your results to screen, more specifically, you should ignore that SDL can only render to integer positions and work things out in floats to keep them accurate rounding to the int to draw. Keeping logic seperate from graphics API's is something that seems to be missed alot, but should be kept in mind.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!