根据处理中的距离更改比例

Changing scale based on distance in processing

我正在尝试为双人游戏创建一个可以根据玩家距离进行放大和缩小的相机。

执行此操作的当前代码,

    float scale = 1;
    float distance = dist(player.location.x, player.location.y,     
    player2.location.x, player2.location.y);

    if (distance > 
    scale = 0.99; 
    }

    //Places the camera between the two players at all times. No matter what 
    scale the game is in.
    translate((-player.location.x * scale / 2) - (player2.location.x * scale / 
    2) + width / 2
    , (-player.location.y * scale / 2) - (player2.location.y * scale / 2) + height / 2);

    scale(scale);

我的计划是使用 if 语句,但这显然行不通。我正在考虑使用模数来执行此操作,但我还不完全了解如何使用模数。有谁能告诉我如何解决这个问题?

距离变量应该检查第一个玩家和第二个玩家之间的距离。例如,当两个玩家之间的距离为 100 时,比例应该改变 0.01。由于距离,比例将从 1 变为 0.99。我怎么想减去或加上 100 dist.

0 dist = scale 1
100 dist = scale 0.99
200 dist = scale 0.98
ect.

我应该怎么做才能做到这一点?

这个简单的线性表达式将允许连续缩放。

scale = max(0.1, 1 - distance/10000);

如果您希望每增加 100 距离以 0.01 的增量减小比例,请使用以下方法(如您所建议的,它使用模数):

scale = max(0.1, 1 - (distance - distance%100)/10000)

max() 是一个内置处理函数,我在这里使用它为比例因子提供一个底值(不能低于 0.1)。