Problem with texture flipping

Jul 8, 2011 at 8:35 AM
Edited Jul 8, 2011 at 9:05 AM

In the editor I have added bone for the left arm, then I have added new bone for the right arm with the same texture and flip it horizontally. It shows correctly in the editor, but in viewer that texture looks not flipped. In my own code it looks differently also, they flipped, but positioning is wrong. Any thoughts? Probably I am doing something wrong.

Looks like it happens when flipped texture is child of non flipped texture. If parent is flipped then flipped child draws ok.

Coordinator
Jul 8, 2011 at 2:56 PM

I think there's a fairly good chance that there's a bug in that. I added the feature a long time ago, but I think you're the first person to ever use it. :)

I'll take a look as soon as I can. In the meanwhile, it might be better if you create a pre-flipped image. You can always switch back if I manage to fix the bug later on.

Jul 11, 2011 at 7:31 AM

That feature is greatly simplify and unify the code for me. Now I have managed to workaround this bug with some texture changes, but it would be nice if you will be able to fix that.

Btw, Demina is the great tool, probably the best animation time saver for XNA I have ever discovered. Thank you so much!

Coordinator
Jul 11, 2011 at 7:29 PM

I updated the viewer to show the texture flips, and it's working.

After that I tested an animation in one of my games, and it seemed to render properly. If you still have a problem, please let me know.

Jul 12, 2011 at 11:01 AM

Thank you! Viewer is working fine now.

Unfortunately, not runtime classes.

Strangely enough the all stuff you have changed are already presented in runtime classes, but works unstable for me, some of flipped textures looks fine, others moved by X axis. I have debugged the code a little bit, but could not find anything.  spriteBatch.Draw in AnimationPlayer draws positions exactly as they defined in anim xml, but then something goes wrong and they rendered with offset.

Could it be something wrong with matrix in spriteBatch.Begin?

 

Jul 12, 2011 at 12:07 PM
Edited Jul 12, 2011 at 12:58 PM

Looks like I have fixed the problem, but I am not sure if these changes are correct, could you check please if it makes sense?

In AnimationPlayer I have added origin offset, replacing the row below: 

 

                spriteBatch.Draw(animation.Textures[bone.TextureIndex].Texture, position + BoneTransformations[boneIndex].Position, animation.Textures[bone.TextureIndex].TextureBounds.Location, tintColor,
                    BoneTransformations[boneIndex].Rotation, animation.Textures[bone.TextureIndex].TextureBounds.Origin,
                    BoneTransformations[boneIndex].Scale, spriteEffects, 0);

With this:

 

                Vector2 origin = animation.Textures[bone.TextureIndex].TextureBounds.Origin;
                SpriteEffects spriteEffects = SpriteEffects.None;
                if (bone.TextureFlipHorizontal)
                {
                  spriteEffects = SpriteEffects.FlipHorizontally;
                  origin.X = animation.Textures[bone.TextureIndex].TextureBounds.Location.Width - origin.X;
                }

                spriteBatch.Draw(animation.Textures[bone.TextureIndex].Texture, position + BoneTransformations[boneIndex].Position, animation.Textures[bone.TextureIndex].TextureBounds.Location, tintColor,
                    BoneTransformations[boneIndex].Rotation, origin,
                    BoneTransformations[boneIndex].Scale, spriteEffects, 0);

And now it renders correctly for me.

Forgot to add - I am using textures dictionary, this is probably the reason why origins are not at the center of texture.

 

Coordinator
Jul 13, 2011 at 2:42 PM

Ah, there is indeed a problem when using the dictionaries. I'll look into it right away.

Coordinator
Jul 13, 2011 at 2:57 PM

I've made a change that takes care of this.

The problem was that when you flip a texture using SpriteEffects, it doesn't flip around the origin of the texture, just around the center. When the textures have their white space trimmed (as happens during the packing of texture dictionaries) the center is moved, but the origin is not.

Now I'm using negative scaling to mirror the textures. This means you have to disable back face culling if you're handling your own SpriteBatch.Begin. (That's handled automatically by Demina if you use the normal Draw calls.)

I updated the sample application to show this working. It's in the svn if you'd like to see it.

Jul 13, 2011 at 4:47 PM

It works perfectly, thank you for the great product!