Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Player sprite just doesnt move smooth?  (Read 17908 times)
0 Members and 1 Guest are viewing this topic.
Offline Peasley

Junior Newbie





« Posted 2010-07-25 15:42:26 »

Hey guys, so I am developing my multiplayer game but I found out that my playe sprite while'st moving tends to get stuck sometimes.. not flickering but rather just CPU context switch is really long & obvious, is this makes any sense..

so I made just a rectangle which you can move with keys but it is not smooth moving, sometimes IT seems like vsync decides to drop few frames(it's just assumption, I dont know what exactly is going on..)


I have tried many high precision timers and different ways, here is the code:

here is my main window, taking care of sleeping and moving.
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  
public class Main {

   public static void main(String[] args) {
      System.out.println("2D sprite test.");
      Window fx = new Window();
       fx.g.createBufferStrategy(2);
      fx.g.buffer = fx.g.getBufferStrategy();
      long lastLoopTime = System.currentTimeMillis();
      int offset = 250;
      while(true)
      {
         long delta = System.currentTimeMillis() - lastLoopTime;
         lastLoopTime = System.currentTimeMillis();

         for(int i = 0; i < 4; i++){
            if(fx.g.keystates[i]){
               System.out.println("It is " + i);
               switch(i){
               case 0:
                  // down
                  fx.g.y += (delta * offset) / 1000;

                  break;
               case 1:
                  // left
                  fx.g.x -= (delta * offset) / 1000;
                  break;
               case 2:
                  // up
                  fx.g.y -= (delta * offset) / 1000;
                  break;
               case 3:
                  // right
                  fx.g.x += (delta * offset) / 1000;
                  break;
               }
            }
         }
   
         fx.g.draw();
         try{Thread.sleep(10);}catch(Exception e){}
      }
   }

}
MAIN.JAVA





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  
import java.awt.Canvas;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;



public class GraphicsX extends Canvas implements KeyListener {
   public BufferedImage image;
   public BufferStrategy buffer;
   public boolean keystates[] = new boolean[4];
   public int x = 20, y = 20;
   public GraphicsX()
   {
        GraphicsEnvironment ge =
              GraphicsEnvironment.getLocalGraphicsEnvironment();
          GraphicsDevice gd = ge.getDefaultScreenDevice();
          GraphicsConfiguration gc = gd.getDefaultConfiguration();
       image =  gc.createCompatibleImage(400,  400);
       setSize(500, 500);

   }
   //public void repaint() {}
   
     public void draw()
     {
        Graphics g = buffer.getDrawGraphics();
        g.setColor(Color.cyan);
        g.fillRect(0, 0, 400, 400);
          g.setColor(Color.red);
          g.fillRect(x, y, 25, 25);
          g.dispose();
          buffer.show();

     }
     
       public void keyPressed(KeyEvent e)
       {
          keystates[e.getKeyCode() % 4] = true;
       }

       public void keyReleased(KeyEvent e)
       {
          keystates[e.getKeyCode() % 4] = false;
       }
       public void keyTyped(KeyEvent e) {   }
}

GraphicsX.java



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  
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class Window extends JFrame
{
   public GraphicsX g;
   public Timer fpstimer;
   public Window()
   {
      super("lol");
      g = new GraphicsX();
      g.setIgnoreRepaint(true);
      g.setBackground(Color.cyan);
      g.setFocusable(true);
     
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      getContentPane().setBackground(Color.BLACK);
        setSize(600, 600);
       
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(g, BorderLayout.NORTH);
       
        JButton hay = new JButton("LOL");
        content.add(hay, BorderLayout.SOUTH);
   
        g.addKeyListener(g);
        setContentPane(content);
        setVisible(true);
       
        //

       g.createBufferStrategy(2);
      g.buffer = g.getBufferStrategy();
   }

}

Window.java




Can anybody check out and see what could be possibly wrong or provide me code which renders really nicely and doesnt randomly 'lag'..
I really suspect this might be the because of my Java version or maybe OS(Win7 64 bit)  Huh
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-07-25 15:46:23 »

Run with -verbose:gc and check whether the delays are at the same time as the GC.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Peasley

Junior Newbie





« Reply #2 - Posted 2010-07-25 17:46:37 »

Im not so sure how it helps you/me but here I run it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
Heap
 PSYoungGen      total 76480K, used 26487K [0x0000000015420000, 0x000000001a970000, 0x000000001a970000)
  eden space 65600K, 40% used [0x0000000015420000,0x0000000016dfde80,0x0000000019430000)
  from space 10880K, 0% used [0x0000000019ed0000,0x0000000019ed0000,0x000000001a970000)
  to   space 10880K, 0% used [0x0000000019430000,0x0000000019430000,0x0000000019ed0000)
 PSOldGen        total 174784K, used 0K [0x000000000a970000, 0x0000000015420000, 0x0000000015420000)
  object space 174784K, 0% used [0x000000000a970000,0x000000000a970000,0x0000000015420000)
 PSPermGen       total 21248K, used 8773K [0x0000000005570000, 0x0000000006a30000, 0x000000000a970000)
  object space 21248K, 41% used [0x0000000005570000,0x0000000005e01750,0x0000000006a30000)

I couldnt make it display with dates tho, seems like date option doesnt exist in new VM or is placed with something new :\

also I tried few others game which are in internet(Space inwaders for example) and they all seem to have same problem as me.
So I tried with my other computer(has vsync disabled) and it had exactly the same problem..so Im just thinking that java doesnt suit my needs for player movement, I think it is inevitable and every java game has it, it does not affect playing game but it sure does annoy my eye..  Undecided


I also used VM flags to make my heap 512 mb by default, my program used 20mb so I dont think the GC was problem anyways..
it just seems that the faster player moves, the  more obvious the random jittering is..the few games I played were pretty okish, I could definitely see the jittering but it wasnt that annoying.(althought characters in those games were moving slower by like 35% or so.)

I think this is why they say that Java isnt the best option for game development.. works well for simple games such as card games, I havent yet chance to get look at java3D library but it sounds like it is programmed using native libraries(C or so)..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Karmington

Senior Devvie


Medals: 1
Projects: 1


Co-op Freak


« Reply #3 - Posted 2010-07-25 19:15:18 »

I feel the same problem, a small unidentifiable hitch now and then, but i dont see it in puppygames games... hmmm. Theres must be a trick hidden somewhere.
Perhaps as riven suggested, with the debug console open, does the gc spout material during the hitches? I havent yet tried this being busy with another project but will definitely investigate more, as i presume android games will have similar gc processes behind the scenes. Perhaps the answer is to make sure we are not allocating/deallocating anything during gameplay?
I refuse to believe Java should just be ditched as unsuitable for gaming on a feeling, hard data is needed.. As another thread, showed, there are starting to be over a dozen marketquality games made in Java, and they have gotten around these hitches surely.

Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #4 - Posted 2010-07-25 19:22:48 »

I experience the same problem in all of my games. I don't think it is due to GC pauses because once in fullscreen exclusive mode, everything runs smooth. I'm still hoping for a Java update that magically eliminates that problem. Hoping for many years now...

lots of sillystupid games: http://www.emaggame.com
Offline Peasley

Junior Newbie





« Reply #5 - Posted 2010-07-25 19:41:40 »

I experience the same problem in all of my games. I don't think it is due to GC pauses because once in fullscreen exclusive mode, everything runs smooth. I'm still hoping for a Java update that magically eliminates that problem. Hoping for many years now...
nice, I was under the  impression that full screen would work better(i dont know why but just had feeling lol), I shall try it soon and tell how it goes.
thanks
Offline Karmington

Senior Devvie


Medals: 1
Projects: 1


Co-op Freak


« Reply #6 - Posted 2010-07-25 23:22:23 »

I tested your games hansdampf and couldnt get a glitch for 10-15 minutes ( quite good stuff too) ...but it's not fullscreen mode, so the magic is/isnt in the full screen exclusive mode?

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2010-07-26 04:30:53 »

I've never had anything but bad experiences with Java2D.

I have never had any issues like this when using LWJGL (which PuppyGames uses also). In general if you want good graphics performance you'll want to switch to OpenGL. If you want good performance without having to sign your JAR, try out PulpCore, I've only seen beautiful stuff from it.

See my work:
OTC Software
Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #8 - Posted 2010-07-26 06:00:18 »

I tested your games hansdampf and couldnt get a glitch for 10-15 minutes ( quite good stuff too) ...but it's not fullscreen mode, so the magic is/isnt in the full screen exclusive mode?
The problem is very obvious in my game 'CaveCopter', a sider-scroller. The Applet version has framedrops from time to time. There is a Webstart link below the Applet and you can go fullscreen with F11 -> runs very smooth for me. You can press F2 to see the uncapped fps. I have around 130 fps, so there should be lots of time left, but the Applet version is still jerky.

I've never had anything but bad experiences with Java2D.

I have never had any issues like this when using LWJGL (which PuppyGames uses also). In general if you want good graphics performance you'll want to switch to OpenGL. If you want good performance without having to sign your JAR, try out PulpCore, I've only seen beautiful stuff from it.
I want to live in your world, Demonpants.
I remember there was a guy defending C over Java here on this forum and asking for a simple loop to animate a moving rectangle SMOOTHLY. There was nobody here on this forum who could deliver such a loop with plain java2D and non-fullscreen. The praised Pulpcore is great, but it suffers from the same problem. Just try the
http://www.interactivepulp.com/pulpcore/tilemap/ demo. I can see glitches. I remember (90% sure) a version of that tilemap demo with the possibility of scrolling with cursor keys, when the glitches were more obvious. Now it is hard to detect because the movement is so quick.

edit: sorry Demonpants, I didn't read your first sentence correctly (just woke up), now that makes sense  Smiley

lots of sillystupid games: http://www.emaggame.com
Offline CommanderKeith
« Reply #9 - Posted 2010-07-26 06:39:02 »

Yeah the problem is actually that Java2D has no way to sync to screen refreshes. It does sync in full screen mode by doing page flipping, depending on your hardware i think. There's a discussion and a bug for it here:
http://www.java-gaming.org/topics/why-java-games-look-choppy-vertical-retrace/14696/view.html
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6378181

try out PulpCore, I've only seen beautiful stuff from it.
Pulpcore uses Java2D under the hood to render without permissions.

The problem is very obvious in my game 'CaveCopter', a sider-scroller. The Applet version has framedrops from time to time. There is a Webstart link below the Applet and you can go fullscreen with F11 -> runs very smooth for me. You can press F2 to see the uncapped fps. I have around 130 fps, so there should be lots of time left, but the Applet version is still jerky.
Applets are slow on vista since hw-acceleration is disabled due to some windows bug:
http://www.java-gaming.org/topics/winvista-bug-volatileimage-drawn-to-applet-unaccelerated/21468/view.html
Do you see a lower FPS on the applet version? With a lower FPS the screen tear would be more obvious I guess so it would look worse? With full screen mode there should be no screen tear since Java2D would use page-flipping, and should v-sync to the screen's refresh rate.

I'm sure you know this but another way to render a smooth rectangle and cover up the screen-tearing is to use motion blur, but that's a sub-par solution...

Keith  Cool

PS: with just 15 more votes that bug (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6378181) would be in the top 25 bugs. Come on guys vote it up!!

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

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #10 - Posted 2010-07-26 11:17:45 »

Can't help but to see you're using int for your positioning/offsetting. I'd recommend float for a more smooth movement.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline NewOnJava

Junior Devvie




1970 born Java game hobbyer.


« Reply #11 - Posted 2010-09-07 16:03:38 »

SmoothScrolling!

I have boardgames and clickclick site on a build, i only use Java2D with my project,
i have quite a smooth game play, i use following on my "render" loop.

1  
2  
3  
4  
5  
if ( -- this.gamesurface_sync_delay < 1 )
{
   Toolkit.getDefaultToolkit ( ).sync ( ) ;
   this.gamesurface_sync_delay = 4 ;
}


My games on a build are found here -> http://temp4322.dy.fi

There may be some slowdowns at start on windows machines, but with linux it is quite a smooth all the way.
you may should choose the "Q" key for Air Pilots 1917 to see the best and smoothest for purpose, it is still to set only for 25 fps.

( Edit.  I also use scale for smooth movement, 1500-15000 pixels applet height, and 4:3 or 16:10, seems to be enough for me. )

( Edit2. I also have here my Caveflyer demo from 2007, it is quite a smooth too, even if it is not smooth at all, when game situtations
change fast game may look smooth, even if not -> http://temp4322.dy.fi/JavaGame001/dist/launch.jnlp

//.....

Thanks..
Offline DzzD
« Reply #12 - Posted 2010-09-11 09:36:59 »

@Peasley :

your mainloop does not seems right for me especially
1  
try{Thread.sleep(10);}catch(Exception e){}

Thread.sleep can vary a lot between OS and even different call  in the same program

maybe you could try something like that

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
interval=40; 
lastDraw=0;
while(running)
{
 while(System.currentTimeMillis()-lastDraw<interval)
   Thread.sleep(1);
 
 lastDraw=System.currentTimeMillis();
 this.draw();
 
}

Offline Mordan

Junior Devvie





« Reply #13 - Posted 2010-10-27 09:20:43 »

the stuttering is obvious with this demo http://www.interactivepulp.com/pulpcore/pathmotion/



Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #14 - Posted 2010-10-27 10:12:48 »

This bug should be in the top 25 now (it hast 43 votes, minimum is 42), let's vote it up to the top

lots of sillystupid games: http://www.emaggame.com
Offline CommanderKeith
« Reply #15 - Posted 2010-10-27 11:33:48 »

You're right! woot, and I bet that when it gets up there more people will vote it up even higher

Offline Cero
« Reply #16 - Posted 2011-02-18 20:20:55 »


Since this problem still persists with JDK7 b129, I just voted for this to be fixed.
Whoever hasn't voted, should.

Offline Jack_E

Senior Newbie





« Reply #17 - Posted 2011-05-05 08:05:26 »

Quote
There's a discussion and a bug for it here:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6378181
This bug should be in the top 25 now (it hast 43 votes, minimum is 42),
let's vote it up to the top
   
OMG, it looks like this bug has disappeared from the bug database ...

Last week I tried to vote for this bug, but the voting-process
looked broken then, the site was real sloooow and nothing happend
when voting.
Now I was attempting to try again, but the bug is no longer there ...
(The site says 'This bug is not available').

Does anyone remember the title or the keywords for this bug,
so we can search the bug database for it ?
Offline Jack_E

Senior Newbie





« Reply #18 - Posted 2011-05-05 14:51:50 »

Never mind, the bug is back in the bug database.

I'm attempting to vote, but the site is, again,
real sloooow, so I guess it will not succeed this
time either ... (already waiting more than 10 minutes
after clicking 'vote' ...)  Sad

I always wondered why those bugs get so few votes,
the bugs in the top 25 only have around 250 votes or so,
but I think now I know why that is ...   Angry
Offline tberthel
« Reply #19 - Posted 2011-05-06 06:13:26 »

I have 10 game that run smooth on Android, J2ME/BlackBerry, and J2SE.  It is not a bug.  It is the code and it is not a secret.

After a game is fully loaded the game play should be smooth. 

The issues are performance related usually to threading or long running code per frame or paint.  I promise.  I get clean steady 60 fps on 59 dollar phones.

Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #20 - Posted 2011-05-06 08:04:18 »

I have 10 game that run smooth on Android, J2ME/BlackBerry, and J2SE.  It is not a bug.  It is the code and it is not a secret.

After a game is fully loaded the game play should be smooth. 

The issues are performance related usually to threading or long running code per frame or paint.  I promise.  I get clean steady 60 fps on 59 dollar phones.
So why don't you enlighten us all and provide a simple applet that renders a moving image or tilemap without hickups?
Btw, this is not about Android / J2ME/BlackBerry.

lots of sillystupid games: http://www.emaggame.com
Offline JESTERRRRRR

Senior Devvie


Medals: 7
Exp: 1 year



« Reply #21 - Posted 2011-05-06 09:31:06 »

all sounds a bit gey. I was wondering why everything did this, its really noticable in my windowed but fullscreen sized game :/
Offline ReBirth
« Reply #22 - Posted 2011-05-06 12:05:42 »

Try to avoid using sleep(). Use update(delta).

Offline tberthel
« Reply #23 - Posted 2011-05-06 13:11:08 »

I would like to give you my AllBinary Platform to make life easier, but I need to find funding first.

What I can say is lots of things can cause it, but in general it is a code performance issue(s) in your own code.  The problems include:

Thread.sleep needs to change to lower times when you have longer running frames and such
Creating to many objects causing gc to run during gameplay
Getting the system time to many times
Long running paint methods
Long running frame processing

Note: Applets are a little slower so if the lag is barely noticeable then you might remove what little lag you have left by just using JNLP or getdown.

Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #24 - Posted 2011-05-06 14:14:40 »

You really need your libraries to write a 20 lines long applet which moves a filled circle?
Hm... sounds like an excuse

lots of sillystupid games: http://www.emaggame.com
Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #25 - Posted 2011-05-06 14:49:54 »

I made a simple test with processing (javascript)

http://www.krautsoft.com/html5/incubator/Moving/index.html
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
void setup() {
   size(800,600);
   frameRate(60);
}

void draw() {
   background(150);
   int m=millis();
   int amount=50;
   for(int i=0;i<amount;i++){
   int n=m/(i+1);
   ellipse(n%width,height*i/amount,20,20);
   }
}


Please take that code (for free!!!) and make a butter smooth applet.  Grin

lots of sillystupid games: http://www.emaggame.com
Offline tberthel
« Reply #26 - Posted 2011-05-06 15:39:35 »

You really need your libraries to write a 20 lines long applet which moves a filled circle?
Hm... sounds like an excuse

No, but why would I.  A game != a filled circle

Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #27 - Posted 2011-05-06 15:45:45 »

So... you don't want to prove your awesome statements/skillz?  This would be a great opportunity to show off your AllBinary Platform.

lots of sillystupid games: http://www.emaggame.com
Offline Addictman

Senior Devvie


Medals: 3
Projects: 1


Java games rock!


« Reply #28 - Posted 2011-05-06 16:16:26 »

As stated in another thread (that's lost for me), switching from Thread.sleep to Thread.yield has solved this problem for several people.
Also, small stuff, if you're running in a JFrame, use an empty RepaintManager (since you'll be using active rendering anyway),

- which pipeline are you running with? I just booted some old Java2D games I wrote some years ago, and I have no stuttering. (They use the OpenGL pipeline for Java2D). Beyond that, and the fact that I use Thread.yield instead of Thread.sleep, the games wary hugely in both design and form, yet none of them stutter.
Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #29 - Posted 2011-05-06 16:27:49 »

If ten more persons come and claim they can animate a moving circle/rectangle without stuttering... I think I have to believe it. Why it is so complicated just to prove it and give a minimal example so that we all can learn from the rendering loop?
Doesn't seem to be that simple...

lots of sillystupid games: http://www.emaggame.com
Pages: [1] 2
  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.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!