Left sided animations are not rendered correctly

Started by
3 comments, last by Ungunbu 6 years, 11 months ago

I have a problem in my game where the left sided meleeing animations get pushed to the right instead of pushing forward to the left as they should. The reason for this is because the sprite sheet frames are extracted with their x,y locations at the top left and LibGDX TextureRegions have their origin at the top left. This means that if the current frame in an animation has a greater width than the previous frame, then the current frame looks as if it gets pushed to the right, because it is drawn from the top left.

This video shows the problem with the meleeing left animation. As you can see, because each frame has its origin at the top left, the character looks as though it is getting pushed back when it throws a punch.

[media]https:

[/media]

This shows the meleeing right animation. Since the origin is the top left, this looks natural. This is how I want the left sided animations to look, only in the opposite direction.

[media]https:

[/media]

The animation speeds have been slowed down to help you see the problem more clearly. In the game they would be faster.

I haven't been able to find a way to change the origin in the API. What can I do to fix this?

Advertisement

Instead of drawing from one of the corners, define an anchor point for each sprite frame and draw your sprite from that relative position. Somewhere between the feet is often useful, also for ground collisions etc. Hope this makes sense.

Hey, that's fantastic Prototype, it worked!

Here's the improved animation.

[media]https:

[/media]

The only problem is that now I have to go to every frame of every animation that needs it's position adjusted and manually give it a relative position to displace from the object position. This is time consuming, requires eye ball judgement for every animation and, probably worst of all, produces lots of extra initialise-esque code compared to what I had. Is that just something I'll have to live with or is there a nicer way of going about this?

Either way, thanks again Prototype, this has been something I've been unable to figure out for a good while. I'm delighted to have solved the problem.

You're welcome, nice to hear you got it working. Unfortunately there is no way around this, as no algorithm can guess how you want to align your different-sized sprites. You just need that extra piece of data. So you'd have to incorporate this in your sprite editor / file format, or use one that supports it out of the box.

Good luck!

You can put your sprites into an atlas with a fixed frame size. You would need to align the sprites to make the animation look correct. Once you have your atlas ready you can create a tool that crops the sprites (to shrink the atlas) but also generates a data file. The info you need to store for each sprite are the source rectangle and the anchor offset relative to one corner of the rect. When loading the atlas you will have to parse the data file to reconstruct the original positioning of each sprite. That should do it.

This topic is closed to new replies.

Advertisement