I’m trying to create an animation like a hockey disc moving slowly and whose speed decreases little by little.
So I tried to do this :
for(Disc d : panel.getDiscs()
{
if(d.getLaunch() <= 70) move(d);
}
And :
private void move(Disc d)
{
Point p = d.getLocation();
Point speed = d.getSpeed();
Dimension size = d.getSize();
int vx = speed.x;
int vy = speed.y;
int x = p.x;
int y = p.y;
if (x + vx < 0 || x + size.width + vx > panel.getWidth()) {
vx *= -1;
}
if (y + vy < 0 || y + size.height + vy > panel.getHeight()) {
vy *= -1;
}
x += vx;
y += vy;
d.setSpeed(new Point(vx, vy));
d.setLocation(new Point(x, y));
try
{
Thread.sleep(d.getLaunch());
} catch (InterruptedException e)
{
e.printStackTrace();
}
if(d.getCoeff() < 5) d.setCoeff(d.getCoeff()+1);
else { d.setLaunch(d.getLaunch()+1); d.setCoeff(0); }
}
What I’m trying to do here is to increase a value (launch) that begins at 20 and keeps running until 70, then I use this value in every Thread.sleep(launch);.
I only increase this value when 5 frames were previously displayed (coeff).
The problem is the following : when I try it with a too small value, the disc is too fast and stops running too early. When I try it with a higher value, the disc speed looks well but the last animations are not very smooth… What should I do ?
Your approach seems invalid. Displaying the disc and determining it’s position are two different things. I think that: