如何使图像沿 x 轴反弹?

发布于 2024-12-10 07:21:32 字数 532 浏览 0 评论 0原文

我有一个图像在增量时间(dt)内以给定速度水平移动。但问题是,当图像到达世界大小的尽头时,图像不会反弹。我怎样才能使图像向后反弹,以便将其保留在世界内部?

任何帮助都可以。

到目前为止,这是我尝试过的:

@Override
public void move(long dt)
{
    // v = dx / dt
    //   dx m = v m/s  . dt s

    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;


    double left_wall = 0;
    double right_wall = board.x1_world;

    if (x <= right_wall)
    {
        x += dx_m;
        if (x >= right_wall)
        {

            x = right_wall;
           x *= -dx_m;
        }
    }
}

I have an image which moves horizontally with a given speed over a delta time(dt). But the problem is,the image doesn't bounces off when it reaches the end of the size of the world.How can i make the image bounces off backward so it would be kept inside the world?

Any help will do.

Here's what i've tried so far:

@Override
public void move(long dt)
{
    // v = dx / dt
    //   dx m = v m/s  . dt s

    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;


    double left_wall = 0;
    double right_wall = board.x1_world;

    if (x <= right_wall)
    {
        x += dx_m;
        if (x >= right_wall)
        {

            x = right_wall;
           x *= -dx_m;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-12-17 07:21:32
    @Override
public void move(long dt)
{
    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;


    double left_wall = 0;
    double right_wall = board.x1_world;

    x += dx_m;
    if (x <= 0) speed *= -1.0;
    if (x >= right_wall) speed *= -1.0;
}

当图像的 x 坐标到达边界时,只需更改水平速度的方向(将其乘以 -1)。但您应该使用这样的条件:

if (x >= (right_wall - width_of_image)) speed *= -1.0;

而不仅仅是 x >= right_wall,因为这样做,图像在“触及”世界尽头时会反弹。

    @Override
public void move(long dt)
{
    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;


    double left_wall = 0;
    double right_wall = board.x1_world;

    x += dx_m;
    if (x <= 0) speed *= -1.0;
    if (x >= right_wall) speed *= -1.0;
}

When the x coordinate of your images reach an border, just change the orientation of the horizontal speed (multiply it for -1). But you should use an condition like this:

if (x >= (right_wall - width_of_image)) speed *= -1.0;

Instead of just x >= right_wall, because doing so, the image will bounce when it "touchs" the end of the world.

2024-12-17 07:21:32

除了按照 @Oscar 的建议单独检查每一端之外,您可能还需要考虑图像的有限宽度,如 地铁模拟。

In addition to checking each end separately, as suggested by @Oscar, you may need to account for the image's finite width, as shown in this Subway simulation.

半透明的墙 2024-12-17 07:21:32
private boolean goleft=false;//keep direction
@Override
public void move(long dt)
{
    // v = dx / dt
    //   dx m = v m/s  . dt s

    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;

    double left_wall = 0;
    double right_wall = board.x1_world;

    if(goleft)x += dx_m;
    else x-= dx_m;

    if (x >= right_wall)//touching 1 wall
    {
        x = right_wall;
        x += dx_m;
        goleft=true;
    }else if(x<=left_wall){//touching the other wall
        x = left_wall;
        x += dx_m;
        goleft=false;
    }
}
private boolean goleft=false;//keep direction
@Override
public void move(long dt)
{
    // v = dx / dt
    //   dx m = v m/s  . dt s

    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;

    double left_wall = 0;
    double right_wall = board.x1_world;

    if(goleft)x += dx_m;
    else x-= dx_m;

    if (x >= right_wall)//touching 1 wall
    {
        x = right_wall;
        x += dx_m;
        goleft=true;
    }else if(x<=left_wall){//touching the other wall
        x = left_wall;
        x += dx_m;
        goleft=false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文