Pages

Thursday, September 8, 2011

What the flip yo... ID3DXSprite flipping

For a 2D platformer, it's often true that the "Run" animation frames are the same in either direction (run left, run right). It makes sense then to save texture space by allowing a sprite to be flipped.

While some sprite libraries come with the option to flip horizontally or vertically, the ID3DXSprite interface does not. If you're rolling your own sprite class using a quad, you can easily add this functionality by inverting the texture co-ordinates.

Another method of flipping, which works for ID3DXSprites as well, is to inverse the scale on the flipped axis.

Here's a simple pseudo code example:
D3DXVECTOR2 scale;
D3DXVECTOR2 position;
...
if(FlippedOnX)
{
   scale.x = -scale.x; //Invert the sprite on the x axis
   position.x += spriteWidth; //Shift the sprite by the amount of 'width' to re-position after the mirror flip
}

if(FlippedOnY)
{
   scale.y = -scale.y;
   position.y += spriteHeight;
}

Of course, you can get rid of the two if statements:
D3DXVECTOR2 scale;
D3DXVECTOR2 position;
...
scale.x = (scale.x * !FlippedOnX) + (-scale.x * FlippedOnX);
scale.y = (scale.y * !FlippedOnY) + (-scale.y * FlippedOnY);
position.x = posixion.x + (spriteWidth * FlippedOnX);
position.y = posixion.y + (spriteHeight * FlippedOnY);

This is far from secret knowledge, but when I was looking for an answer to this question, I found mostly short forum responses, such as: "Just invert the scale".  While this is to the point and it's easy enough to try for yourself, I figured an example and a brief explanation could have been of some use.

Thursday, September 1, 2011

Elpis 2D

I've been absent from blogging for some time now, and this I choose to blame on the lack of interesting things to say or willingness to say it. Anyway, I haven't ceased development and this post will bring the blog up to date with what I've been doing on Elpis.

The focus for the past little while has been on a 2D puzzle platformer game, or rather, the engine and script features required by this game. While Elpis was originally a 3D engine, I was fond enough of the framework to add 2D features to it instead of making a specialized 2D engine from scratch.

One feature we are toying with for this game is having multiple scenes displayed and semi-active at the same time. At first, I thought this might be problematic (being that we all like as large a display area as possible), but it's grown on me. The practicality of this feature is to allow the user to quickly swap between scenes to solve puzzles that take place in multiple game areas simultaneously, and to display to the user what effects he is having in each scene when interacting with another. We are aware that this feature has very limited uses, but it may prove effective as a puzzle element.

A small list of features that have been added for this game:
  • Simultaneously active scenes
  • Tiling 2D scene elements
  • Animated sprites
  • Basic 2D physics (simple, scalable, collisions using circles and rectangles as bounding volumes, sprite velocity)
  • Sepia toning (post processing effect)
  • Scene layer scrolling at varying speeds
  • Sprite animation frame audio triggers (ie, frame 3 of hero's run animation triggers the 'footfall' audio clip)
  • Audio playback using XACT

As for the game itself, there will be a dedicated post for it, but here's an image of a scene.

A conceptual throw together of one of the potential scenes for the game.