Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java - Smooth tiles movement [Solved]  (Read 1271 times)
0 Members and 1 Guest are viewing this topic.
Offline JanNovak18

Senior Newbie





« Posted 2013-01-22 20:13:54 »

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
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #1 - Posted 2013-01-22 20:24:10 »

Are you sure your rendering at something around 50 fps (every 18 ms).
Sounds more like your game is lagging.
Online Mac70
« Reply #2 - Posted 2013-01-22 20:25:11 »

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.

Check out my Devblog! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline myrealityde

Senior Newbie


Medals: 1
Exp: 3 years


Blogger, GameMaker, Creator.


« Reply #3 - Posted 2013-01-22 20:28:43 »

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:

1  
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?

Blogger, GameMaker, Creator.
Offline JanNovak18

Senior Newbie





« Reply #4 - Posted 2013-01-22 21:16:44 »

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:

1  
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
1  
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. Cheesy

Tile move like: --------  Huh
I want: _____________  Smiley



Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #5 - Posted 2013-01-22 21:27:14 »

I guess you should post your game loop then.
Easyer to find an solution.
Offline Best Username Ever

Junior Member





« Reply #6 - Posted 2013-01-22 22:36:33 »

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.
Offline Phibedy

Senior Member


Medals: 8



« Reply #7 - Posted 2013-01-22 22:49:34 »

A great gameloop tut: http://www.java-gaming.org/topics/game-loops/24220/view

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
Offline JanNovak18

Senior Newbie





« Reply #8 - Posted 2013-01-22 23:09:49 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
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();
    }
}
Offline Best Username Ever

Junior Member





« Reply #9 - Posted 2013-01-22 23:12:37 »

Thread.sleep cannot be relied on as a timing mechanism.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JanNovak18

Senior Newbie





« Reply #10 - Posted 2013-01-22 23:16:35 »

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

And what I should use instead?
Offline Best Username Ever

Junior Member





« Reply #11 - Posted 2013-01-22 23:59:25 »


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:
1  
2  
3  
4  
5  
6  
do
{
  now = System.getNanoTime();
  // Do something besides stalling
} while(now < nextUpdateTime);
nextUpdateTime += FRAME_DURATION;


There are topics that provide helpful information.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (20 views)
2014-09-21 02:42:18

BurntPizza (14 views)
2014-09-21 01:30:30

moogie (14 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (42 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (97 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!