# Scalar modifiers per frame

This topic is 2550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My game includes a very simple physics simulation, in which an object accelerates based on user input. I need this to remain constant despite any framerate changes which might occur. I'm having a problem, however, when it comes to handling friction (this is in because I don't want the object to carry on moving for a long time after user input stops). Is there any better way of doing it than the method exemplified in the code below? Thanks.

 // Please note that this is in no way my real code. It is just simplified to show my problem. const float FRICTION = 0.05f; const float POWER = 1; float xvel = 0; float yvel = 0; float x = 0; float y = 0; void gameLoop(float elapsedSeconds) { // Acceleration can be easily be scaled down to frame duration. float acceleration = POWER * elapsedSeconds; // Generic booleans used to indicate player input. if (right) xvel += acceleration; if (left) xvel -= acceleration; if (down) yvel += acceleration; if (up) yvel -= acceleration; // Friction requires an expensive fractional pow call! float friction = pow(FRICTION, elapsedSeconds); xvel *= friction; yvel *= friction; x += xvel; y += yvel; }

##### Share on other sites
Hi!

As for the acceleration your approach looks good. POWER would be the acceleration in m/s².
The friction however is a little more complicated since it is not linear.
The German Wikipedia page on inclined planes derives the equations for the derivative of the velocity. You would simply have to add this change to your velocity to compute the velocity at the next time step. The equations and depictions are rather self-explainatory, so you don’t need to speak German, I think.

Hope this helps a little!

##### Share on other sites
Thanks for the response, but I haven't studied maths to the level where I can understand these diagrams and equations without English explanation, and as such I don't know where to look on the page. I do speak German to a level, I'm certainly not at the stage where I understand technical mathematical vocabulary. Could you possibly summarise the relevant part of the article? Thank you!

##### Share on other sites
Hi, again.

How physically correct do you want your simulation to be? Friction is a very tough topic, with lots of effects to take into account (static friction, dynamic friction, aerodynamic drag …). The site I linked before, for instance also incorporates aerodynamic drag.

You asked me to summarize the content of the site. Alright, but I do it only very briefly, because after thinking about it a little I’d rather suggest using an approximation, as listed below. But, now to the site: First they summarize the forces being present on an inclined plane. There are the gravity (which is split into a normal and tangential part), the normal force (keeps the object from sinking into the surface) and the force perpendicular to the normal force, pushing the object down the inclined plane. Afterwards they define some conditions whether an object would move at all (maybe friction is too high or angle is too small -> we need this later), then they incorporate the aerodynamic drag into the equation for the conservation of forces (which is making the equations rather complicated). This yields a differential equation of second order. Differential equations are commonly used to model systems that depend on a function y(t) and involve the derivative of y(t). We are talking about the velocity here so the function we search is v(t) and the system depends on the acceleration, which is the derivative of v(t). They derive a solution for that and then start distinguishing some cases (some forumula for the breaking distance if an object would decelerate instead of accelerate etc.). As I already mentioned… I’d not recommend using this rather complicated approach. Sorry, for sending you down this road in the first place. (But perhaps you can see now, that this is a complicated topic. )

I found an approximation, here. Perhaps that yields results that seem plausible enough for your use case. (Computer graphics is often about approximation and making things seem plausible ) They add the tangential component of the gravity to the current velocity (as acceleration, which is right) and scale the velocity down by some constant(!) factor (as friction, which is an approximation). Actually, it should depend on the angle of the inclined plane as well. Unfortunately by their simple approach an object would never really stop, though. However, you can find out when that should be the case and then hack some sort of “break” into your code. The condition for motion is that the friction coefficient is bigger than tan(alpha), whereas alpha is the angle between the surface normal and the gravity direction. Well, making approximations of physics usually never yields pretty code...

I hope this helps more than my last post.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013390
×