2
\$\begingroup\$

We are using delta time like this:

private static final float SPEED = 200f; // Moving pixels in one second

@Override
public void render(float delta) {
    myObject.vx += SPEED * delta;
    myObject.vy += SPEED * delta;

    myObject.move();
}

So I am using this update method to apply friction on my object:

private static final float FRICTION = 0.98f;

@Override
public void render(float delta) {
    //...

    myObject.vx *= FRICTION;
    myObject.vx *= FRICTION;

    myObject.move();
}

I know this is wrong:
myObject.vx *= FRICTION * delta

I want to use friction with "multiplication operation" not "minus".

So how can I implement the delta time to my FRICTION variable?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

If my assumption that you want your speed to lose 2% of its value every second, then this is a perfect opportunity to use the exponential rate of decay expression, which looks like this:

A=Pe^(rt)

A is the final amount you want (so, after 1 second, .98(vx)) and P is the initial amount. t is just time, so since you want the velocity to only be 98% of its value from the second before you can just plug in 1 for t, .98(vx) for A, and vx for P and solve for r.

This will get you about a value of about -.0202, but you can always store something more accurate.

So what your friction application code should look like is this:

myObject.vx *= Math.pow(Math.E, delta * -.0202);

If you did not know already, decimal numbers that omit the f are automatically initialized as doubles. Math.pow also uses and returns doubles, since they are more accurate. So you may want to have your velocity measured as a double. This will not likely make a very noticeable difference, but if you want accurate simulations, I recommend using doubles.

I have not tested this, so please leave a comment if you notice a problem, but I do believe this is the best way to do what you want.

\$\endgroup\$
7
  • \$\begingroup\$ This is a good method but if you use floats it won't be accurate, since some of the digits get lost. e^(0.016*-0.0202)=0.99967685222 so ~4 digits get lost \$\endgroup\$
    – VaTTeRGeR
    Commented Apr 25, 2015 at 17:04
  • \$\begingroup\$ *this may only become important at high framerates because of smaller delta values => more digits that become relevant because the error adds up \$\endgroup\$
    – VaTTeRGeR
    Commented Apr 25, 2015 at 17:12
  • 1
    \$\begingroup\$ @VaTTeRGeR I added a note about using doubles (2nd to last paragraph). \$\endgroup\$ Commented Apr 25, 2015 at 17:19
  • \$\begingroup\$ hmm i used it but yes, i am using "float" the speed is not lessening after a while, and object is still moving little speed. So am i use "-" instead of "*" for the friction? What is often used for friction in games? \$\endgroup\$
    – MarsPeople
    Commented Apr 25, 2015 at 18:27
  • \$\begingroup\$ You'll want a tolerance in which your speed stops changing, too since it's very unlikely that this will actually get you to 0. \$\endgroup\$ Commented Apr 25, 2015 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .