Drag/Friction

Started by
11 comments, last by miles vignol 22 years, 9 months ago
I've got a 2D system where collisions take place and forces are redistributed to the colliding objects. Everything is great, but I wanna introduce a drag/friction component to my system. Actually, I wanna introduce a REALISTIC drag/friction component. My problem is that my game is not locked to a certain iteration cycle, that is, it might be 10 milliseconds from one update to the next, or perhaps 3, or maybe 45 -- depends on drawing speed. I'd like to figure out an equation that makes sense in this sorta system. What I currently have is: drag = pow(DRAG_FACTOR, 1 + time_frame*10); velocity = velocity / drag; time_frame is how many seconds have elapsed since my last update call. generally, it's around 0.01-0.02... the DRAG_FACTOR is currently 1.07 which stops things pretty quickly (which is what I want)... This works, but it seems a little kludgy to me. I'm also interested in limiting the velocity for each entity. Should I do it in the drag equation (sorta terminal velocity) or in the accelleration application to velocity? If the latter, what's a good technique? Right now I'm simply adding my accelleration to my velocity in an ever increasing way, then just clamping it at a specific value which also seems kludgy. Any thoughts? Edited by - miles vignol on July 23, 2001 7:08:46 PM
Advertisement
First it''s far easier to do this is you have a fixed time frame: calculations are simpler it''s especially difficult to debug a time frame depending on a frame rate that can vary greatly depending on hardware and OS. I would suggest seeing if you can put the dynamics in a seperate thread from the rendering as in the long run it will simplify things. E.g for your example instead of using expensive functions like pow you can use simpler linear expressions such as:

v = v - k * v

As for terminal velocity, this should work with a fixed time frame: with fixed frame rate drag is a constant, and as velocity increases the amount of velocity lost to drag each time step also increases. Eventually it matches the thrust/accleration, so it cannot go any faster. With variable time frame it should still work, though it may be destabilised by a variable frame rate.

One thing you can try is using quadratic drag. I.e. make the drag proportional to v2 instead of v. This is more realistic and has the benefit that it cuts off high speeds much more quickly. It has the opposite effect at low speeds though - things moving slowly are much less effected - so it''s often best to use a combination of both v and v2. Something like (in 1D)

v = v - k * v - m * v2

John BlackburneProgrammer, The Pitbull Syndicate
I think that it is a good idea of using two different friction formulas for static friction (v in [0,some threshold]) and dynamic friction (v in [threshold,inf]). Simply because formula that behave well with high speeds tend to behave strangely at low speeds and vice versa. You could even introduce a third friction term: high speeds, low speeds, static. Is is even more realistic, since you could introduce seperate dynamic and static friction coefficients, which is realisitic (it is far easier to accelerate an object already moving than one that is standing).

- AH
Thanks for the input. I guess the main things I need to play with are the coefficients. It gets a little more complicated because it seems a little silly to apply a heavy drag opposite the direction of accelleration.

This is for a game that is tank based. I used to have a simpler movement scheme that felt very good, but was not very good from a physics standpoint -- collisions and forces didn''t exist well with the movement system. I''m trying to get the feel of my old movement system back using a more realistic approach that fits with the rest of the package.

I suppose I could break my drag into a weak drag (wind resistance or something) and a strong drag (ground friction). The friction component should be reduced for the direction of accelleration, I would think, but I''m not sure how. I''ve implemented a simple increase in the coef when "power sliding" (moving counter to the tread direction), but it''s all very kludgy still...

I''ll play around with reformatting by velocity equations to be more like the ones mentioned above, rather than simply recursively multiplying them... maybe something will strike me...

thanks again.
If dynamic_friction_value is the value you''ll be using in your equations and static_friction_value is the friction amount assigned to a surface, then:

dynamic_friction_value = static_friction_value / FPS
Sorry, I missed what you said before:

quote:
drag = pow(DRAG_FACTOR, 1 + time_frame*10);


But anyway, there''s a problem with frame-rate dependant physics. Different frame rates result in different floating point rounding errors, so your movement will be different at different frame rates. This was exploited in Quake III until a patch was released making physics entirely independant of framerate.
yeah, my goal is to make the game function identically no matter what the "view rate" is. At the moment I''m cheating by breaking down large time intervals into shorter ones, but I''d like to keep this at a minimum...
miles, I'm not sure why you write,

velocity = velocity/drag;

I mean, you do reduce the velocity but that really isn't a true drag or friction. That is, a drag or friction is a force that affects acceleration, and thus indirectly affects velocity. Your approach may work if you get the coefficients worked out, but is not a drag or friction force. Its great as long as it does what you want.

A true drag acts opposite velocity not acceleration.

Why not apply a viscous drag, which is like an aerodynamic drag? It may work well for a varying time step. Apply the viscous drag force along a vector direction opposite the direction of velocity over whatever time step your current frame produces. Viscous drag naturally goes to zero as velocity goes to zero, so it scales naturally.

The magnitude of viscous drag can be calculated using this formula:

  viscous_drag_magnitude = C_d * rho * vel * vel * ref_area  


Here, C_d is a drag coefficient. For blunt bodies (i.e., spheres or similar) at low speeds start with C_d = 1.0 and tweak. rho is the density of whatever fluid the object is moving in (use rho = 0.0023769 slugs/cubic foot or 1.225 kg/cubic meter for air at sea level) vel is the magnitude of velocity of the object relative to the fluid (just the magnitude of velocity in world space if the fluid itself is not moving----i.e., there is no wind). Make sure vel is consistent with your units for rho. And ref_area is a reference area, for a blunt body use the frontal projected area---there are of the object as projected onto a plane towards its front side. Make sure the units are consistent (if you use rho in slugs/cubic foot, ref_area should be square feet. If you use kg/cubic meter, ref_area should be square meters.) Drag will be in pounds if you're using slugs and feet and it will be in Newtons is you're using kg and meters.

(For units people, 1 pound is equal to 1 slug-foot/second_squared, and 1 Newton is equal to 1 kilogram-meter/second_squared----which you can figure since F = ma and the units on both sides of "=" have to be the same.)

Anyway, this approach may be more than you want to do but it might work.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Edited by - grhodes_at_work on July 25, 2001 10:41:48 AM
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Dang.

Actually, that''s all very helpful. I''m not sure I''m gonna bother with the some aspects -- I''ll likely have a composite drag coef that gets my results rather than rho and ref_area.

My total problem is this:

I have a tank with essentially instant acceleration (no throttle). The velocity is then affected by this acceleration. I''d like to be able to specify a max speed and an acceleration amount. For example, lets say a max speed of 35 units per second and an acceleration of 10 units per second^2. I don''t want the acceleration to be linear and then just stop once the tank is moving at the max speed. I''d like it to ramp in. I also want any residual velocity from collisions or sharp turns to die out very quickly (so the tanks don''t act like hockey pucks). Unfortunately this tends to make the tank''s terminal velocity very low, so it seems I need to affect the drag/friction differently based on the direction of acceleration. Since the treads are the real point of contact, I could probably base my math on the tread speed, then apply the tank acceleration as a "childed" space or something...

It''s all a lot more annoying than I thought it would be. I''ve generalized a lot of my physics code, so I''m trying to get a handle on all the hacks to get them to be more in line with real world units and concepts. The drag system came from a simple iterative percentage loss of speed, then moved to a power function when I altered my timing code. Now I''m trying to replace it completely... The nice thing about it, tho, is that it''s accurate over an arbitrary time frame. Since the velocity is used in a drag formula, and drag influences velocity, the iteration size would seem to be important...

My head is swimming...

ok, here's how it works. x=x'+v*dt+a*dt^2, dt being time increment. that's euler integration, which i assume is what you're using, although obviously without the time step. it's horrible for anything attempting to even remotely approximate real world physics, you should look into verlet or leapfrog integration, but i digress. dt = t - t', or current time stamp minus the last frame's time stamp. that would be your "10 milliseconds or perhaps 3 or maybe 45."
now, here's how you would apply the drag force as explained by graham. as you might remember, a=F/m. F here, for all intents and purposes, is the combined force acting on the object. in other words, you add up all the force vectors that you're dealing with and apply them to the object as follows(still using euler. definitely look into verlet, P* = 2P-P'+a*dt^2, it'll make your life a lot easier)

P += V;
V += A;

if you want the drag equation to work properly, you definitely don't want to put ref_area into a composite drag coefficient. it depends on which way the object is moving relative to its local space, in other words, a sheet of cardboard dropped parallel to the ground will encounter a lot more drag than a sheet of cardboard dropped perpendicularly to the ground. a simple approximation (an incorrect one, but it'll work) would be to project the geometry onto a plane perpendicular to the direction of travel and compute the area of the boolean OR of the resulting polygon set.

also, you seem to be confusing drag with friction.. they aren't the same thing.

-goltrpoat


PS - just realized that you're in 2d, not 3d.. so disregard what i said about the plane projections in regards to the reference area

Edited by - goltrpoat on July 25, 2001 12:30:05 AM
--Float like a butterfly, bite like a crocodile.

This topic is closed to new replies.

Advertisement