Drag/Friction
Started by miles vignol, Jul 23 2001 12:07 PM
12 replies to this topic
#1 Members  Reputation: 122
Posted 23 July 2001  12:07 PM
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.010.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
#2 Members  Reputation: 351
Posted 23 July 2001  11:06 PM
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 v^{2} 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 v^{2}. Something like (in 1D)
v = v  k * v  m * v^{2}
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 v^{2} 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 v^{2}. Something like (in 1D)
v = v  k * v  m * v^{2}
#3 Anonymous Poster_Anonymous Poster_* Guests  Reputation:
Posted 24 July 2001  03:05 AM
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
 AH
#4 Members  Reputation: 122
Posted 24 July 2001  08:00 AM
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.
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.
#6 Members  Reputation: 142
Posted 24 July 2001  09:10 AM
Sorry, I missed what you said before:
But anyway, there''s a problem with framerate 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.
quote:
drag = pow(DRAG_FACTOR, 1 + time_frame*10);
But anyway, there''s a problem with framerate 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.
#8 Moderators  Reputation: 1373
Posted 25 July 2001  03:35 AM
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:
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 movingi.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 areathere 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 slugfoot/second_squared, and 1 Newton is equal to 1 kilogrammeter/second_squaredwhich 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
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:

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 movingi.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 areathere 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 slugfoot/second_squared, and 1 Newton is equal to 1 kilogrammeter/second_squaredwhich 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
#9 Members  Reputation: 122
Posted 25 July 2001  01:07 PM
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...
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...
#10 Members  Reputation: 122
Posted 25 July 2001  05:26 PM
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* = 2PP'+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
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* = 2PP'+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
#11 Members  Reputation: 122
Posted 25 July 2001  08:29 PM
I''m not really confusing drag with friction, so much as just trying to combine the two. Perhaps that''s not a good idea. My root problems are:
1. trying to get rid of velocity in undesired directions (sliding)
and
2. trying to keep the tanks from accelerating to infinite speeds.
I figured a combined drag/friction model would be able to address both of these issues.
I suppose I could ditch the drag concept and work purely with friction as a momentum stealing force. Then I could invertly relate acceleration to velocity. That''d give me a clean cap on speed with a semi natural acceleration falloff as speeds increase.
I play around with it some more (damned Diablo 2 expansion pack!) and see where it takes me...
1. trying to get rid of velocity in undesired directions (sliding)
and
2. trying to keep the tanks from accelerating to infinite speeds.
I figured a combined drag/friction model would be able to address both of these issues.
I suppose I could ditch the drag concept and work purely with friction as a momentum stealing force. Then I could invertly relate acceleration to velocity. That''d give me a clean cap on speed with a semi natural acceleration falloff as speeds increase.
I play around with it some more (damned Diablo 2 expansion pack!) and see where it takes me...
#12 Members  Reputation: 122
Posted 26 July 2001  12:43 AM
Hi!
There is an article about car physics at http://home.planet.nl/~monstrous/tutcar.html
There are also information about drag and friction.
Greetings
McMc
There is an article about car physics at http://home.planet.nl/~monstrous/tutcar.html
There are also information about drag and friction.
Greetings
McMc
#13 Moderators  Reputation: 1373
Posted 27 July 2001  07:55 AM
quote:
Original post by goltrpoat
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.
There''s a problem with your equation. It should be:
x = x'' + v*dt + 0.5*a*dt^2
You were missing the 0.5 coefficient on the last term. Also, that is not an Euler integration *unless* you say that x, v, and a on the righthand side are all from the previous time step. If you mean for x'' to be the initial starting value of x'' then the equation doesn''t represent an Euler integration. Rather, it represents an exact solution for constant acceleration, a, and in this case the v also must be the initial velocity at the start time. And dt must be measured from the start time.
Also, a minor beef. Note that I''ve capitalized Euler. Euler is a person''s name and when a method, number, or equation is named after a person the word should be capitalized. Any technical editor, book editor, or professional scientist or engineer will correct you on this. (And I am occasionally all of these.)
quote:
Original post by goltrpoat
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.
Actually, ref_area does *not* depend on the direction moving relative to the wind. Its a constant for a given shape, and it is defined based on how the drag coefficient is defined, which is independent of the orientation of the object. In fact, it is C_d that changes depending on direction of motion relative to the wind. In the case you mention, the cardboard dropped perpendicular to the ground has much less wind resistance and a much lower C_d than the cardboard dropped parallel to the ground. The ref_area is really just a constant scaling factor on drag, a convenience term that is used to convert a drag force (which has units such as pounds or Newtons) to a unitless value.
But you make a good point. If you have an object that does exhibit significantly different wind resistance (which *is* related to the dynamically changing projected areadifferent than ref_area), then C_d must change for realistic drag. For some simple shapes, such as airplane wings at moderate angles to the wind, its easy to come up with an estimate (using a parabolic drag polar, possibly with an additional parabolic lowdrag bucket for very low angles relative to the wind). For complex shapes (automobiles) it is not as easy.
I realize I''m going off on a tangent here, that may no longer be important to this topic. Forgive me, but I studied aerodynamics in university for 9 years and its nice to talk about sometimes, .
quote:
Original post by goltrpoat
also, you seem to be confusing drag with friction.. they aren''t the same thing.
Hmmm. Of course, a component of aerodynamic drag *is* the friction force between the air and the body and so they really *are* the same thing to a degree. There is a pressure component to drag as well and that is not friction.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.