Friction
I know the formula and all to pull it off, and how to get the Normal force and all, but after trying it out in an example app, I failed miserably. Can anyone show me some example code on an object sliding along a surface till it gets slower and slower, and makes a complete stop? Thanks.
What is the behavior of your app? Do you have a small (under 20 lines) fragment of code that you can share for evaluation?
Yeah, here's my code. It's in VB, but you should be able to understand it easily. I'm only showing you the math stuff I used though, not the whole project.
[Edited by - Jacob Roman on January 4, 2006 8:45:34 PM]
'-------------------------------------------'Variables I declared'-------------------------------------------Private Const GRAVITY As Single = 9.8Private X As Single, Y As Single 'X and Y positionPrivate x0 As Single, y0 As Single 'Initial positionPrivate t As Single 'TimePrivate v As Single 'VelocityPrivate m As Single 'MassPrivate v0 As Single 'Initial VelocityPrivate F As Single 'Net ForcePrivate N As Single 'Normal ForcePrivate u As Single 'Dynamic Friction CoefficientPrivate Fric As Single 'Friction Force'-------------------------------------------'Math functions'-------------------------------------------Private Function Normal_Force(m As Single, g As Single) As Single Normal_Force = m * gEnd FunctionPrivate Function Net_Force(m As Single, a As Single) As Single Net_Force = m * aEnd FunctionPrivate Function Friction_Force(u As Single, N As Single) As Single Friction_Force = u * NEnd Function'-------------------------------------------'Initialized values when the program loads'-------------------------------------------Private Sub Form_Activate() X = 100 Y = 200 x0 = X y0 = Y m = 68 u = 0.3 F = Net_Force(m, 1) N = Normal_Force(m, GRAVITY) Fric = Friction_Force(u, N) v0 = F / m Milliseconds = Get_Elapsed_SecondsEnd Sub'-------------------------------------------'Main loop'-------------------------------------------Private Sub Game_Loop() Do While Running = True DoEvents Lock_FPS 60 t = Get_Elapsed_Seconds - Milliseconds v = v0 * F * t X = x0 + v PSet (X, Y), RGB(0, 0, 0) LoopEnd Sub
[Edited by - Jacob Roman on January 4, 2006 8:45:34 PM]
I didn't implement friction in my code cause I couldn't do it. I just have an object moving consistantly towards the right at the same speed. So if any of you math geniuses know how to pull this off, I would sure appreciate it.
I didn't go through the whole code but ... instead of these 2 lines here:
Do something like
I didn't get what languuage that was--if the '+=' is not allowed, change it as you see fit.
Remember, if there is no force acting, there chould be no change in velocity. In the above lines, if 'F' is 0, then X gets changed by an unoform amount (with a stable frame rate), wehich is what you want. When you have frictional force (make sure this is negative so that the object slows) the velocity decreases like you would expect.
NOTE:
1. Make sure you have something in there to stop the object if it's velocity is 0, or else it will start going backwards.
2. If you read the FAQ, you will see that Euler integration is evil and you shouldn't use it. Think of a replacement long-term.
Quote: v = F * t
X = x0 + v
Do something like
v += F * tX = x0 + v*t
I didn't get what languuage that was--if the '+=' is not allowed, change it as you see fit.
Remember, if there is no force acting, there chould be no change in velocity. In the above lines, if 'F' is 0, then X gets changed by an unoform amount (with a stable frame rate), wehich is what you want. When you have frictional force (make sure this is negative so that the object slows) the velocity decreases like you would expect.
NOTE:
1. Make sure you have something in there to stop the object if it's velocity is 0, or else it will start going backwards.
2. If you read the FAQ, you will see that Euler integration is evil and you shouldn't use it. Think of a replacement long-term.
I originally had it like that but the problem was that it made it go from 0 to 60 mph (metaphorically speaking) and left the window.
Just so you know, my variable t (time) is in milliseconds, so 0.001 would be 1 millisecond, and 1.000 would be 1 second. It's not time per frame, cause if it was, it would make it move consistantly then. ;)
[EDIT] Nevermind, I stuck v0 (initial velocity) in there for v = v0 + F * t, but had to make X = x0 + v cause it looked very awkward multiplying t in there as well. I already done it with v, so it wasn't needed in the other one.
Just so you know, my variable t (time) is in milliseconds, so 0.001 would be 1 millisecond, and 1.000 would be 1 second. It's not time per frame, cause if it was, it would make it move consistantly then. ;)
[EDIT] Nevermind, I stuck v0 (initial velocity) in there for v = v0 + F * t, but had to make X = x0 + v cause it looked very awkward multiplying t in there as well. I already done it with v, so it wasn't needed in the other one.
Quote:Original post by deavik
2. If you read the FAQ, you will see that Euler integration is evil and you shouldn't use it. Think of a replacement long-term.
hopefully i'm not starting a whole big thing, but why is it evil. it doesn't say in the faq.
just to try and NOT hijack this thread, is it because of the (relatively) large error it intorduces for large timesteps?
Quote:Original post by minamurQuote:Original post by deavik
2. If you read the FAQ, you will see that Euler integration is evil and you shouldn't use it. Think of a replacement long-term.
hopefully i'm not starting a whole big thing, but why is it evil. it doesn't say in the faq.
just to try and NOT hijack this thread, is it because of the (relatively) large error it intorduces for large timesteps?
If by "relatively large" you mean "the potential for all your variables to shoot off to infinity and then some" then you would be correct.
Another thing is
v = v0 * F * t
X = x0 + v
Should be
v = v0 + F * t / m
X = x0 + v * t
And including friction,
v = v0 + (F - Fric) * t / m
v = v0 * F * t
X = x0 + v
Should be
v = v0 + F * t / m
X = x0 + v * t
And including friction,
v = v0 + (F - Fric) * t / m
Well like I said earlier, these formulas didn't work right:
v = v0 * F * t
X = x0 + v * t
And the reason is cause time has already been multiplied. So if you do this:
X = x0 + v0 * F * t
Then you'll see what I mean. Multiplying t to both equations made it go from slow to fast, when it was suppose to be constant speed.
Also, this didn't decelerate at all.
v = v0 + (F - Fric) * t / m
It maintained constant speed. Not sure what I need to do though.
v = v0 * F * t
X = x0 + v * t
And the reason is cause time has already been multiplied. So if you do this:
X = x0 + v0 * F * t
Then you'll see what I mean. Multiplying t to both equations made it go from slow to fast, when it was suppose to be constant speed.
Also, this didn't decelerate at all.
v = v0 + (F - Fric) * t / m
It maintained constant speed. Not sure what I need to do though.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement