Java - Smooth tiles movement [Solved]

Hi!

I am developing my first game and all work perfectly except some moving tiles. I am moving tiles for 10px every 18ms. Moving and collision detection work fine, but movement does not look smooth. It looks like the tiles are jumping for 10 px and not moving smooth.If I set speed to 1px, is good but tiles are than moved too slow for my requirements.(Code is executed every 18ms) The tiles are images but even if set them just like drawed rectangles it is the same. (So performance is not a problem but something else). I searched over internet for motion blur effect but I could not find anything useful. Does anyone have an answer how can I fix this small problem about smooth moving?

Regards Jan

Are you sure your rendering at something around 50 fps (every 18 ms).
Sounds more like your game is lagging.

Try to use 1000/60 speed instead of 18. Also, do you have FPS counter in your game? This may be caused by frame drop.

The moving speed should be a float value like 0.2f. To calculate the total moving amount, multiply the speed with the current frame delta:

float speed = 0.2f * delta;

After that, you could move your tile (or game object, whatever) in any direction. After reaching the next tile cell, you can stop the movement (in a tile based map). Or how does your “tile” game look like?

Thank you guys for fast answers. I will try what you recommend. I don`t think the game lags. I paint only tiles that are visible in view field (1280x560) and tiles that positionX is smaller than 0 are removed and not updated. My game is a scrolling game.There is character on fixed location who is jumping,sliding and picking an objects on grid. Grid is moving left in negative direction.I tried also with removing a background, painting tiles like rectangle and make very small grid with only one visible object and is still the same, tiles are moving same way. My game structure:

In tile class is main constructor (public Tile() {}) where are defined positionX, positionY, speed and type (image) of moving object. Than there is a collision method and update method where I update position with

tileX += tileSpeed;

(where speed is -10)

In main applet class there is an ArrayList for tiles and many methods. One of them is method which reads from txt file and create tiles. Than method for updating ArrayList of tiles and for tiles painting.

I dont have any FPS counter inside game, but FRAPS (tool for recording clips inside games) is showing me that in this applet FPS are between 109 and 111. With pause of 10ms i have 200 FPS and tiles are moving same way. It is not annyoing problem but still want to fix it. :smiley:

Tile move like: -------- ???
I want: _____________ :slight_smile:

I guess you should post your game loop then.
Easyer to find an solution.

Make sure you double check your game loop works the way you think it does before moving on. When the scene gets drawn, print the time. Go back and make sure it’s getting called as often as you expect and at a regular interval.

If you think it is an error, then remember to post all the relevant code and only the relevant code.

A great gameloop tut: http://www.java-gaming.org/index.php/topic,24220.0

That’s maybe a little bit offtopic, but you could have a look at libgdx. It offers you great features. I would prefere it to java2d, because opengl is fun, if you have time to learn it.
If you just started programming, you might wanna stick a little bit to java2d but later on libgdx would be a great opportunity.
best regards

Hey. Now I have created new simple applet just for test(same structure like my game). And moving is same as my tiles.It is horible. So somewhere here in basic code is my problem.

package test;

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class game extends Applet implements Runnable{

    int x,y;
    public void init() {
        x = 0;
        y = 100;
        setSize(1280,560);
    }

private Image image; 
private Graphics second; 


@Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }
        
        second.setColor(getBackground());
        second.fillRect(0, 0, 1280, 560);
        second.setColor(getForeground());
        paint(second);

        g.drawImage(image, 0, 0, this);
    }
    
    @Override
    public void paint(Graphics g) {
        g.fillRect(x, y, 80, 80);
    }
    
    @Override
    public void run() {
        while (true) {
            x += 10;
            if (x > 1280) {
                x = 0;
            }
            repaint();
            try {
               Thread.sleep(18);
            } catch (InterruptedException e) {}
        }
    }
    
    Thread thread;
    @Override
    public void start() {
        thread = new Thread(this);
        thread.start();
    }
}

Thread.sleep cannot be relied on as a timing mechanism.

And what I should use instead?

Find a way to occupy time until it is time to update. Do Thread.yield() if you have other threads in the same program, Thread.sleep(1) if you have time and want to tell the the operating system to give some time for other programs to run, wait in a busy loop, or do something else to keep busy.

The most basic form is:

do
{
  now = System.getNanoTime();
  // Do something besides stalling
} while(now < nextUpdateTime);
nextUpdateTime += FRAME_DURATION;

There are topics that provide helpful information.