游戏物理 - 摩擦 [英] Game Physics - Friction

查看:134
本文介绍了游戏物理 - 摩擦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常简单的问题是:我将如何实施摩擦?我尝试的任何东西似乎都没有用。

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

哦, speedX = 1。

这是我的代码:

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;
}

编辑:

现在,一旦玩家停止,他就会减速(这很好),但他并没有完全停止。

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";
    }


推荐答案

通常认为摩擦是考虑到两个表面之间的摩擦,使用恒定法向力,这将会减速你的物体线性速度。其他摩擦力如空气阻力通常也具有线性和二次项。
如果你想在两个表面之间使用摩擦,你可以这样做:

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;

我还包括足够的质量 F = m * a 用于加速。如果还有其他力作用在物体上,那将会有点复杂,因为如果物体停止移动,它只会在剩余力超过摩擦力时再次开始移动。

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.

顺便说一下,如果你想让数字更正确,你可以在模拟中添加一个时间步长: vX + = accelerationX * timeStep; x + = vX * timeStep;

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;.

这篇关于游戏物理 - 摩擦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆