DEV Community

Cover image for Feet on the Floor: Why Your Cut Sprites Jitter (It's the Anchor)
Crane Zhou
Crane Zhou

Posted on Fully Autonomous

Feet on the Floor: Why Your Cut Sprites Jitter (It's the Anchor)

Here are two animations made from exactly the same eight frames. Same pixels, same timing, same slime.

Two slime animations. In the left one the slime hovers in the middle of its box and the ground seems to move. In the right one the slime sits on the bottom edge and hops straight up

On the right, a slime hops. On the left, a slime holds perfectly still in mid-air while the planet moves up and down beneath it.

The difference is one switch, and if you've ever cut a sprite sheet and watched your character vibrate, this is almost certainly why.

Where the jitter comes from

When you cut sprites out of a sheet by their actual pixels, instead of by a fixed grid, each frame comes out a different size. A squashed slime is wide and short. A stretched one is narrow and tall. Here are the eight frames of that hop, each trimmed tight:

Eight trimmed slime frames in a row, each with a different width and height, labelled 22×15, 26×12, 24×14, 18×21, 18×25, 20×25, 20×20 and 28×11

From 28×11 to 18×25. No two the same.

That's a problem, because almost everything that plays animations wants every frame to be the same size. So each frame gets padded with transparent pixels up to the size of the biggest one, here 28×25.

And "padded" is hiding a decision. The frame is smaller than its new cell. Where in the cell does it go?

Centre is the wrong default

The natural answer is the middle. Equal padding on all sides. It's what "centre" means, and it's what most tools do without asking.

Think about what that does to a jump. In the tallest frame, the slime fills the cell top to bottom. In the shortest frame, 11 pixels tall in a 25 pixel cell, it gets 7 pixels of padding above and 7 below. Its feet are now 7 pixels off the bottom of the cell.

But the slime's feet didn't move. It's squashed flat on the ground in that frame. The centring moved them.

So across the animation, the bottom of the character bobs up and down by an amount that has nothing to do with the art and everything to do with how tall each frame happens to be. When the body stretches upward, centring pushes the feet down to compensate. You get that "floating in a jar" look on the left.

Bottom-centre is what you meant

For anything that stands on a surface, the pixel that should stay still is the one touching the ground. So pad from there: centred left to right, flush with the bottom.

const dx = Math.floor((cellW - frameW) / 2);
const dy = anchor === "bottom"
  ? cellH - frameH                      // feet on the floor
  : Math.floor((cellH - frameH) / 2);   // floating in a jar
Enter fullscreen mode Exit fullscreen mode

That's the whole fix. One subtraction instead of a division. Every frame's bottom edge lands on the same row, and all the size change happens upward, where a jump is supposed to happen.

It also matches what your engine does next. A character's position in a game is nearly always its feet: that's the point you compare against the ground, the point enemies aim at, the point you sort by for draw order. If the art's anchor and the game's anchor agree, you never write an offset table.

When centre is actually right

Bottom isn't a universal law. It's right for things with feet. For things without them, pick the point that stays still in the world:

  • A spinning coin, a pulsing orb, an explosion: centre. They grow outward from their middle.
  • A flying enemy, a floating ghost: usually centre. There's no floor to agree with.
  • A hanging lamp, a dripping stalactite: top. The attachment point is the ceiling.
  • A character's walk, run, idle, jump, attack: bottom.

The question is always the same: which pixel is bolted to the world? Anchor there.

The other fix: don't trim per frame

There's a way to dodge the whole question. Instead of trimming each frame to its own pixels, find the one rectangle that contains every frame's pixels, and crop them all with that.

The slime hopping inside a single box that never changes size

Same crop for all eight frames: 28×25, the smallest box that never cuts anything off. Since every frame is cut from the same place, nothing can shift relative to anything else. No anchor needed, no jitter possible.

That's the right move when frames are already aligned and you just want the empty margin gone. You lose nothing and risk nothing.

Per-frame trimming and an anchor are for the other case: when the frames weren't aligned to begin with. A sheet drawn by hand without a grid, or assembled from separate files, or generated. There, each sprite sits wherever it sits, the only reliable reference is the sprite itself, and you have to say which part of it is the floor.

If you want to see it for yourself, the sprite sheet cutter has this as a switch called "Line sprites up by their feet". It's on by default. Turn it off while the preview is playing and watch the floor start moving.

Ever shipped a character that moonwalked because of an anchor?

Top comments (0)