Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Wobbly/Blurred Animations after capping framerate (Slick2D)  (Read 249 times)
0 Members and 1 Guest are viewing this topic.
Offline timeaisis

Junior Newbie





« Posted 2013-05-09 00:09:38 »

Hey all,

I have a question about something that's puzzling me. Let me give you some details:

I have a simple game written in Slick2D presently with a player character and two NPCs that follow some simple pathfinding (and move on their own). My framerate was uncapped and in the thousands. Anyhow, I recently decided to cap my framerate via gc.setTargetFramerate to 60. Naturally, I had to alter some of my movement code to take into account delta (which I wasn't accounting for previously, as my framerate was unlimited...). So after doing that, I noticed my NPCs movement animations were displaying very wobbly (almost blurry), as if they were shaking as they were moving. Strangely enough, after editing my player movement code to account for delta, his movement remained smooth and natural.

My question to you all is, does anyone have any idea why my NPCs begin to wobble/blur on movement after I cap my framerate? Note this only happens during movement, and not while standing still.

I'm pasting some of my code below. The first one is a snippet from my NPC movement code. The second is a snippet from my player movement code.

NPC movement...was working before accounting for delta
Note that they are simply moving along a path found via a previous method.
Also note that setSpriteForPath is for determining with Animation the NPC will use (walking left, walking right, etc.) when 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  
46  
47  
48  
49  
50  
51  
52  
    public void moveAlongPath(int delta)
    {
        float newSpeed = speed * delta; //here is the only change after capping the FPS at 60. Previosuly this line was simply newSpeed = speed; (speed=.1f)
       if (movingAlongPath && !isDead)
        {
            if (path.getX(pathIndex) < (x/tileSize))
            {
                x-= newSpeed;
                stepDistX -= newSpeed;
            }
            else if (path.getX(pathIndex) > (x/tileSize))
            {
                //System.out.println("x>...x is" + x);
               x+= newSpeed;
                stepDistX -= newSpeed;
                //System.out.println("now x is..." + x);
           }
           
            if (path.getY(pathIndex) < (y/tileSize))
            {
                y -= newSpeed;
                stepDistY -= newSpeed;
            }
            else if (path.getY(pathIndex) > (y/tileSize))
            {
                y += newSpeed;
                stepDistY -= newSpeed;
            }
            //If the stepDist is like 0.0001, we're making sure that counts as zero.
           if ((stepDistY <= 0 || stepDistY-.1f <=0) && (stepDistX <= 0 || stepDistX-.1f <=0))
            {
                pathIndex++;
                if (pathIndex < path.getLength())
                {
                    stepDistX = Math.abs(path.getX(pathIndex) * tileSize - x);
                    stepDistY = Math.abs(path.getY(pathIndex) * tileSize - y);
                    setSpriteForPath();
                }
                else
                {
                    movingAlongPath = false; ///break out of the loop
                   pathIndex = 0;
                    stepDistX = 0;
                    stepDistY = 0;
                    path = null; //kill the path
                   setEndSprite();
                    trans();
                }

            }
        }
    }


Player Movement...
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  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
public void updatePlayer(int delta)
    {
        boolean isWalking = false;
        float xChange = 0, yChange = 0;
        Input input = gc.getInput();
 
        int runMod = 1;
        if (input.isKeyDown(Input.KEY_LSHIFT))
        {
            runMod = 2;
        }
        if(input.isKeyDown(Input.KEY_A))
        {
            //LEFT
           sprite = walkingAnimLeft;
            sprite.start();
            xChange -= speed * runMod * delta;
            isWalking = true;
            facing = "left";
        }
 
        if(input.isKeyDown(Input.KEY_D))
        {
            //RIGHT
           sprite = walkingAnimRight;
            sprite.start();
            xChange += speed * runMod * delta;
            isWalking = true;
            facing = "right";
        }
 
        if(input.isKeyDown(Input.KEY_W))
        {
            //UP
           sprite = walkingAnimUp;
            sprite.start();
            yChange -= speed * runMod * delta;
            isWalking = true;
            facing = "up";
        }
        if(input.isKeyDown(Input.KEY_S))
        {
            //DOWN
           sprite = walkingAnimDown;
            sprite.start();
            yChange += speed * runMod * delta;
            isWalking = true;
            facing = "down";
        }
        if(input.isKeyPressed(input.KEY_E))
        {
            toggleEquip();
        }
        //If the player is not walking, stop the animation
       if (isWalking && !currentMap.blocked(x+xChange, y+yChange, facing))
        {
            x += xChange;
            y += yChange;
            Transition tempTrans = currentMap.getTran(x+1, y+1);
            if (tempTrans!=null)
            {
                //System.out.println("transitioning...");
               this.trans(tempTrans);
                //transition code here
           }
           
        }
        if (!isWalking)
        {
            sprite.setCurrentFrame(1);
            sprite.stop();
        }
        isWalking = false;
    }


Any help would be immensely appreciated! Thanks!  Smiley
EDIT: I also took out the setTargetFramerate and kept the newSpeed = speed * delta, and it's also smooth. So it seems like it has to do with the framerate. Possibly something to do with Slick2D pathfinding? Maybe a wobbly path is not noticeable at a super high framerate?
EDIT2: I tried a with random movement (no pathfinding, just randomly moves places) and it seemed to move smoothly. Could this be like little "hiccups" with pathfinding?
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.

CogWheelz (9 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
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!