Flipping 2D sprite for platformer

Alright, here’s the gist of it. I have a sprite that’s animated. It’s on a quad, and it’s got a characterControl, and he runs to the right just fine. When he runs to the left, I need to flip the texture so it’s facing left. Now, I’ve tried scaling by -1 and setting the view direction–both don’t work.

So, really, the question is: how do you flip a texture on a quad across the x axis?

More info: the texture is really a spritesheet whose texcoords are being changed after a bit of time (I think).

Scaling by -1 only works if your texture is set to repeat.

Well, I did that, and the same thing occurs, which I’ll explain.

If I use scale(-1,-1,-1), I see the image, but it’s upside-down and reversed. If I do (-1,-1,1), it still works like that. If I do (-1,1,1), or (1,-1,1) it doesn’t work. Both these make the texture disappear. Any thoughts why?

i will not be able to help you a lot as i don’t have a “test code”. However, you could maybe try (1,1,-1) or any other possibility.

I post here just to say : if you are creating a 2D plateformer game, jme is maybe not the best engine for this, and charactercontrol is likely not the best control at all for this. Of course that jme can do a 2D game, but you will likely end with something faster with an appropriate engine for this kind of game. I am not sure(certain) of that however, only at 99% (i am not aware of lastest hardware accelerations).

What i sure it that charactercontrol is not the better thing for a 2d game, as there is a lot of approximations on it and you may want to don’t have such approximations. For exemple, charactercontrol has problems with roofs (warp etc.).

@machspeeds said: Well, I did that, and the same thing occurs, which I'll explain.

If I use scale(-1,-1,-1), I see the image, but it’s upside-down and reversed. If I do (-1,-1,1), it still works like that. If I do (-1,1,1), or (1,-1,1) it doesn’t work. Both these make the texture disappear. Any thoughts why?

Well, why do you scale by -1, -1, -1 if you only want to flip one axis? -1, 1, 1 seems right to me. If it was backwards and upside down with -1, -1 then there should be no reason it’s invisible with -1, 1

I agree with bubuche that we can’t debug code we cannot see… so until then I guess it’s up to you to figure out.

Actually, are you scaling the texture coordinates or the whole shape?!?

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Mesh.html#scaleTextureCoordinates(com.jme3.math.Vector2f)

Well, I’m scaling the whole shape. In fact I’m scaling a node the shape is attached too. However, I just tried using negative values for flipping the texture using scaleTexCoords. It does nothing to the texture; looks like it just ignores values < 0

And I realize I haven’t given you the code. But in reality, I’m using Nyphoon Game’s sprite engine, so the only code to see on my end thus far was either:

playerNode.scale(-1);

or

characterControl.setViewDirection(walkDirection);

Well, when you scale a mesh by -1, 1 then all of the winding will be backwards.

If you scale the texture coordinates by -1, 1 then it will flip the texture. Make a simple test case and try it… then back up from there and find out what’s different. Note: you will have to have repeat set on the texture for it to work.

Okay, that’s funny. Per your suggestion (I should have done so in the first place), pspeed, I made a test case. I am able to flip my guy over the y axis using scaleTextureCoordinates(1f,-1f). But he will not, for the life of me, flip on the x, using -1f,1f. Here is the relevant code:

Simple creation of sprite with quad, tex, material, etc:

[java]Texture s_spritesheet = assetManager.loadTexture(imageLocation);
s_spritesheet.setWrap(Texture.WrapMode.Repeat);
Quad s_quad = new Quad((int)(s_spritesheet.getImage().getWidth()/frames/240)(int)(s_spritesheet.getImage().getHeight()/rows/305));
s_quad.scaleTextureCoordinates(new Vector2f(-1f,1f));
s_geometry = new Geometry(name, s_quad);
Material s_material = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”); s_material.setTexture(“ColorMap”, s_spritesheet);
s_geometry.setMaterial(s_material);
if (transparent){
s_material.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
}
[/java]

Attaching to root:

[java]playerSprite = new Sprite(“Textures/KnightForward.png”, “Player”, assetManager, true, true, 24, 1, 0.02f, “Loop”, “Start”);

    playerSprite.setPaused(false);
    SpriteLibrary library = new SpriteLibrary("Library 1", false);
    SpriteLibrary.setL_guiNode(rootNode);
    library.addSprite(playerSprite);
    engine.addLibrary(library);
    
    playerNode = new Node();
    playerNode.attachChild(playerSprite.getNode());
    playerSprite.move(-.5f,-.5f);

    rootNode.attachChild(playerNode);[/java]

The spritesheet used as a texture is 24 frames in 1 row.

EDIT: In fact, any scaleTextureCoordinates(x,1) where x is ANY number does absolutely nothing.

Have you thought about just rotating the quad backside and set the facecullmode to display backwards triangels as well?

Do a test without the sprite library involved as I think that must be resetting the texture coordinates or something. I would think it would have to if it is using a sprite sheet.

This is a sprite library problem, I guess. Empire’s approach may be the only simple way to get this to work… but I have 0 familiarity with this sprite library.

Alright, I got it working. All I did was rotate the sprite about the z axis (so it’s upside-down) and then flip it using scaleTextureCoordinates(1f,-1f) and then fiddled with it’s translation to put it back in the right place!

I agree though, this was a little complicated for such a simple action, and maybe JME3 isn’t made for 2d games like this. But I’m of the believe that, in much the same way Unity finally came out with a 2d toolkit for its audience because lots of people were using it for 2d games, all it takes is someone to start using the engine in a way people haven’t used it before to get the the bandwagon rolling.

And I haven’t seen a pure 2D platformer made from JME3 yet, so I’m happy to (probably) be a first!

Thanks for all your help guys. This community support is what keeps me motivated (and makes me want to learn shaders more and more)!

Nothing. To. Do. With. JME.

I flip textures this way on regular quads all the time. This is 100000% to do with the sprite library you are using.

Okie dokie. Thanks for telling me. I’ve taken it up with the library’s creator already.