G
G
gosha_tst2018-01-02 01:19:55
C++ / C#
gosha_tst, 2018-01-02 01:19:55

How animation works?

I can't figure out how the animation works. There is such an algorithm (2D platformer in SFML): there is a tileset containing character textures. In one of the fields of the character class, the currentFrame and FloatRect rect are created, and in the main loop, the time variable is divided by some number. Function code that uses these variables:

void update(float time) {
  sprite.setTextureRect(IntRect(0, 190, 40, 50)); // Устанавливает спокойный спрайт игрока

  rect.left += dx * time; // rect.left - координата x
  if (!onGround) {
    dy += 0.005*time; // 0.005 - ускорение, 
  }
  rect.top += dy*time; // rect.top - координата y
  onGround = false;
  if (rect.top > ground) {
    rect.top = ground;
    dy = 0;
    onGround = true;
    // Если координата по y больше координаты земли по y, то скорость по y = 0, а игрок становится на земле
  }

  currentFrame += 0.005; // 0.005 - скорость анимации 
  if (currentFrame > 6) currentFrame -= 6;
    
  if (dx>0) sprite.setTextureRect(IntRect(40 * int(currentFrame), 244, 40, 50));
  if (dx<0) sprite.setTextureRect(IntRect(40 * int(currentFrame) + 40, 244, -40, 50));

  sprite.setPosition(rect.left, rect.top);

  dx = 0;
}

The questions themselves are: 1) why does currentFrame reset exactly after reaching 6? 2) why is the x coordinate multiplied by the rounded currentFrame in the last two conditions? 3) why are rect.top and rect.left added to dx(y)*time?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2018-01-02
@gosha_tst

time is the duration of the clock (in the game, obviously, not just a variable FPS, but also a variable clock frequency). I wrote this, however, not in a platformer, but in a race.
rect.left is the exact solution of the difur left′ = dx (moving at constant speed). Obviously dx is being set by management somewhere.
rect.top and dy is an approximate solution of difur top′′ = 0.005 if the onGround flag (flying under gravity) is unchecked. Here 0.005 is the acceleration due to gravity, the Y axis is pointing down. The difur of the second order is transformed into the system top' = dy, dy′ = 0.005, and what was exact in the first case is approximate here, but acceptable for games.
currentFrame is simply scrolled to make the 6-frame animation run in 1200 cycles, regardless of the frequency. Here 1200 = 6 / 0.005.
Note that dx and dy go through different data paths: one is the state of control, the second is the state of the character's physics. In addition, what does dx = 0 do and where it is set not to 0, so as not to depend on the frequency of the auto-repeat of the clave, is not clear. And a lot of magic constants. Shitcode.
UPD. Direct answers to your questions.
1) 6-frame animation, but here one of two. Or because of the huge cycle (1200 cycles), it is obviously not the legs that are animated. Or maybe the author drives cycles with the maximum frequency and therefore 1200 cycles will really pass in a second or two, but then this is too obvious a binding to the speed of the computer;
2) depending on the frame number, select one or another sprite on the atlas;
3) dy is the speed, and to correct the position (rect.y), you need to add speed time to it.
UPD2. In addition, it is not clear why there is no running in different directions in the animation code. Although, due to the extremely simple physics, there is a suspicion that the genre is an endless runner.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question