<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>voyce &#187; iPhone</title>
	<atom:link href="http://www.voyce.com/index.php/tag/iphone-software-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.voyce.com</link>
	<description>Programming and debugging tidbits</description>
	<lastBuildDate>Sun, 15 Jan 2012 13:10:46 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Drawing animated shapes and text in Core Animation layers</title>
		<link>http://www.voyce.com/index.php/2011/12/03/drawing-animated-shapes-and-text-in-core-animation-layers/</link>
		<comments>http://www.voyce.com/index.php/2011/12/03/drawing-animated-shapes-and-text-in-core-animation-layers/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 12:30:01 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[CALayer]]></category>
		<category><![CDATA[Core Animation]]></category>
		<category><![CDATA[Core Graphics]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1370</guid>
		<description><![CDATA[Using paths and text to simple create composite animated graphics with Core Graphics, UIKit and Core Animation.]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1399" class="wp-caption alignleft" style="width: 148px"><a href="http://www.voyce.com/wp-content/uploads/2011/12/star_hello.png"><img src="http://www.voyce.com/wp-content/uploads/2011/12/star_hello.png" alt="Star and text" title="star_hello" width="138" height="149" class="size-full wp-image-1399" /></a><p class="wp-caption-text">Star and text</p></div>The other day I was overcome by the desire to create an animated start-burst, price-tag type graphic with iOS. Time to break out some Core Graphics and Core Animation code. On the way to getting it going, I came across some interesting gotchas, which I thought it&#8217;d be useful to talk about here.<br />
<span id="more-1370"></span></p>
<h3>Drawing the basic shape</h3>
<p>The first step is to draw the &#8217;star&#8217; shape. I decided to create a re-usable path, although at the minute I&#8217;m actually only drawing it once, so the benefits are limited. Doing this involves using the <code>CGPath...</code> versions of the Core Graphics functions, that add themselves to a mutable path objects, rather than being drawn directly on the currently active graphics context.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    CGMutablePathRef r <span style="color: #002200;">=</span> CGPathCreateMutable<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">// Always have to move to a start point</span>
    CGPathMoveToPoint<span style="color: #002200;">&#40;</span>r, <span style="color: #a61390;">NULL</span>, x1, y1<span style="color: #002200;">&#41;</span>; 
    CGPathAddLineToPoint<span style="color: #002200;">&#40;</span>r, <span style="color: #a61390;">NULL</span>, x2, y2<span style="color: #002200;">&#41;</span>;
    CGPathCloseSubpath<span style="color: #002200;">&#40;</span>r<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Once you&#8217;ve got created the path you can merrily set various stroke and fill colours and draw it over-and-over to your hearts content.</p>
<p><a href="http://www.voyce.com/wp-content/uploads/2011/12/star_algo.png"><img src="http://www.voyce.com/wp-content/uploads/2011/12/star_algo.png" alt="star_algo" title="star_algo" width="145" height="160" class="alignleft size-full wp-image-1383" /></a>The star drawing is parameterised in a few ways: we can set the inner radius (r2), outer radius (r1) and the number of points, which must be divisible by 3. We draw the points in groups of three, the first on the outer radius, then the inner, then the last on the outer. In that way the sections tesselate into a complete circle.</p>
<h3>Using it with CALayer</h3>
<p>So now we&#8217;ve got some code to draw the shape we want, we need a way to get it onto the screen, using <code>CALayer</code>s. There are 3 main ways of providing content for layers:</p>
<ul>
<li>Use a delegate that implements <code>drawInContext</code> (and don&#8217;t forget to call <code>setNeedsDisplay</code> at least once to cause it be drawn!)</li>
<li>Set the content to a <code>CGImageRef</code>. Meh&#8230; that&#8217;s going to mean it&#8217;s a bitmap, with all the associated aliasing/scaling issues.</li>
<li>Subclassing. Nah.</li>
</ul>
<p>We&#8217;ll use the first approach; we can create a simple <code>NSObject</code>-derived class that can manage the layer hierarchy (more of that later) and implement the required selector on it. In that function we can switch on the layer we&#8217;re being asked to draw, and do the appropriate handling:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawLayer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CALayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theLayer
        inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGContextRef<span style="color: #002200;">&#41;</span>context 
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>theLayer <span style="color: #002200;">==</span> _textLayer<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>theLayer <span style="color: #002200;">==</span> _starLayer<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h3>Animating</h3>
<p>We&#8217;re only going to be doing fairly basic rotation animation here, so we can use <code>CABasicAnimation</code>. We use the key-value coding support to specify the <code>transform.rotation</code> property as the target. This is an alias for rotation around the Z axis, which is pointing &#8220;out&#8221; of the screen. We rotate from 0 to 2*pi radians, repeating essentially indefinitely by specifying a large repeatCount.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    CABasicAnimation <span style="color: #002200;">*</span>animation <span style="color: #002200;">=</span> 
        <span style="color: #002200;">&#91;</span>CABasicAnimation animationWithKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;transform.rotation&quot;</span><span style="color: #002200;">&#93;</span>;
    animation.duration<span style="color: #002200;">=</span><span style="color: #2400d9;">8.0</span>;
    animation.repeatCount<span style="color: #002200;">=</span>HUGE_VALF;
    animation.autoreverses<span style="color: #002200;">=</span><span style="color: #a61390;">NO</span>;
    animation.fromValue<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span><span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#93;</span>;
    animation.toValue<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>TWOPI<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_starLayer addAnimation<span style="color: #002200;">:</span>animation forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotation&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h3>Drawing the text</h3>
<p>This was an interesting one. I originally started looking at a <code>CATextLayer</code>-based solution, but was surprised to find that it&#8217;s quite difficult to get vertical alignment within a rectangle. Instead, I decided to use the <code>NSString</code> UIKit additions that provide enough drawing and &#8211; importantly &#8211; measuring functions for us to work out exactly where we need to place the text.    </p>
<p>One important thing to note here is that we&#8217;re potentially mixing Core Graphics and UIKit functions here. They have different expectations about how to get hold of the required graphics context; with Core Graphics it&#8217;s always passed explicitly, whereas UIKit will grab the current context. This means that if you try and call <code>drawInRect</code> within your <code>drawLayer</code> function, you&#8217;ll see errors like &#8220;Invalid context: 0&#215;0&#8243; on the console, and no output.</p>
<p>The solution is simple when you know how: tell UIKit about your explicit context, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawLayer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CALayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theLayer
        inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGContextRef<span style="color: #002200;">&#41;</span>context 
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #11740a; font-style: italic;">// Let UIKit know about this context</span>
    UIGraphicsPushContext<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">// Because this function uses it internally...</span>
    <span style="color: #002200;">&#91;</span>myString drawInRect<span style="color: #002200;">:</span>r 
                     withFont<span style="color: #002200;">:</span>font
            lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeClip 
                    alignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;
    UIGraphicsPopContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>By measuring the text before we draw, we can align it centrally vertically and fill the space horizontally, letting iOS worry about the horizontal alignment. <code>sizeWithFont</code> takes into account a bounding rectangle and our desired breaking/clipping options:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">        CGSize sz <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>s sizeWithFont<span style="color: #002200;">:</span>font 
                 constrainedToSize<span style="color: #002200;">:</span>theLayer.bounds.size 
                     lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeClip<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h3>Setting up a layer hierarchy</h3>
<p>Given that I wanted some parts of the thing to rotate, and others to be static, I needed to create multiple layers and put them together. This is very easily achieved by adding layers to the <code>subLayers</code> collection of our root layer, then we return the root layer and add that to the view.</p>
<p><div id="attachment_1391" class="wp-caption alignright" style="width: 170px"><a href="http://www.voyce.com/wp-content/uploads/2011/12/star_layers1.png"><img src="http://www.voyce.com/wp-content/uploads/2011/12/star_layers1.png" alt="Layer arrangement" title="star_layers" width="160" height="194" class="size-full wp-image-1391" /></a><p class="wp-caption-text">Layer arrangement</p></div>The layer set-up looks like this, with the root being empty, and having first the star layer, then the text layer added to the sublayers. This is just because <code>addSublayer</code> appends the sublayer, instead we could use the <code>insertSublayer</code> overloads to be explicit about the ordering we desire.<br />
<br clear="all"/><br />
The set-up function returns the root layer, and then we add that to our view:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    _star <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StarLayer alloc<span style="color: #002200;">&#93;</span> initWithRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self.view.layer<span style="color: #002200;">&#93;</span> addSublayer<span style="color: #002200;">:</span>_star.root<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h3>Next Steps</h3>
<p>Here&#8217;s a red star and random text sitting a bit incongruously in a prototype of a spelling app I&#8217;m writing. </p>
<p><iframe width="200" height="165" src="http://www.youtube.com/embed/kAgYThdoy_c?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>It would be good to create a whole load of stars (probably not with text in) and shoot them across the screen in a star-burst by generating a random direction/speed vector and animating their speeds, opacity and scale to make them fade out.</p>
<p>You can check out the code <a href="https://github.com/voyce/StarLayer">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2011/12/03/drawing-animated-shapes-and-text-in-core-animation-layers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a physics-based OpenGL iOS app</title>
		<link>http://www.voyce.com/index.php/2011/11/18/creating-a-physics-based-opengl-ios-app/</link>
		<comments>http://www.voyce.com/index.php/2011/11/18/creating-a-physics-based-opengl-ios-app/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 22:40:50 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Bullet]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[physics]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1013</guid>
		<description><![CDATA[If you know a bit of OpenGL you can integrate the powerful Bullet physics API into your iPad or iPhone app.]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.voyce.com/wp-content/uploads/2010/09/puppet_grab.png" alt="puppet_grab" title="puppet_grab" width="128" height="128" class="alignleft size-full wp-image-1030" />With the success of iOS games like Angry Birds and its flocks of imitators, there are lots of people looking at creating physics-based games, so I decided to try and create a simple demo using OpenGL ES and the Bullet physics engine.<br />
<span id="more-1013"></span></p>
<h2>OpenGL</h2>
<p>The 3D work I&#8217;d done previously on iOS was using OpenGL ES 1.1, so as part of doing this, I also needed to get up to speed with the changes in version 2.0. The move involves replacing the fixed function pipeline approach with the use of shaders crafted in &#8220;shader language&#8221;, specifically <a href="http://www.opengl.org/documentation/glsl/">GLSL</a>. This approach is more flexible, but it means you take full responsibility for combining all the relevant matrices (model, projection etc), rather than letting OpenGL do it for you.</p>
<p>There are 2 distinct types of shader programs that operate on either vertices (vertex shaders) or pixels (fragment shaders). The steps required to create shader programs mirror the process of creating &#8220;normal&#8221; programs: they&#8217;re loaded, compiled and linked. Once you&#8217;ve got a program loaded, you can initialise it by setting variables of various kinds: attribute, uniform or varying. These are used for vertex and colour data, constants and passing data between vertex and fragment shaders respectively. If you&#8217;re interested in learning more, I suggest you check out some of the plethora of internet and dead-tree material on OpenGL. I&#8217;ll only be doing pretty basic stuff here.   </p>
<h2>Bullet</h2>
<p>I decided to try using <a href="http://bulletphysics.org/wordpress/">Bullet</a> as the physics engine. It looks very powerful and is widely used in various games and <a href="http://bulletphysics.org/wordpress/?p=241">movies</a>; the only bad thing I can say about it is that decent documentation is a bit lacking. I found the best way to get help was googling for API names on the Bullet forum; not a very efficient way of doing things. Maybe I&#8217;ve just been spoilt by the copious Apple and MS docs (yeah, yeah). </p>
<h3>Building it</h3>
<p>Luckily Bullet comes with an Xcode project &#8220;in the box&#8221;, so with a bit of <code>autoconf</code> magic, it was soon up and building the demo apps. Although there are loads of files in there, the core library itself isn&#8217;t too large or complex. As such, I decided to create a separate static library project targeting the iOS simulator (i386) and devices (armv6/7).</p>
<h3>API basics</h3>
<p>My intention is to use only a small subset of the Bullet API, encompassing rigid bodies and a couple of simple constraint types. For the purposes of the demo, I&#8217;m going to create the pretty standard set-up of a large static ground-box, along with a collection of more dynamic objects.  </p>
<p>The key part of joining up Bullet with your existing API is the <a href="http://www.bulletphysics.com/Bullet/BulletFull/classbtMotionState.html">btMotionState</a> class. It enables the API to callback into your code with updated positions at each &#8216;tick&#8217; of the physics engine, and you can then alter your geometry appropriately. I created a simple class that derives from <code>btMotionState</code>, implements the required pure virtual methods and contains a reference to my world object. Bullet calls the subclassed methods and we pass the transformations on to the object.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> QuadMotionState <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> btMotionState <span style="color: #008000;">&#123;</span>
    Quad <span style="color: #000040;">*</span>_object<span style="color: #008080;">;</span>
    btTransform _transform<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    QuadMotionState<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> btTransform <span style="color: #000040;">&amp;</span>initialpos, Quad <span style="color: #000040;">*</span>node<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        _object <span style="color: #000080;">=</span> node<span style="color: #008080;">;</span>
        _transform <span style="color: #000080;">=</span> initialpos<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> ~QuadMotionState<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">void</span> setNode<span style="color: #008000;">&#40;</span>Quad <span style="color: #000040;">*</span>node<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        _object <span style="color: #000080;">=</span> node<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> getWorldTransform<span style="color: #008000;">&#40;</span>btTransform <span style="color: #000040;">&amp;</span>worldTrans<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span>
        worldTrans <span style="color: #000080;">=</span> _transform<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> setWorldTransform<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> btTransform <span style="color: #000040;">&amp;</span>worldTrans<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #666666;">// Pass the worldTrans to _object</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<h3>Debugging</h3>
<p>Now, obviously <em>you</em> will write the perfect code first time, and will have no need for any kind of diagnostics. But I am not that perfect; I need to see the difference between what I&#8217;ve told the API to do, and what it&#8217;s actually doing. You can do this by creating an object implementing <code>btIDebugDraw</code> and passing it to the (slightly oddly named) setDebugDrawer function on your world:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">btIDebugDraw <span style="color: #000040;">*</span>debug <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> DebugDrawHelper<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
m_dynamicsWorld<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setDebugDrawer<span style="color: #008000;">&#40;</span>debug<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The most important function on <code>btIDebugDraw</code> is <code>drawLine</code>. It will be called repeatedly to draw axes and bounding boxes in a variety of colours to indicate the object&#8217;s state. We can use the <code>Shader</code> we created earlier to give us an easy way of generating these debug visuals. All we need is a very simple shader, so can declare it inline:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">program.<span style="color: #007788;">loadShaders</span><span style="color: #008000;">&#40;</span>
		<span style="color: #FF0000;">&quot;attribute vec4 position;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;uniform mat4 projection;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;void main () {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;gl_Position = position * projection;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, 
&nbsp;
		<span style="color: #FF0000;">&quot;uniform highp vec3 color;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;void main () {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;gl_FragColor = vec4(color.r, color.g, color.b, 0.0);<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Of course, anything more complex than this and you&#8217;ll want to put it into a separate file, but this program allows us to pass in the start and end vertex and its colour to draw the line in our implementation of <code>DrawLine</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> drawLine<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> btVector3<span style="color: #000040;">&amp;</span> from,<span style="color: #0000ff;">const</span> btVector3<span style="color: #000040;">&amp;</span> to,<span style="color: #0000ff;">const</span> btVector3<span style="color: #000040;">&amp;</span> color<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        glUseProgram<span style="color: #008000;">&#40;</span>program.<span style="color: #007788;">getProgram</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Set the projection matrix</span>
        GLint pu <span style="color: #000080;">=</span> glGetUniformLocation<span style="color: #008000;">&#40;</span>program.<span style="color: #007788;">getProgram</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;projection&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glUniformMatrix4fv<span style="color: #008000;">&#40;</span>pu, <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">0</span>, _projection.<span style="color: #007788;">Pointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Set the colour of the line</span>
        GLint puc <span style="color: #000080;">=</span> glGetUniformLocation<span style="color: #008000;">&#40;</span>program.<span style="color: #007788;">getProgram</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;color&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        vec3 v <span style="color: #000080;">=</span> vec3<span style="color: #008000;">&#40;</span>color.<span style="color: #007788;">getX</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, color.<span style="color: #007788;">getY</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, color.<span style="color: #007788;">getZ</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glUniform3fv<span style="color: #008000;">&#40;</span>puc, <span style="color: #0000dd;">1</span>, v.<span style="color: #007788;">Pointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Set the line vertices</span>
        <span style="color: #0000ff;">float</span> tmp<span style="color: #008000;">&#91;</span> <span style="color: #0000dd;">6</span> <span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> from.<span style="color: #007788;">getX</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, from.<span style="color: #007788;">getY</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, from.<span style="color: #007788;">getZ</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
            to.<span style="color: #007788;">getX</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, to.<span style="color: #007788;">getY</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, to.<span style="color: #007788;">getZ</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
        glVertexAttribPointer<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">3</span>, GL_FLOAT, <span style="color: #0000ff;">false</span>, <span style="color: #0000dd;">0</span>, <span style="color: #000040;">&amp;</span>tmp<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glEnableVertexAttribArray<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glDrawArrays<span style="color: #008000;">&#40;</span> GL_LINES, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><div id="attachment_1327" class="wp-caption alignright" style="width: 236px"><a href="http://www.voyce.com/wp-content/uploads/2010/09/physics_debug_screenshot.png"><img src="http://www.voyce.com/wp-content/uploads/2010/09/physics_debug_screenshot-226x300.png" alt="Bullet debug output" title="physics_debug_screenshot" width="226" height="300" class="size-medium wp-image-1327" /></a><p class="wp-caption-text">Bullet debug output</p></div> You&#8217;ll end up with a display showing your quads, along with their orientation and state (whether they&#8217;re &#8220;active&#8221; in the simulation). It looks something like this:<br />
<br clear="all"/></p>
<h3>Populating the world</h3>
<p>So in order to have something nice to look at, we&#8217;re gonna need to put some &#8216;things&#8217; into our world. And in order to make it act like the real world we&#8217;ll need to give those things some physical properties.</p>
<p>I created a simple class to render a textured box. This OpenGL object then needs to be represented in the physics worlds by a rigid body object, which in turn contains a collision detection object that closely matches the dimensions of the original object. In fact, in the case of a box, the collision detection object matches exactly, otherwise you&#8217;ll need to create an approximation using the more advanced classes. We use a <code>btBoxShape</code> to initialise our <code>btRigidBody</code> instance, along with setting its mass, inertia, and making a link to the state object that I mentioned above.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">btRigidBody <span style="color: #000040;">*</span>addShape<span style="color: #008000;">&#40;</span>Quad <span style="color: #000040;">*</span>shape, btScalar mass <span style="color: #000080;">=</span> <span style="color:#800080;">1.0f</span>, <span style="color: #0000ff;">float</span> restitution <span style="color: #000080;">=</span> <span style="color:#800080;">0.2f</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// Set the initial transform based on the current location of the object</span>
        btTransform transform<span style="color: #008080;">;</span>
	transform.<span style="color: #007788;">setIdentity</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	transform.<span style="color: #007788;">setOrigin</span><span style="color: #008000;">&#40;</span>btVector3<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color:#800080;">0.</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	btCollisionShape<span style="color: #000040;">*</span> cshape <span style="color: #000080;">=</span> 
            <span style="color: #0000dd;">new</span> btBoxShape<span style="color: #008000;">&#40;</span>btVector3<span style="color: #008000;">&#40;</span>btScalar<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getWidth<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>,
                                     btScalar<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getHeight<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>,
                                     btScalar<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getDepth<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        btVector3 localInertia<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>,<span style="color: #0000dd;">0</span>,<span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mass <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span>
            cshape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>calculateLocalInertia<span style="color: #008000;">&#40;</span>mass,localInertia<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        QuadMotionState <span style="color: #000040;">*</span>state <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QuadMotionState<span style="color: #008000;">&#40;</span>transform, shape<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	btRigidBody<span style="color: #008080;">::</span><span style="color: #007788;">btRigidBodyConstructionInfo</span> rbInfo<span style="color: #008000;">&#40;</span>mass, state, cshape, localInertia<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	btRigidBody<span style="color: #000040;">*</span> body <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> btRigidBody<span style="color: #008000;">&#40;</span>rbInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	body<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setDamping<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.85</span>, <span style="color:#800080;">0.85</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	body<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setRestitution<span style="color: #008000;">&#40;</span>restitution<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Add the rigid body to the Bullet physics world</span>
	m_dynamicsWorld<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addRigidBody<span style="color: #008000;">&#40;</span>body<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Establish the link between our object and the physics object</span>
	shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setPhysicsObject<span style="color: #008000;">&#40;</span>body<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> body<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>To make things look a bit more polished I added some textures to the quads, along with very simple normal mapping to get some lighting effects. I also added the ability to rotate the view and zoom in and out by using <code>UIGestureRecognizer</code>s and changing the camera transform appropriately. The &#8220;finishing&#8221; touch (yeah, right), is the ability to tap to add a new quad.</p>
<h3>The results</h3>
<p>Here&#8217;s how the final thing looks. It&#8217;s very basic, I&#8217;ve hardly touched the surface of the Bullet API &#8211; there are no constraints in play, for instance &#8211; but it&#8217;s still kind of fun to play with. It&#8217;s running fairly slowly here, what with it being in the simulator, and being recorded. On a physical iPad it&#8217;s much snappier.<object width="420" height="315"><param name="movie" value="http://www.youtube.com/v/vbQ_EC_YlFY?version=3&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/vbQ_EC_YlFY?version=3&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
Let me know if you&#8217;d be interested to see some more coverage of some of Bullet&#8217;s constraints and hinges features. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2011/11/18/creating-a-physics-based-opengl-ios-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sneaky peek: Physics on iOS</title>
		<link>http://www.voyce.com/index.php/2010/10/15/sneaky-peek-physics-on-ios/</link>
		<comments>http://www.voyce.com/index.php/2010/10/15/sneaky-peek-physics-on-ios/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 08:00:19 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[physics]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1057</guid>
		<description><![CDATA[A quick look at the physics demo I've been writing using Bullet on iOS.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been experimenting with the <a href="http://bulletphysics.org/wordpress/">Bullet</a> physics library on iOS. It&#8217;s a great way of adding 3d collision detection and realistic looking movement to your OpenGL apps and games. I&#8217;m working on a full blog post with all the details, but in the meantime, here&#8217;s a quick look at what I&#8217;ve been doing: a simple 2.5d ragdoll character running on the iPad simulator.<br />
<span id="more-1057"></span><br />
<iframe title="YouTube video player" class="youtube-player" type="text/html" width="560" height="345" src="http://www.youtube.com/embed/yWD6b_SvPzQ?rel=0" frameborder="0"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2010/10/15/sneaky-peek-physics-on-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating an iPad flip-clock with Core Animation</title>
		<link>http://www.voyce.com/index.php/2010/04/10/creating-an-ipad-flip-clock-with-core-animation/</link>
		<comments>http://www.voyce.com/index.php/2010/04/10/creating-an-ipad-flip-clock-with-core-animation/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 17:48:04 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[iPad]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=791</guid>
		<description><![CDATA[Using Core Animation to create flip-card number animations on the iPad and iPhone]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.voyce.com/wp-content/uploads/2010/04/flipclock_one.png"><img src="http://www.voyce.com/wp-content/uploads/2010/04/flipclock_one.png" alt="flipclock_one" title="flipclock_one" width="69" height="127" class="alignleft size-full wp-image-793" /></a>As part of the sliding puzzle game I&#8217;m developing for the iPhone and iPad (well, I can&#8217;t survive on the profits from <a href="http://voyce.com/BattleFingers/">BattleFingers</a> forever), I looked for a way to represent a numeric score and time display in an interesting way. One of the nicer visual effects you could use for this is the &#8220;flip-card clock&#8221; style, where each number consists of a top and bottom part, and the top part flips down to reveal the next number. It&#8217;s been used in a few other places including the home screen in the HTC Diamond device, and its physical, realistic style fits well with the iPad, so I set about creating a version for the iPhone and iPad using the built-in Core Animation library. Read on for more details.<br />
<span id="more-791"></span><br />
I started by thinking about the motions that would be required for the animation. We can break it down as three elements, the top, the bottom and the flipping part. Each of these parts can be represented as a Core Animation Layer (a <code>CALayer</code>). We create them using the class method on CALayer, there&#8217;s no additional <code>retain</code> here, as the <code>_toplayer</code> property is retained.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">_toplayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CALayer layer<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h2>Splitting images</h2>
<p>Now we&#8217;ve got the layers, we need something to but into them. I didn&#8217;t want to create lots of pre-processed images for the top and bottom sections of each number, so instead I wrote a routine to split the images in code, drawing each half into an image context and grabbing a <code>UIImage</code> from it. This is then stored in an array for later use. We can get any of the elements from the array and set it as the <code>content</code> of the layer as required.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// The size of each part is half the height of the whole image</span>
CGSize size <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>img size<span style="color: #002200;">&#93;</span>.width, <span style="color: #002200;">&#91;</span>img size<span style="color: #002200;">&#93;</span>.height<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create image-based graphics context for top half</span>
UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>size<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Draw into context, bottom half is cropped off</span>
<span style="color: #002200;">&#91;</span>img drawAtPoint<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>,<span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// Grab the current contents of the context as a UIImage </span>
<span style="color: #11740a; font-style: italic;">// and add it to our array</span>
UIImage <span style="color: #002200;">*</span>top <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> retain<span style="color: #002200;">&#93;</span>;			
<span style="color: #002200;">&#91;</span>_imagesTop addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>top<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Now draw the image starting half way down, to get the bottom half</span>
<span style="color: #002200;">&#91;</span>img drawAtPoint<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>,<span style="color: #002200;">-</span><span style="color: #002200;">&#91;</span>img size<span style="color: #002200;">&#93;</span>.height<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// And store that image in the array too</span>
UIImage <span style="color: #002200;">*</span>bottom <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> retain<span style="color: #002200;">&#93;</span>;			
<span style="color: #002200;">&#91;</span>_imagesBottom addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>bottom<span style="color: #002200;">&#93;</span>;
&nbsp;
UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Now our <code>_imagesTop</code> and <code>_imagesBottom</code> arrays contains all the images we need, let&#8217;s get &#8216;em moving.</p>
<h2>Applying animation</h2>
<p><div id="attachment_808" class="wp-caption alignright" style="width: 212px"><a href="http://www.voyce.com/wp-content/uploads/2010/04/flipclock_axes1.png"><img src="http://www.voyce.com/wp-content/uploads/2010/04/flipclock_axes1-202x300.png" alt="The axes and anchor point of a CALayer" title="flipclock_axes" width="202" height="300" class="size-medium wp-image-808" /></a><p class="wp-caption-text">The axes and anchor point of a CALayer</p></div>The animation we need to apply to the &#8216;flipping&#8217; part is a rotation around the bottom of the layer, at the join between the two halves. The default location of the anchor point is the middle of the layer, which would mean the piece would rotate around the middle, but we can easily change it to instead be at the middle in the bottom (for our purposes it could be anywhere on the bottom, as we&#8217;re only interested in x-axis rotation):</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">_fliplayer.anchorPoint <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.5</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>I use the <code>CATransform3DMakeRotation</code> function to create a transform around the X axis using the vector [1,0,0]. For the first part of the motion, we go from 0 radians to pi/2 radians (0 to 90 degrees), using an explicit <code>CABasicAnimation</code> object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CABasicAnimation <span style="color: #002200;">*</span>topAnim <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CABasicAnimation animationWithKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;transform&quot;</span><span style="color: #002200;">&#93;</span>;
topAnim.duration<span style="color: #002200;">=</span><span style="color: #2400d9;">0.25</span>;
topAnim.repeatCount<span style="color: #002200;">=</span><span style="color: #2400d9;">1</span>;
topAnim.fromValue<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSValue</span> valueWithCATransform3D<span style="color: #002200;">:</span>CATransform3DMakeRotation<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">float</span> f <span style="color: #002200;">=</span> <span style="color: #002200;">-</span>M_PI<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>;
topAnim.toValue<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSValue</span> valueWithCATransform3D<span style="color: #002200;">:</span>CATransform3DMakeRotation<span style="color: #002200;">&#40;</span>f, <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
topAnim.delegate <span style="color: #002200;">=</span> self;
topAnim.removedOnCompletion <span style="color: #002200;">=</span> FALSE;
<span style="color: #002200;">&#91;</span>_fliplayer addAnimation<span style="color: #002200;">:</span>topAnim forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithUTF8String<span style="color: #002200;">:</span>k_TopAnimKey<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h2>Drawing layer content</h2>
<p>Unfortunately a CALayer doesn&#8217;t have a &#8220;back&#8221;, so creating the flip layer is not as easy as it could be. We need to add a step in the process to change the image displayed by the flip layer half way through its fall. We can do this in a variety of ways, I&#8217;ve chosen to set a delegate object to perform the drawing of the layer contents:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>_fliplayer setDelegate<span style="color: #002200;">:</span>_layerHelper<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This means our delegate object (<code>_layerHelper</code>) has its <code>drawLayer</code> method called whenever the layer needs to display its contents. Be wary though, this may not be as often as you think! Core graphics aggressively caches the contents of the layer, and will only call your delegate when absolutely necessary. You can force it to happen by calling <code>setNeedsDisplay</code>, which invalidates the contents of the layer:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>_fliplayer setNeedsDisplay<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>I encapsulated this in the helper object itself, by providing a custom implementation of the <code>isTop</code> property setter that calls <code>setNeedsDisplay</code>. At least that way callers don&#8217;t have to be aware of this requirement.</p>
<p>The <code>drawLayer</code> method itself simply draws one of two images into the context, depending on whether it&#8217;s in &#8216;top&#8217; mode. A slight further complication is that when drawing the bottom part, the image needs to be inverted to display upside down. This is achieved by applying a translation and scale to the context before drawing the image:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGContextSaveGState<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;
CGContextTranslateCTM<span style="color: #002200;">&#40;</span>context, 0.0f, r.size.height<span style="color: #002200;">&#41;</span>;
CGContextScaleCTM<span style="color: #002200;">&#40;</span>context, 1.0f, <span style="color: #002200;">-</span>1.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
CGContextDrawImage<span style="color: #002200;">&#40;</span>context, r, <span style="color: #002200;">&#91;</span>_imgTop CGImage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
CGContextRestoreGState<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h2>Avoiding implicit animation</h2>
<p>I&#8217;ve tried to avoid using implicit animation here, instead using explicit construction of <code>CABasicAnimation</code> objects, because we need some control of when things happen, and the order in which they happen. Normally, it would be easier to just set some property of the view and let the OS manage animating it. Many properties have default transition that are applied whenever they&#8217;re changed, so it takes some additional effort to make them change immediately.</p>
<p>In our example we change the contents of the top half and show the flip layer, but we need that to happen immediately so we disable actions within an explicit transaction that we then commit. This gives us the control we need, and avoids the default behaviour of the transition occurring when the message loop next runs.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>CATransaction begin<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>CATransaction setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kCFBooleanTrue
				forKey<span style="color: #002200;">:</span>kCATransactionDisableActions<span style="color: #002200;">&#93;</span>;
_toplayer.contents <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_imagesTop objectAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self getNextIndex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> CGImage<span style="color: #002200;">&#93;</span>;
_fliplayer.hidden <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#91;</span>CATransaction commit<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h2>Getting some perspective</h2>
<p>Although the images I&#8217;ve used to illustrate this post are isometric, what we really want is a front-on perspective view, so that the falling piece seems to stick out. For this we have to set a <code>sublayerTransform</code> on the view&#8217;s inherent layer, by setting an individual element on a transform:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CATransform3D aTransform <span style="color: #002200;">=</span> CATransform3DIdentity;
<span style="color: #a61390;">float</span> zDistance <span style="color: #002200;">=</span> <span style="color: #2400d9;">1000</span>;
aTransform.m34 <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span> <span style="color: #002200;">/</span> <span style="color: #002200;">-</span>zDistance;	
<span style="color: #002200;">&#91;</span>self layer<span style="color: #002200;">&#93;</span>.sublayerTransform <span style="color: #002200;">=</span> aTransform;</pre></div></div>

<p>This gives us a much more realistic look.</p>
<h2>Putting it together</h2>
<p>Wow, so now we&#8217;ve got some appropriately sliced images, some layers and some animations. Let&#8217;s put them together to get the final effect.<br />
<div id="attachment_812" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.voyce.com/wp-content/uploads/2010/04/flipclock.png"><img src="http://www.voyce.com/wp-content/uploads/2010/04/flipclock-300x96.png" alt="The animation states (last is same as first)" title="flipclock" width="300" height="96" class="size-medium wp-image-812" /></a><p class="wp-caption-text">The animation states (last is same as first)</p></div><br />
I&#8217;ve used a very simple state machine that moves through 3 states; TopToMiddle, MiddleToBottom, ResetForNext. We can use the <code>animationDidStop</code> delegate as the state transition point. We don&#8217;t have to bother determining exactly which animation finished, because we do the same thing (change state) regardless.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>animationDidStop<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CAAnimation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldAnimation finished<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>flag
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self changeState<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<ul>
<li><strong>TopToMiddle</strong>: show flip layer, and start rotation through 90 degrees</li>
<li><strong>MiddleToBottom</strong>: change image on flip layer, start rotation through further 90 degrees</li>
<li><strong>ResetForNext</strong>: change displayed top and bottom layer contents and hide flip layer</li>
</ul>
<p>Let&#8217;s see how it looks in action:<br />
<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/RbPmAl_AeSU&#038;hl=en_US&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/RbPmAl_AeSU&#038;hl=en_US&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2010/04/10/creating-an-ipad-flip-clock-with-core-animation/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>iPad &#8211; The rise of the naturalistic user interface</title>
		<link>http://www.voyce.com/index.php/2010/03/11/ipad-the-rise-of-the-naturalistic-user-interface/</link>
		<comments>http://www.voyce.com/index.php/2010/03/11/ipad-the-rise-of-the-naturalistic-user-interface/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 01:05:53 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[iPad]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=679</guid>
		<description><![CDATA[Apple's iPad applications make use of real-world objects in their user interfaces: books, newspapers, address books. What is it about the iPad that makes it a good platform for this kind of UI?]]></description>
			<content:encoded><![CDATA[<p>It was fascinating to see Apple unveiling its new iPad hardware recently, and one of the things that caught my eye were the interfaces of the various apps that were demonstrated. They look different from apps on other platforms, and even from the equivalent apps on the iPhone. It seems to me as if there&#8217;s been a change to a more naturalistic style of user interfaces. Why is this, and what is it about the iPad that makes it suited to this kind of UI?<br />
<span id="more-679"></span></p>
<h2>Form factor</h2>
<p>The most obvious reason that this kind of interface works on the iPad is the form factor of the device itself. Think about it: an iPhone is a small (well, smallish) object which has no physical, real-world equivalent. It&#8217;s not supposed to &#8220;be&#8221; anything other than a phone. The iPad, on the other hand, is intended to mimic an actual object, or rather, a variety of objects: a book, a newspaper or an address book for example. The interface for each of these uses reflects this directly: the address book looks like a &#8220;little black book&#8221;, complete with tabs. </p>
<p>The iPhone uses some of the same metaphors &#8211; like the address book for contacts &#8211; but the representation is abstract, whereas the iPad UI is literal, using an actual image of a book. The abstraction is necessary on the iPhone in order for it to remain usable with a smaller screen. Compare the iPad and iPhone Contacts interfaces:</p>
<table>
<tr>
<td><div id="attachment_682" class="wp-caption alignnone" style="width: 310px"><a href="http://www.voyce.com/wp-content/uploads/2010/02/contacts_ipad_fullpage_small.png"><img src="http://www.voyce.com/wp-content/uploads/2010/02/contacts_ipad_fullpage_small-300x230.png" alt="The realistic looking address book application on the iPad" title="contacts_ipad_fullpage_small" width="300" height="230" class="size-medium wp-image-682" /></a><p class="wp-caption-text">The realistic looking address book application on the iPad</p></div>
</td>
<td><div id="attachment_684" class="wp-caption alignnone" style="width: 130px"><a href="http://www.voyce.com/wp-content/uploads/2010/02/contacts_iphone_small.png"><img src="http://www.voyce.com/wp-content/uploads/2010/02/contacts_iphone_small-156x300.png" alt="iPhone contacts interface" title="contacts_iphone_small" width="120" height="230" class="size-medium wp-image-684" /></a><p class="wp-caption-text">iPhone contacts interface</p></div></td>
</tr>
</table>
<p>The abstract to realistic continuum has been explored extensively in the world of icons (explained brilliantly <a href="http://ignorethecode.net/blog/2010/01/21/realism_in_ui_design/">here</a>), but less so in the interface as a whole. Obviously some apps don&#8217;t have any form of real world equivalent (think System Preferences.app), so they&#8217;re likely to stay in a more abstract form. </p>
<h2>Display fidelity</h2>
<p>Of course, it&#8217;s always been possible to <i>represent</i> real objects on the screen, but without a display capable of a decent resolution it probably won&#8217;t look real. So another enabler for naturalistic UI is display technology: the more pixels and colour depth the better when it comes to rendering physical things. Realism often relies on subtle lighting and rich textures to achieve the desired effect. Anyone who&#8217;s ever attempted to render wood grain using 8-bit colour will appreciate how much easier it is with a 32-bit 1024&#215;768 display. </p>
<p>The need for display quality goes hand-in-hand with a need for more processing power to push the pixels. If the processing &#8220;oomph&#8221; required to render your interface with sufficient quality means that it&#8217;s not responsive enough, then you&#8217;ve failed by any measure. The iPad seems to have reached a sweet spot where display capabilities, raw power and form-factor meet to make naturalistic UIs a feasible approach.   </p>
<h2>Direct manipulation</h2>
<p><a href="http://www.voyce.com/wp-content/uploads/2010/03/Apple_Mouse_no.png"><img src="http://www.voyce.com/wp-content/uploads/2010/03/Apple_Mouse_no.png" alt="Apple_Mouse_no" title="Apple_Mouse_no" width="160" height="160" class="alignleft size-full wp-image-722" /></a>Up until the relatively recent introduction of direct manipulation technologies like touch-senstive displays, there&#8217;s a been a forced physical separation between users and their computer in the form of a mouse and keyboard. A drag operation using a mouse involves disconnected, abstract operations like pointing and clicking, which bear no relation to the object being acted upon. As such, there&#8217;s little use in having an extremely realistic representation of something to interact with, because the suspension-of-disbelief has already been broken. </p>
<p>But in the world of touch-based devices, our interactions are much closer to the physical actions we&#8217;d make, so having a closer visual representation to go along with it is beneficial. For instance, turning a page in an eBook and actually seeing the page flick over seems consistent when using a natural gesture like flicking a finger across the screen. Otherwise if you had to click a corner of the page and drag it across in a poor imitation of the physical action the visual aspects can quickly start to feel superfluous.   </p>
<p>Perhaps given the iPad&#8217;s larger interaction surface we can expect to see a wider and more expressive range of gestures supported?  </p>
<h2>History</h2>
<p>Of course, naturalistic UI isn&#8217;t really a new thing. It&#8217;s in use in quite a few applications already, albeit mostly in desktop apps, where capable hardware is more readily available. In fact, there was much a-twittering (not <i>all</i> of it from Will Shipley) about the similarity between the demo&#8217;d iPad apps and the interface of the fantastic Mac application <a href="http://delicious-monster.com/">Delicious Library</a>. I&#8217;m sure of course &#8211; how do they say it in the movie credits &#8211; the similarity is entirely coincidental&#8230;</p>
<table>
<tr>
<td valign="top"><div id="attachment_733" class="wp-caption alignnone" style="width: 303px"><img src="http://www.voyce.com/wp-content/uploads/2010/03/delicious_library_shrunk.png" alt="Delicious Library UI" title="delicious_library_shrunk" width="293" height="196" class="size-full wp-image-733" /><p class="wp-caption-text">Delicious Library UI</p></div></td>
<td><div id="attachment_734" class="wp-caption alignnone" style="width: 160px"><img src="http://www.voyce.com/wp-content/uploads/2010/03/ibooks_shrunk.png" alt="iBooks UI" title="ibooks_shrunk" width="150" height="199" class="size-full wp-image-734" /><p class="wp-caption-text">iBooks UI</p></div></td>
</tr>
</table>
<p><a href="http://www.voyce.com/wp-content/uploads/2010/03/rebirth_knobs_crop.png"><img src="http://www.voyce.com/wp-content/uploads/2010/03/rebirth_knobs_crop.png" alt="rebirth_knobs_crop" title="rebirth_knobs_crop" width="250" height="110" class="alignleft size-full wp-image-717" /></a>One of the first widespread uses of naturalistic UI elements was in software synths. Guys like <a href="http://www.propellerheads.se/">Propellerheads</a> and <a href="http://www.native-instruments.com/">Native Instruments</a> set about creating exact software duplicates of well-known and well-loved bits of <a href="http://en.wikipedia.org/wiki/Roland_TB-303">mid-80&#8217;s audio hardware</a>. The UIs included virtual versions of a huge number of physical elements that had never been used before: including things like sliders, LEDs, toggle buttons, rotary knobs and rocker switches. These interface elements were probably first seen in software-based mixing desks, but the techniques used to make them feel real have gone on to be used in many different ways. Even the lowly push button in both Windows and MacOS got an upgrade by application of some of the 3D effects that they originated. </p>
<h2>Where Now?</h2>
<p>It will be interesting to see what other types of app are given the iPad realism makeover. It certainly looks like all of the standard Apple apps are being changed, including Notes, iCal, iBooks and Contacts, which were shown in the launch presentation, but how will 3rd-party developers manage? It undoubtedly takes extra effort to make something look convincing when it can be compared to the physical object itself. </p>
<p>Apple has previously managed to get a good level of consistency in its abstract UIs by providing the various interface elements ready-made, but will that approach scale to this kind of UI? No doubt the developers who take great pride in the interfaces now will continue to strive to create apps that sit comfortably alongside Apple&#8217;s own, but perhaps there&#8217;ll just be a more obvious visual distinction between those guys and the less polished application user interfaces than there has been in the past.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2010/03/11/ipad-the-rise-of-the-naturalistic-user-interface/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Why the Peggle mobile experience beats GTA</title>
		<link>http://www.voyce.com/index.php/2010/01/26/why-the-peggle-mobile-experience-beats-gta/</link>
		<comments>http://www.voyce.com/index.php/2010/01/26/why-the-peggle-mobile-experience-beats-gta/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 00:27:21 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Gaming]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[games]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=637</guid>
		<description><![CDATA[Peggle and Grand Theft Auto: Chinatown Wars. Two fantastic and very different iPhone games, but how is your enjoyment of them affected by the platform on which they run?]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_643" class="wp-caption alignleft" style="width: 74px"><img src="http://www.voyce.com/wp-content/uploads/2010/01/peggle_icon.png" alt="Peggle icon" title="peggle_icon" width="64" height="64" class="size-full wp-image-643" align="left" /><img src="http://www.voyce.com/wp-content/uploads/2010/01/gta_icon.png" alt="GTA icon" title="gta_icon" width="64" height="64" class="size-full wp-image-644" /><p class="wp-caption-text">Pegs vs Triads</p></div>I&#8217;ve had two very different iPhone gaming experiences over the last few weeks: <a href="http://itunes.apple.com/gb/app/peggle/id314303518?mt=8">Peggle</a> and <a href="http://itunes.apple.com/gb/app/grand-theft-auto-chinatown-wars/id344186162?mt=8">Grand Theft Auto: Chinatown Wars</a>. It&#8217;s safe to say I got completely addicted to Peggle, but when it arrived I couldn&#8217;t resist the temptation of having the GTA universe in my pocket too. After shelling out the almighty sum of £5.99 on the sandbox triad-&#8217;em-up, I discovered that there were many aspects of playing/using Peggle that make it a better fit on the iPhone than GTA. Let&#8217;s break down how these very different experiences manage the transition to a hand-held, &#8220;casual&#8221; gaming platform. What exactly does &#8220;usability&#8221; mean in this context?<br />
<span id="more-637"></span></p>
<h3>1. Minimise cost of entry/exit &#8211; Instant on, instant off</h3>
<p>If there&#8217;s one thing that most characterises the mobile gaming experience, it&#8217;s the difference in playing cycle frequency and duration. As this highly unscientific graph shows:<br />
<div id="attachment_664" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.voyce.com/wp-content/uploads/2010/01/gaming_graph1.png"><img src="http://www.voyce.com/wp-content/uploads/2010/01/gaming_graph1-300x149.png" alt="Gaming intensity over an 18 hour period" title="gaming_graph" width="300" height="149" class="size-medium wp-image-664" /></a><p class="wp-caption-text">Gaming intensity over an 18 hour period</p></div>Players of a console game are much more likely to be giving the game their full attention, for potentially extended time periods. As such we expect them to give back experientially; rousing scores, scene-setting cinematics. How well do GTA&#8217;s movie stylings transfer to the 3.5&#8243; screen? Fairly well, as it turns out.</p>
<p>Handheld games need to be able to be picked-up and played, and similarly, put-down, in a flash. It&#8217;s just not feasible to insist on finishing a mission, returning to a safe-house etc, before the game can be safely stopped. You don&#8217;t want to lose the last few minutes progress just because you&#8217;ve arrived at your destination. Peggle has a few wrinkles here, the only times it crashed on me was after unlocking the phone to continue a game, but generally it&#8217;s perfectly paced for snatched play; each ball lasts a few seconds, and each game only a few minutes. Furthermore when resuming a game you can instantly see the number of blue and orange pegs remaining and therefore gauge game progress at a glance. Having a free-roaming city to explore is a fantastic experience, but keeping track of where you are requires more mental effort/investment.</p>
<h3>2. Support restricted input &#8211; One-handed operation</h3>
<p><img src="http://www.voyce.com/wp-content/uploads/2010/01/peggle_screenshot-300x200.png" alt="peggle_screenshot" title="peggle_screenshot" width="300" height="200" class="alignleft size-medium wp-image-655" />Personally, I get a few good hours a week of gaming time on the tube. Unfortunately most of that is while hanging from the hand-rails amongst a crowd of other commuters. This one-handed playing style is completely incompatible with GTA&#8217;s control model; the driving requires one hand for the accelerator, brake etc, and another for the directional controls. The overall pacing of the game is also important; twitch gaming doesn&#8217;t fit well with being buffeted around on the underground, but board-game style pacing is largely unaffected.  </p>
<h3>3. Degrade output gracefully &#8211; Shhhhhh!</h3>
<p>GTA is most definitely a multi-sensory experience. The radio soundtrack that kicks in as you jump into a &#8216;jacked motor. The ambient sounds of the city as you stalk the sidewalks looking for unsuspecting victims to eviscerate. Peggle has its own rousing soundtrack of course, Ode to orange peg Joy, but it&#8217;s much more of an ancillary aspect of the game. You can safely turn it off and it&#8217;s not going to reduce your ability to play, or alter the play experience. The importance of this becomes apparent whilst enjoying a surreptitious few minutes of fun in, say, just for example, err, the toilets at work. Not that I would <i>ever</i> do that, obviously&#8230; </p>
<h3>4. Work with device restrictions &#8211; Physicality</h3>
<p><img src="http://www.voyce.com/wp-content/uploads/2010/01/gta_screenshot-300x200.png" alt="gta_screenshot" title="gta_screenshot" width="300" height="200" class="alignright size-medium wp-image-654" />One of the things I found most jarring about the translation of the GTA experience to the iPhone is the very thing that makes the iPhone what it is: the touch screen. It&#8217;s just odd not having the haptic feedback on a stick, especially when you&#8217;re virtually pushing a car round a tight corner, or trying to chase someone down the street. I just felt like I needed to feel the limits of the controls. Part of the reason for this is the fact that you&#8217;re essentially controlling physical objects directly, whereas Peggle is much more abstract &#8211; albeit fundamentally based on the concrete physical behaviour of a falling ball. </p>
<h4>&#8230;but which is best?</h4>
<p>The answer? Who knows! The fact is they&#8217;re completely different experiences, so it depends what you&#8217;re looking for. The thing about smartphones, portable gaming devices etc, is that people use them in a vast array of different environments and in very different ways. Personally I don&#8217;t want a console replacement, but I do enjoy console-like experiences on the go every once in a while. I&#8217;m willing to change the way I use the phone &#8211; at least temporarily &#8211; to accommodate that. But I also love a game that fits perfectly with the way I already use my device; especially if it happens to be incredibly addictive, with simple but emergent game-play. These days I&#8217;m a real casual gamer, having to grab what time I can between work and the family, so having something that I can enjoy in 30 second segments on my own terms is a real pleasure. </p>
<p>It will be interesting to see how developers, gaming and otherwise, deal with the different usability requirements of these emerging platforms. These factors could quickly become important differentiators in a crowded market.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2010/01/26/why-the-peggle-mobile-experience-beats-gta/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BattleFingers is here!</title>
		<link>http://www.voyce.com/index.php/2009/02/03/battlefingers-is-here/</link>
		<comments>http://www.voyce.com/index.php/2009/02/03/battlefingers-is-here/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 08:01:57 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Gaming]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[battlefingers]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=78</guid>
		<description><![CDATA[
Well, I&#8217;ve done it: I&#8217;ve got my first game live on the AppStore. It&#8217;s been an interesting journey. I&#8217;m terribly bad at getting my hands on devkits and SDKs, having a play with them and then not doing anything constructive. This dates way back to things like the Playstation NetYaroze, which was pretty expensive, and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.voyce.com/BattleFingers"><img title="BattleFingers" src="http://www.voyce.com/BattleFingers/bftext.png" alt="BattleFingers text" hspace="10" vspace="5" width="100" height="69" align="left" /></a></p>
<p>Well, I&#8217;ve done it: I&#8217;ve got my first game live on the AppStore. It&#8217;s been an interesting journey. I&#8217;m terribly bad at getting my hands on devkits and SDKs, having a play with them and then not doing anything constructive. This dates way back to things like the Playstation NetYaroze, which was pretty expensive, and with which I failed to produce anything concrete. So this time around all the pieces were in place: shiny new &#8220;gaming&#8221; kit, interesting SDK, low cost of entry. I was determined to create!</p>
<p>I&#8217;ll be making a series of posts on the process and details of creating it, in the interest of sharing the fun. In the meantime, you can find out more about the game <a title="BattleFingers page" href="http://www.voyce.com/BattleFingers">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2009/02/03/battlefingers-is-here/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sound formats for iPhone development</title>
		<link>http://www.voyce.com/index.php/2009/01/06/sound-formats-for-iphone-development/</link>
		<comments>http://www.voyce.com/index.php/2009/01/06/sound-formats-for-iphone-development/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 23:10:48 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[caf]]></category>
		<category><![CDATA[endian]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[libsndfile]]></category>
		<category><![CDATA[wav]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=58</guid>
		<description><![CDATA[Ahh, back to work today. It&#8217;s pretty tough getting back into the swing of things after what turned out to be a long break this year. While I was off I finally got to spend some time working on an iPhone game. After getting hold of the SDK a while back, it&#8217;s only now that [...]]]></description>
			<content:encoded><![CDATA[<p>Ahh, back to work today. It&#8217;s pretty tough getting back into the swing of things after what turned out to be a long break this year. While I was off I finally got to spend some time working on an iPhone game. After getting hold of the SDK a while back, it&#8217;s only now that I&#8217;ve gotten around to doing something with it.</p>
<p>One of the things that seemed a little odd about the SDK is it&#8217;s use of CAF-format audio files, detailed <a href="http://developer.apple.com/documentation/MusicAudio/Reference/CAFSpec/CAF_intro/chapter_1_section_1.html">here</a>. I got hold of a few very nice audio samples from the <a href="http://www.freesound.org">freesound</a> site, but needed to convert them from WAVs to CAFs.</p>
<p>I thought ffmpeg might be up to the job, but the version I had didn&#8217;t list it as an avaliable output format using <code>ffmpeg -formats</code>. However after a bit of digging I discovered that it is supported by <a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a>, so set about installing it using <a href="http://www.macports.org">MacPorts</a>:</p>
<blockquote><p><code>sudo port install libsndfile</code></p></blockquote>
<p>Then I used the included libsndfile-convert app to convert my file:</p>
<blockquote><p><code>libsndfile-convert file.wav file.caf</code></p></blockquote>
<p>The output format is inferred from the file extension, so you don&#8217;t have to specify it. However, when I rebuilt and ran my iPhone app using the new file, it didn&#8217;t play back. I suspected there may be something wrong with the format of the file, so I took a look to see what <a href="http://en.wikipedia.org/wiki/File_(Unix)">file</a> reported. For the original WAV file I got</p>
<blockquote><p><code>file.wav: RIFF <strong>(little-endian)</strong> data, WAVE audio, Microsoft PCM, 16 bit, mono 44100 Hz</code></p></blockquote>
<p>Unfortunately file doesn&#8217;t work on .CAF files, but you can open them using QuickTime Player, and using the Movie Inspector window you can see that the file has the following format:</p>
<blockquote><p><code>16-bit Integer <strong>(Big Endian)</strong>, Mono, 44.100 kHz</code></p></blockquote>
<p>So it looks like the problem may be libsndfile-convert changing the endian-ness of the file contents, from the x86-style little-endian to Motorola-ish (i.e. pre-x86 Mac) big-endian, which is a bit of a pain. According to the docs, the libsndfile API supports endian-ness manipulation, so it&#8217;s probably just the case that the helper app is doing the wrong thing automatically. I&#8217;ll look at putting together a small command line app to use the API directly and enable me to batch process .WAV files correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2009/01/06/sound-formats-for-iphone-development/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

