user2277862
user2277862

Reputation: 11

Game Physics - Friction

My very simple question is: how would I implement friction? Nothing I try seems to be working.

Oh, and speedX = 1.

Here's my code:

public void update() {
    x += velocityX;
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_RIGHT)
        vx = xSpeed;

    if (key == KeyEvent.VK_LEFT)
        vx = -xSpeed;
}

public void KeyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_RIGHT)
        vx = 0;

    if (key == KeyEvent.VK_LEFT)
        vx = 0;
}

Edit:

Now, once the player stops, he slows down (which is good), but he doesn't stop completely.

if (key == KeyEvent.VK_RIGHT) {
        vx = 0.20 * vx;

        if (vx < 0.2)
            vx = 0;

        playerAction = "still";
    }

Upvotes: 0

Views: 8703

Answers (5)

John Vonachen
John Vonachen

Reputation: 65

I have worked as a game AI engineer. That often involves making gameplay and physics. I recently had to answer this question myself. That's why I found this. Here is my solution. My problem may be a little different from yours but my challenge was to make it work no matter what framerate I was using. Here's my solution:

speed -= (speed * (1 - friction)) / fps;

friction is always a float between 0 and 1, 1 being no friction and 0 being essentially infinite friction. This makes sure the the right amount of friction will be applied proportional to frames per second.

Upvotes: 1

user3904599
user3904599

Reputation: 21

If u, m and g in Ff=umg for the formula for the force of surface friction is constant, then a in F=ma would also be a constant number. Also because a = deltaV/s that means that the velocity equals acceleration times time (as=deltaV). This means that for every time interval the deltaV remains the same if Ff is the same. The change in speed only stops if it is not moving(v=0). So I think the best way to do this is to detect whether it's moving or not and apply the constant deltaV (for the opposite direction) and not apply it when v reaches zero.

Upvotes: 0

fibonatic
fibonatic

Reputation: 216

Often friction is assumed to be a constant force, considering the friction between two surfaces with a constant normal force, which would slowdown your objects velocity linearly. Other frictional forces like air drag often also have linear and quadratic terms. If you would want to use friction between two surfaces you could do it like this:

if (vx > 0) vx += -friction / mass;
if (vx < 0) vx +=  friction / mass;

I also included the mass to suffice F = m * a for the acceleration. If there are also other forces acting on the object it would be a little more complicated since if the object has stopped moving it would only start moving again if the remaining forces exceed the frictional force.

By the way if you would like to make it more numerical correct, you could add a time step size into the simulation: vX += accelerationX * timeStep; and x += vX * timeStep;.

Upvotes: 1

DarenW
DarenW

Reputation: 16906

Friction is a force opposite to velocity. If you're dealing with forces at all, gravity or electric fields or rocket engines or something more mysterious, just add to the total force one more computed as velocity times some coefficient. You could get fancy and make its magnitude proportional to the speed squared or cubed, for different effects. Then compute velocity and position as usual.

If you're not dealing with forces and F=ma, but using a simpler kind of physics based only on velocity, which is common in video games, you need to decrease that velocity gradually. For each time step, compute new_velocity = 0.99 * current_velocity. Use whatever coefficient gives the right amount of slow-down. Again, you could get fancy and involve velocity squared in some way to alter the feel of the object's slowdown.

Upvotes: 1

Stochastically
Stochastically

Reputation: 7846

Friction is a force that slows down any objects you have that are moving. So you'd need to have something like a background thread that constantly looks at the speed of all your objects, and slowly decrements each speed towards zero.

Upvotes: -1

Related Questions