Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 sprite animation goes faster if the FPS is higher  (Read 579 times)
0 Members and 1 Guest are viewing this topic.
Offline Zhon

Junior Member


Medals: 4



« Posted 2013-08-06 20:29:37 »

Basically, what I do is:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void update(int delta) {
     
      time += delta;

      if (time >= interval) {

         time = 0;
         index++;
      }
     
      //draws the sprite: someSpriteArray[index].draw();
  }


The higher the FPS is, the faster the index variable increments.

Haha, call me dumb, but I'm out of ideas, I tried a lot of things to get it right, like multiplying by the current fps, among other nonsenses..

When I use the delta variable to multiply on movement stuff, like moving something, it works right.

I thought I only had to use the delta and everything would be goody-neat to go :/

Turns out to be that my entire life was a big lie. Any help is appreciated =o
Offline R.D.

Senior Member


Medals: 2
Projects: 1


"For the last time, Hats ARE Awesome"


« Reply #1 - Posted 2013-08-06 20:38:10 »

Do you use any engine? It could be because you use a constant delta value or you smooth the delta value.
Offline Jeremy
« Reply #2 - Posted 2013-08-06 20:43:12 »

Basically, what I do is:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void update(int delta) {
     
      time += delta;

      if (time >= interval) {

         time = 0;
         index++;
      }
     
      //draws the sprite: someSpriteArray[index].draw();
  }


The higher the FPS is, the faster the index variable increments.

Haha, call me dumb, but I'm out of ideas, I tried a lot of things to get it right, like multiplying by the current fps, among other nonsenses..

When I use the delta variable to multiply on movement stuff, like moving something, it works right.

I thought I only had to use the delta and everything would be goody-neat to go :/

Turns out to be that my entire life was a big lie. Any help is appreciated =o

If time is 10*interval, you now have skipped 10 intervals.
Use a for loop

1  
2  
3  
4  
for(;time>=interval;time-=interval)
{
    index++;
}

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zhon

Junior Member


Medals: 4



« Reply #3 - Posted 2013-08-06 20:48:30 »

Do you use any engine? It could be because you use a constant delta value or you smooth the delta value.

Nope, I only use LWJGL, the delta I use is the one seen here

If time is 10*interval, you now have skipped 10 intervals.
Use a for loop
1  
2  
3  
4  
for(;time>=interval;time-=interval)
{
    index++;
}


Sorry, I didn't get it, wouldn't this for-loop happen only once per call (since the time doesn't reset to 0)?
Offline Jeremy
« Reply #4 - Posted 2013-08-06 20:58:44 »

Do you use any engine? It could be because you use a constant delta value or you smooth the delta value.

Nope, I only use LWJGL, the delta I use is the one seen here

If time is 10*interval, you now have skipped 10 intervals.
Use a for loop
1  
2  
3  
4  
for(;time>=interval;time-=interval)
{
    index++;
}


Sorry, I didn't get it, wouldn't this for-loop happen only once per call (since the time doesn't reset to 0)?

Lets say the game freezes for a couple of seconds and you application doesn't get enough CPU time. You get a temporary frame drop that and it takes much longer for your update routine to be called than usual.

Say your delta time was 5000 milliseconds and your interval is 1000 milliseconds. With your implementation,

We add 5000 to our time.

Is time >= 1000 ? Yes, so set time to zero, and move to the next frame - wait, what about those other 4000 seconds we totally discarded? Now we're four frames behind and we'll never catch up.

With my algorithm:

is 5000 > 1000? Yes, subtract 1000 from 5000, now it is 4000. Move to the next frame.
is 4000 > 1000 ? yes, subtract 1000 from 4000, now it is 3000, move to the next frame.

Etc

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Zhon

Junior Member


Medals: 4



« Reply #5 - Posted 2013-08-06 21:08:47 »

But after some loops the time will turn to be smaller then the interval and will no longer enter the for loop?

Also, on that for-loop, the index might increment more than one time per update call.. making it skip some indices (sprite frames)

I use the update call to increment the index variable and use it to draw a sprite afterwards. If that for loop increments the index by "3" in a single update() call it will skip 3 frames..

I think we're thinking on different scenarios, or I'm just not getting your thought :/
Offline Jeremy
« Reply #6 - Posted 2013-08-06 21:13:12 »

But after some loops the time will turn to be smaller then the interval and will no longer enter the for loop?

Also, on that for-loop, the index might increment more than one time per update call.. making it skip some indices (sprite frames)

I use the update call to increment the index variable and use it to draw a sprite afterwards. If that for loop increments the index by "3" in a single update() call it will skip 3 frames..

I think we're thinking on different scenarios, or I'm just not getting your thought :/

That's the whole point - it should skip frames if it's lagging behind.

Time will get smaller and smaller but you're also adding deltaTime to it - and it only gets smaller when it is >= the interval at which you want to swap frames (and you swap frames.)

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline jonjava
« Reply #7 - Posted 2013-08-06 21:22:21 »

Here's a couple excerpts from a thing - maybe they will help. For basic animation i simply check the System.currentTime() to a delay variable: (FPS has no effect):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
private void updateEyes(long now) {
      if (now > lastBlink + blinkDelay) {
         blinking = !blinking;
         lastBlink = now;
         Bitmap tmp = eyes;
         eyes = blink;
         blink = tmp;
         int delay;
         if (blinking)
            delay = 100 + Game.random.nextInt(100);
         else
            delay = 400 + Game.random.nextInt(1400);
         blinkDelay = delay;
      }
   }


For animation based on movement I increment a variable based on the speed and pick a rounded index to pick the sprite image: (FPS has no effect):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private void updateLegs() {
      if (xspeed == 0) {
         legs = legsArray[legsArray.length / 2];
         legsImage = legsArray.length / 2;
      } else {
         legsImage -= xspeed * 0.2;

         int n = ((int) legsImage) % legsArray.length;

         while (n < 0) {
            n += legsArray.length;
         }

         legs = legsArray[n];
      }
   }

Offline supaFool

Junior Member


Projects: 2



« Reply #8 - Posted 2013-08-08 20:26:46 »

for a basic tile to tile movement, just turn down framerate

1  
2  
3  
4  
5  
@Override
   public void init(GameContainer container, StateBasedGame game)
         throws SlickException {
      container.setTargetFrameRate(5);
   }


or you can do a if statement

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
@Override
   public void update(GameContainer container, StateBasedGame game, int delta)
         throws SlickException {
      container.getInput().enableKeyRepeat();

               

      double timercounter = .25;

      if (container.getInput().isKeyPressed(Input.KEY_RIGHT)) {
         if (player.canMoveRight()) {
                               if(timer = 0){
                               moveRight(); ////to keep it from lagging
}

            timer += timercounter;

            if (timer > 1) {
               moveRight(); /// then do again every 4 frames, 1 / .25 = 4
              timer = 0;
            }
         }
      }


make sure you have a private var called timer init to 0.
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.

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

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

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

Riven (19 views)
2014-07-23 20:56:16

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

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

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

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

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

Riven (52 views)
2014-07-14 18:02:53
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!