Jump to content
  • Advertisement
Sign in to follow this  
Jacob Roman

Friction

This topic is 4696 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 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.

Share this post


Link to post
Share on other sites
Advertisement
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.



'-------------------------------------------
'Variables I declared
'-------------------------------------------

Private Const GRAVITY As Single = 9.8

Private X As Single, Y As Single 'X and Y position
Private x0 As Single, y0 As Single 'Initial position
Private t As Single 'Time
Private v As Single 'Velocity
Private m As Single 'Mass
Private v0 As Single 'Initial Velocity
Private F As Single 'Net Force
Private N As Single 'Normal Force
Private u As Single 'Dynamic Friction Coefficient
Private Fric As Single 'Friction Force

'-------------------------------------------
'Math functions
'-------------------------------------------
Private Function Normal_Force(m As Single, g As Single) As Single

Normal_Force = m * g

End Function

Private Function Net_Force(m As Single, a As Single) As Single

Net_Force = m * a

End Function

Private Function Friction_Force(u As Single, N As Single) As Single

Friction_Force = u * N

End 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_Seconds

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

Loop

End Sub





[Edited by - Jacob Roman on January 4, 2006 8:45:34 PM]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I didn't go through the whole code but ... instead of these 2 lines here:

Quote:
v = F * t

X = x0 + v


Do something like

v += F * t
X = 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by minamur
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?


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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

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!