Advertisement

Horizontal throw with air resistance

Started by November 28, 2015 05:25 PM
15 comments, last by IsItSharp 9 years, 2 months ago

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?

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?
Advertisement

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 ?

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


And where did that 0.5 come from?

And where did that 0.5 come from?

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

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' ;)
Advertisement

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

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);
		}

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

And where did that 0.5 come from?

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


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.

This topic is closed to new replies.

Advertisement