<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Horizontal Flip with non-centred character sprites</title>
		<link>http://www.allegro.cc/forums/view/589634</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 20 Jan 2007 14:53:05 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This question is with regards to a 2D fighting game.</p><p>Horizontal flip, in logic, works fine for character sprites which need to face the other directiron, i.e.:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>Player_facing<span class="k3">=</span><span class="k3">=</span><span class="n">1</span><span class="k2">)</span>
<a href="http://www.allegro.cc/manual/draw_sprite" target="_blank"><span class="a">draw_sprite</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">else</span>
<a href="http://www.allegro.cc/manual/draw_sprite_h_flip" target="_blank"><span class="a">draw_sprite_h_flip</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

However, a problem arises if the characters are not centered in the graphic. For example: <img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/b/5/b5bcc530b126f4d80a1dca1fd94c668f.jpg" alt="591031" width="200" height="280" /></p><p>Horizontally flipping it results in this</p><p><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/e/2/e298e8e51650de17af3c743f45316b1f.jpg" alt="591034" width="200" height="280" /></p><p>Now the main problem is, the drawing functions use the top left part of the image to be the &quot;start&quot; point. So when the player changes facing/direction, he actually &quot;moves&quot; a lot in the horizontal plane.</p><p>For a fighting game which relies on the actual sprite graphics to determine hit boxes and collision, this is of course unacceptable.</p><p>I&#39;ve tried searching for some guidance on this, but I do not exactly know what this issue is called and hence turn up with mostly unrelated results. I tried manually plowing through the old posts with not much success.</p><p>As far as I can see, I would need to offset the placement of the sprites. However, my sprites are extremely varied in shape and size, and I cringe at the thought of having to manually determine, then test where the &quot;center&quot; point of each frame is.</p><p>Another way which I have thought of, is to tell my artists to manually determine an anchor/reference point at the feet of the character, and to extend the size of the bitmap to as much width/height as is needed to keep it right at the center (down to one pixel accuracy). This seems logically correct, but would result in, I imagine, a lot of extra effort on the part of my artists which should not be theirs (hey, artists should just draw draw draw!). Secondly, it would increase the overall size of my graphic resources and indirectly, program memory.</p><p>Thus I pose this question to the community: how would you overcome this issue?</p><p>If I am missing something blatantly obvious, please point it out to me as I am new to game design and implementation in general.</p><p>Thanks for any input <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>P.S: That is not my sprite, I merely use it as an example.</p><p>EDIT: wow the pictures aligned weird, but they looked fine in the preview, fixed</p><p>PPS: Why did the draw_sprite_h_flip lose its underscores?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Saros69)</author>
		<pubDate>Fri, 19 Jan 2007 11:30:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Thus I pose this question to the community: how would you overcome this issue?
</p></div></div><p>
The only ways that I can think of are:<br />Increase the canvas size of your sprites and make the start_x actually in the centre (where the collision mostly occur), or store the collision circles/boxes in another file or in your code... I know that you mentioned both of those, but I can&#39;t think of any other ways at the moment.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Fri, 19 Jan 2007 12:15:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
As far as I can see, I would need to offset the placement of the sprites. However, my sprites are extremely varied in shape and size, and I cringe at the thought of having to manually determine, then test where the &quot;center&quot; point of each frame is.
</p></div></div><p>

Alas, you answered your own question.</p><p>Sometimes the obvious answer is the best one. Any other method you can come up with to solve this problem will take some amount of extra effort. When it comes down to it, adding in all those points is not as dramatic as it seems. The only tricky part is making sure each frame has the correct alignment point assigned to it. One way to do this is to encode the offset from the top left corner into the top left pixel. When the frame is loaded you read those offset coordinates from the pixel&#39;s bits and then set that pixel to transparent. Use two pixels even if you need more bits than one pixel can provide based on the bit-depth of the sprites.</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Fri, 19 Jan 2007 12:30:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the info.</p><p>I believe I can establish a standard with my artists with regards to determining the center point, and have them pass back to me the coordinates of pixel determining said center point.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
One way to do this is to encode the offset from the top left corner into the top left pixel. When the frame is loaded you read those offset coordinates from the pixel&#39;s bits and then set that pixel to transparent.
</p></div></div><p>

That&#39;s a very interesting idea, I did not know you could encode data into pixels <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /></p><p>As I was reading that, I was thinking of an alternative way of doing it (as I have no idea how to do what you said), namely using extra attributes (x, y) linked to each frame in the datafile. Then use get_datafile_property and use it to offset the blitting.</p><p>I&#39;ll talk to my artists and see what we can come up with, thanks again!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Saros69)</author>
		<pubDate>Fri, 19 Jan 2007 13:02:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>On a different note, I&#39;d consider doing the mirroring of sprites at load time rather than every time (or even cache on demand), you&#39;d be surprised how much graphics data you can fit nowadays <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Fri, 19 Jan 2007 14:56:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Saros69 said:</div><div class="quote"><p>
using extra attributes (x, y) linked to each frame in the datafile. Then use get_datafile_property and use it to offset the blitting.
</p></div></div><p>

That&#39;s what I use, and the datafile has way over 400 sprites. I modified the Allegro grabber to display this spot as a flickering pixel, and allow to move it manually with cursor keys (It&#39;s a change I keep re-implementing with each Allegro upgrade, since 3.9 !) The properties are then stored as XHOT and YHOT properties.<br />After loading the datafile in the game, I read these properties into an array for faster access.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Fri, 19 Jan 2007 15:00:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
<b>POTEMKIN BUSTER</b></p><p>Anyway, here&#39;s another vote for adding a sprite offset to each frame. It also solves the problem of frames being of different sizes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Fri, 19 Jan 2007 16:43:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Different sized sprites are unavoidable, I have x_offset and y_offset in my frames class.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Fri, 19 Jan 2007 20:59:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well you could extend the left side of the graphic out a bit with magic pink so the graphic is actually centered in the image <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />  But that might cause some other problems later on.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Dalrymple)</author>
		<pubDate>Sat, 20 Jan 2007 14:53:05 +0000</pubDate>
	</item>
</rss>
