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;
}
}
}
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:
Instead of just
x >= right_wall, because doing so, the image will bounce when it “touchs” the end of the world.