Jump to content
• Advertisement

# Horizontal throw with air resistance

This topic is 955 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

I am trying to create a simple simulation where a ball is thrown in x-direction and the gravity pulls the ball to the ground.

I have trouble to apply the appropriate air resistance in my calculation, which looks like this at the moment:

this.xVelocity = 5 - ((0.18 * 0.1*(1.2/2)*this.xVelocity*this.xVelocity) / 5)*this.seconds;
this.xpos += 0.5*this.xVelocity * this.seconds;


5 is the velocity of the ball in x-direction.

this.yVelocity += 9.81*this.seconds - ((0.18 * 0.1*(1.2/2)*this.yVelocity*this.yVelocity) / 5)*this.seconds;
this.ypos = 0.5*this.yVelocity*this.seconds;


Where 0.18 is the coefficient of resistance (c), 0.1 is the surface area of the ball (10*10cm), 1.2 is the air density and 5 is the mass of the ball

Am i doing this right?

Edited by IsItSharp

#### Share this post

##### Share on other sites
Advertisement
The assignment to this.yPos should be a +=' instead. Other than that, for the particular simulation you are doing, maybe this is OK. But the signs are not going to work if either xVelocity or yVelocity is negative.

Why the factor of 0.5 in the position update?

#### Share this post

##### Share on other sites

The assignment to this.yPos should be a +=' instead.

Oh, thanks :)

Why the factor of 0.5 in the position update?

Hm, because of s=0.5*v*t ?

#### Share this post

##### Share on other sites

Hm, because of s=0.5*v*t ?

And where did that 0.5 come from? Edited by Álvaro

#### Share this post

##### Share on other sites

And where did that 0.5 come from?

I don't know, it is just a formula?

#### Share this post

##### Share on other sites
Because the ball moves under constant acceleration (gravity), you can get it's position at any time by this formula:

displacement = (vel * time) + 0.5 * acc * time*time

Guess, that's where the 0.5 comes from... maybe.
But looking at the code it seems you want to integrate with constant timestep, like usual in games:

this.vel += acc * timestep;
this.pos += this.vel * timestep;

To model air resistance, you could simply damp velocity: this.vel *= 0.98

... hope this helps. The math should be simple enough to become second nature without the need to trust in some 'formula' ;)

#### Share this post

##### Share on other sites

To model air resistance, you could simply damp velocity: this.vel *= 0.98

But that would not be realistic because actually the air resistance is dependent on the velocity^2

So that is kind of a problem:

- I have to calculate the velocity of my object (which would be: (9.81-air resistance)*time) but to calculate the air resistance i need the velocity

Edited by IsItSharp

#### Share this post

##### Share on other sites

But that would not be realistic because actually the air resistance is dependent on the velocity^2

Ok, i've read the wikipedia article and wrote this code, hope it's correct.
It uses integration, analytical solution would be harder but i guess integration is what you want.

float airDensity = 11.0f;
float dragCoefficient = 0.1f;
float ballRadius = 0.5f;
float dragForceConstant = 0.5f * airDensity * dragCoefficient * PI * ballRadius*ballRadius;

float px = 0, vx = 7.5f; // ball pos and vel
float py = 0, vy = 3.5f;
float ax = 0, ay = -9.81f; // gravity acceleration

float timestep = 1.0f/60.0f;

for (int i=0; i<100; i++)
{
float dragAccX = vx*vx * dragForceConstant * (vx > 0.0f ? -1.0f : 1.0f);
float dragAccY = vy*vy * dragForceConstant * (vy > 0.0f ? -1.0f : 1.0f);

vx += dragAccX * timestep;
vy += dragAccY * timestep;

vx += ax * timestep;
vy += ay * timestep;

px += vx * timestep;
py += vy * timestep;

RenderPoint (3, sVec3(px, py, 0), 1,0,0);
}


#### Share this post

##### Share on other sites

I have to calculate the velocity of my object (which would be: (9.81-air resistance)*time) but to calculate the air resistance i need the velocity

Ah, that's one of those "What was first, chicken or egg?" questions.

If you look at my code, i've chosen to handle drag first, then gravity.
You can reverse the order and you'll see the results are only slightly different - it should not matter in practice.
You could improve this with more complex integration methods (average of both orders f. ex.).

#### Share this post

##### Share on other sites

And where did that 0.5 come from?

I don't know, it is just a formula?

I don't have the patience for this conversation. There's no such thing as "just a formula". A formula is just a sentence in mathematical language. It means something, and things are in it for a reason. If I am asking you why there is a 0.5 in the formula, you need to tell me something like where you got the formula, or how you came up with it. The velocity is the derivative of the position, not twice the derivative, so there is no need to multiply by 0.5. Edited by Álvaro

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5
• Advertisement

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×

## 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!