So i have this cube i wish to bounce off the walls, (as for now just x-axis) it moves however when it gets to the end of the frame it moves back and forth instead of going the opposite direction.
public void moveBox(int dx,int dy)
{
if(xLeft < 0 || xLeft > frame_width)
{
dx = -dx;
}
xLeft = xLeft + dx;
repaint();
}
Heres what i understand from the code if xLeft (aka origin cordinate for cube pass the frame) then dx (which is the speed at which its being moved) turns negative which should flip the direction. and when it goes back to zero a double negative would flip it back to positive. My logic is flawed because its not bouncing its just floats back and forth in the end of the frame
however that is not the case and i don't understand why is it because the method gets recalled with a different xLeft value each time? if thats the case what should i do to make it bounce? I've tried alot of different things nothing seems to fully bounce it back