Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  my movement's sprite is not linear!!  (Read 1511 times)
0 Members and 1 Guest are viewing this topic.
Offline Graziano Mesina

Senior Newbie





« Posted 2006-05-07 19:51:02 »

I've a problem with my sprite's movement, it's not linear (the sprite move with "jumps")

I've a principal class that calculate the elapsed time like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void gameLoop()
    {
        // Calcola il tempo attuale
       long startTime = System.currentTimeMillis();
        long currTime = startTime;
        while(gameRunning)
        {
            long elapsedTime = System.currentTimeMillis() - currTime;
            currTime += elapsedTime;
           
            resource.update(elapsedTime);
           
            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
            resource.draw(g);
            strategy.show();  
        }
    }  


the draw method is in a class that manages all the objects:

1  
2  
3  
4  
5  
6  
7  
8  
9  
// Disegna le sprites
       Iterator i = map.getSprites();
        while(i.hasNext())
        {
            Sprite sprite = (Sprite)i.next();
            int x = Math.round(sprite.getX()) * TILE_WIDTH;
            int y = Math.round(sprite.getY()) * TILE_HEIGHT;
            g.drawImage(sprite.getImage(),x,y,null);          
        }


what I've to change?

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2006-05-07 23:33:23 »

Your logic is based on the assumption that currentTimeMillis() is accurate, but it isnt.

As I just wrote elsewere... The resolution of currentTimeMillis is 50-55 msec on win9x, 10msec on 2k/xp and 1msec on linux/mac.

So, you cant do it as straightforward as that. Your options are System.nanoTime (1.5+), my adaptive yield loop thingy or something completely different Smiley

弾幕 ☆ @mahonnaiseblog
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #2 - Posted 2006-05-08 04:57:57 »

Another option is the gage timer at http://java.dnsalias.com/

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

JGO Coder


Medals: 1


http://t-machine.org


« Reply #3 - Posted 2006-05-08 21:27:58 »

my adaptive yield loop thingy

Thanks for that, I was about to go digging to find something that would work on OS X as well as 1.1 (for an applet game about to be released commercially).

I'm suspicious it wont actually work, given you dont mention OS X's timing problems Wink which are conisderably worse than win XP Sad Sad, but we'll see how it goes. I can always force that to nanotime, since java2d doesn't work on OS X with less than 1.4 anyway (PNG's don't load, for instance. Thankfully you can mostly count on 1.5 being installed these days (anyone who downloads the auto OS updates will have it)

malloc will be first against the wall when the revolution comes...
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2006-05-08 21:43:17 »

>PNG's don't load, for instance.

PNGs are overrated. While they are really great for webpages and the like, they are sorta pointless for java games. With java you put everything into jar files anyways, which means that raw formats (such as tga) are about as small (unless you use funky stuff like pngquant-ed pngs [effectively 8bit with rgba palette]).

And timing wasnt a problem ever on OSX afaik. I only had some annoying issues with the cursor and that freshly created translucent images arent filled with fully transparent black (same on linux).

弾幕 ☆ @mahonnaiseblog
Offline woogley
« Reply #5 - Posted 2006-05-09 00:14:58 »

just for the record, when I used the adaptive yield loop thingy™ in Goomba4K, the timing was perfect on 98SE, XP, and OSX =o
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #6 - Posted 2006-05-09 03:06:08 »

I'm suspicious it wont actually work, given you dont mention OS X's timing problems Wink which are conisderably worse than win XP Sad Sad,

I've never heard of any timing problems on OS X.

Using this simple test program:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
package timertest;
public class Main
{
   public static void main(String[] args) throws InterruptedException
   {
      long start = System.currentTimeMillis();
      long then = start;
      System.out.println("---- System.currentTimeMillis() Resolution Test ----");
      do {
         long now = System.currentTimeMillis();
         if (now != then)
            System.out.println(now-then);
         then = now;
      } while ((then - start) < 10000);
     
      System.out.println("---- Sleep Test ----");
      start = System.currentTimeMillis();
      for(int i = 0; i < 10000;i++)
         Thread.sleep(1);
      System.out.println("10000 * Thread.sleep(1) caused a delay of "+(System.currentTimeMillis()-start)+"ms");
   }
}

Timer resolution of System.currentTimeMillis() is 16 ms on Windows XP and 1 ms on OS X.
Thread.sleep(1) 10000 times delays 19531ms on Win XP.
Thread.sleep(1) 10000 times delays 10274ms on OS X.

So I don't understand what you are referring to whenyou claim OS X has worse timing problems than Win XP.

Quote
but we'll see how it goes. I can always force that to nanotime, since java2d doesn't work on OS X with less than 1.4 anyway (PNG's don't load, for instance.

There was a release of  Java 1.4 that swapped red and blue for some PNGs (those with alpha channels) on OS X, that was later fixed.  It is certainly false to say that PNGs in general don't load - I use them all the time without problems.

Quote
Thankfully you can mostly count on 1.5 being installed these days (anyone who downloads the auto OS updates will have it)

Well at least you got one thing right Smiley.

Offline Graziano Mesina

Senior Newbie





« Reply #7 - Posted 2006-05-09 09:32:05 »

I think that the problem isn't currentTimeMillis(). I've tried to do a simple load of a sprite and to draw it. The problem there wasn't anymore, the movement was linear without jumps.

I think the problem is 'cause I use tilemap, in fact, when I load a sprite from tilemap, the problem goes again.

this is the code to load the map:

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  
private TileMap loadMap(String name)
    throws IOException
    {
        ArrayList lines = new ArrayList();       
        int height = 0;
        int width = 0;
       
        BufferedReader reader = new BufferedReader(new FileReader(name));
        while(true)
        {
            String line = reader.readLine();
            if(line == null)
            {
                reader.close();
                break;
            }           
            lines.add(line);           
            width = Math.max(width, line.length());
        }       
        height = lines.size();
        map = new TileMap(width, height);
 
        for(int y = 0; y < height; y++)
        {
            String line = (String)lines.get(y);
            for (int x = 0; x < line.length(); x++)
            {
                char ch = line.charAt(x);
                int tile = ch - 'A';               
                if(tile >= 0 && tile < tiles.length)
                {
                    map.setTile(x, y, tiles[tile]);
                }               
                else if(ch == '1')
                {
                    addSprite(map,enemy1Sprite,x,y);
                }               
            }
        }
        return map;
    }


and  this is the method to draw sprites

1  
2  
3  
4  
5  
6  
7  
8  
Iterator i = map.getSprites();
        while(i.hasNext())
        {
            Sprite sprite = (Sprite)i.next();
            int x = Math.round(sprite.getX()) * TILE_WIDTH;
            int y = Math.round(sprite.getY()) * TILE_HEIGHT;
            g.drawImage(sprite.getImage(),x,y,null);           
        }

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline Graziano Mesina

Senior Newbie





« Reply #8 - Posted 2006-05-10 09:08:02 »

... and I don't use threads...

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline Graziano Mesina

Senior Newbie





« Reply #9 - Posted 2006-05-12 17:26:18 »

I've found the problem... in the draw method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
// Disegna le sprites
       Iterator i = map.getSprites();
        while(i.hasNext())
        {
            Sprite sprite = (Sprite)i.next();
            int x = Math.round(sprite.getX()) * TILE_WIDTH;
            int y = Math.round(sprite.getY()) * TILE_HEIGHT;
            g.drawImage(sprite.getImage(),x,y,null);          
        }


the lines

1  
2  
int x = Math.round(sprite.getX()) * TILE_WIDTH;
int y = Math.round(sprite.getY()) * TILE_HEIGHT;


they were no correct... now I've changed with:

1  
2  
int x = Math.round(sprite.getX());
int y = Math.round(sprite.getY());

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
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.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (65 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!