Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a series of objects moving along the straight lines. I want to implement slight changes of velocity of each of the object. Constraint is existing model of animation. I am new to this, and not sure if it is the best way to accommodate varying speeds, but what do I know?

It is a Java application that repaints the panel every time the timer expires. Timer is set via swing.Timer object that is set by timer delay constant. Every time the game is stepped objects’ coordinates advanced by an increment constant. Most of the objects are of the same class.

Is there fairly easy way to refactor existing system to allow changing velocity for an individual object? Is there some obvious common solution I am not aware about?

Idea I am having right now is to set timer delay fairly small, and only move objects every so many cycles of animation so that the apparent speed can be adjusted by varying how often they get moved. But that seems fairly involved, and I do not think it is the most elegant solution in terms of performance what with repainting the whole frame every 3-5 milliseconds.

Can it be done by advancing the objects so many (varying) times during the certain interval (let’s say 35ms for something like 28fps), and use repaint() method to redraw just individual object? Do I need to mess with pausing animation for smoothness at higher redraw rates? Is it common practise to check for collision at larger step interval, but draw animation a lot more frequently?

share|improve this question
up vote 1 down vote accepted

This is fairly simple. Don't change how frequently you move the different objects to alter their apparent speed, change how far you move them on each update. All the objects should be moved/updated at the same time. The faster objects should be moved farther per update than their slower counterparts. You say changes in velocity, I assume you know what you're saying and you mean acceleration. (not the same as different velocities). That means that not only will these objects be moving different amounts per update, the amounts they move will also be changing. This is basic equations of motion stuff (or more geared towards game developers).

Now, if you want to also change the animation speed to match, this is also fairly simple. Take for example, walking animations. At their default speed, they will animate the character moving at a certain speed. This animation could be applied to the model by tracking the time spent on each frame and incrementing the frame count when the current frame is done. So it's easy to change the speed of the animation, by changing the amount of time to stay on each frame. So for example, an animation might be tracked very simply like so:

if(currentFrameTime > timePerFrame) {
    currentFrameTime = 0;

We can add an animation speed into this equation simply by adding a division:

if(currentFrameTime > timePerFrame / animationSpeed) {
    currentFrameTime = 0;

This makes it so an animation speed of 1 makes the animation behave just as before, where an animation speed of 2 will cut the time per frame in half, therefore doubling the speed of the animation.

share|improve this answer
I chose to accept this answer for its quality and insight it provided. It was not exactly what I needed (I really did not need acceleration, I just wanted to have objects move at velocities that are constant, but varying slightly amongst each other), but it still helped. – theUg Jan 21 '13 at 7:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.