• entries
13
15
• views
29837

# 2D Skeleton Woes

1591 views

About two months ago, I started writing a 2D game. Given that my previous work was on a 3d deformable terrain, I figured a nice 2D game would be a nice change of pace, and give me less hassle. I was right...mostly.

Character animation in 3d is not a simple task. There's some great software out there to help you animate it, heck, two years ago even I wrote a simple character animation program that had the ability to automatically attach a mesh to a skeleton. But enough nostalgia!

It seems that skeleton animation in 2D should be easier. You only have one simple rotation angle to worry about, and no need to account for the gimbal lock problem using those pesky yet mathematically beautiful quaternions.

Here's a simple 2d texture skeleton in my game:

Seems straight forward enough to animate. You don't have to necessarily worry about vertex attachments, you can make each bone have its individual sprite, and design them in such a way that they blend in together.

But you don't want to design your animations twice do you? Because a walking animation should be able to be played both walking LEFT and RIGHT. So, need a way to easily flip animations. Unlike in 3d where you can rotate your animation along some axis to orient it in the proper direction, in 2D you have to actually mirror the animation.

I'm cheap, so I decided to go for a cop-out - I'm going to only flip the sprites instead of the whole skeleton. Sound good? Yea. But we can't just flip the image in the sprite - you have to take the actual sprite rectangle and mirror all its vertices along a certain axis. Since SFML doesn't support this, time to write my own CustomSprite class.
Still, not the hardest thing. The simple code for flipping a vertex along an arbitrary x-axis:sf::Vector2f CustomSprite::FlipHorizontal ( const sf::Vector2f &point ) const{ sf::Vector2f pt = point; pt.x = m_axisIntersect - ( pt.x - m_axisIntersect ); return pt;}
The variable m_axisIntersect specifies the line along which to flip. The same method can be used to flip along an arbitrary y-axis-aligned line.
So, here are the results:

Ok, the actual bones (which may be a little hard to notice - they're the thin blue lines) aren't flipped, but the sprite flip seems to have worked fine. The results look promising so far.

Except, I forgot - my character isn't just going to stand always oriented up. Due to the physics of the game, he will lean on slopes and corners. Here's an example:

So, wait, what happens if I use the direction flip on a slope? Well...

Oh, right. I'm flipping along the axis-aligned line that passes through the center of the character, so of course - the program is doing exactly what I told it to do, even if i told it to do was wrong.

It looks like I'm going to have to apply a mirroring along an arbitrarily oriented line now. Mirroring around an arbitrary line isn't that bad, though it's certainly more involved.
Supposing we have a line that passes through the point p, by which you want to mirror. The basics are then:
1. Translate all points by a vector -p - so now the origin of the line matches the global origin.
2. Rotate all points so that the line you want to mirror by is aligned with one of the axis
3. Mirror around that axis using the same method above
4. Undo step 2
5. Undo step 1

While I was thinking about this, I realized that I actually have all my sprites on the model in local coordinates already - they store their positions relative to the model's origin, which is the center point through which the mirroring line will have to pass. And I'm already setting the model's orientation when I touch a slope, so I already have a function that rotates it.
In fact, I was setting the rotation like this: float angle = atan2( -up.x, up.y )*180.f/(float)PI;m_rootBone.SetRotation( angle );
However, I knew that when I mirrored the model, I could simply re-adjust to 'up' vector that the model received so that it was now facing the right direction:float angle = atan2( -up.x, up.y )*180.f/(float)PI;if ( m_rootBone.GetSpriteFlip().first == CustomSprite::xAxisFlip ){ m_rootBone.SetRotation( 360.f - angle );}else{ m_rootBone.SetRotation( angle );}
And the results now looked good:

Sure, the actual skeleton was nowhere near what the sprites displayed were, but that doesn't matter. The skeleton is only used to draw sprites, not for collision, or any other purpose.

At the end making a 2d skeleton overall was easier than a full 3d skeleton, but had some challenges that you don't face when dealing with 3d.

There are no comments to display.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.