Augmented Reality with Cocos2D Tutorial

AR with Cocos2DThe tutorial I created for Ray Wenderlich’s website is now live! Click here to view it.

Here is a snippet of the beginning:

In this tutorial you will learn how to make a simple augmented reality game for the iPhone/iPod touch!
In this game you will utilize the camera, gyroscope, and Cocos2d framework. Sounds exciting right?
It definitely was fun exploring these technologies while writing this article. There is a bit of math and conversions, but don’t worry – it’s nothing too hard!

To use this tutorial, you need an iPhone 4 because the tutorial uses the gyroscope to move your view of the world.
You also need to have at least basic knowledge of Cocos2D and have Cocos2D installed. If you are a complete beginner to Cocos2D, you should go through some of the other Cocos2D tutorials on this site first.

Are you ready to start blasting some virtual aliens? Let’s go!

Email

5 thoughts on “Augmented Reality with Cocos2D Tutorial

  1. Monica Peters

    Hi Nick,
    Awesome tutorial, thanks for sharing :) I’m a student at Full Sail University and I’m very new to learning Objective C.
    I just learned the syntax for using Objective-C NSArray within the past two weeks, so forgive me if my question is silly.

    I have a newbie question for you about ZAttributedString.m and it’s use of NSArray and NSDictionary on lines 462 and 486.

    I downloaded your ARSpaceShips source code into my XCode.
    I click Run, it builds successfully, shows the screen with the cocos logo, then I get 3 Warnings in my XCode console:

    Debugger Output: (lldb)

    1. Target ARSpaceships
    Upgrade Compiler configuration to LLVM…
    I click button to Perform Changes. That runs fine and I’m left with 2 Warnings in my XCode project:

    ZAtributedString.m
    2. LLVM GCC 4.2 Warning
    line 462:
    Incompatible pointer types sending ‘NSArray *’ to parameter of type ‘NSDictionary *’
    Incompatible Objective-C types ‘struct NSArray *’, expected struct…

    3. LLVM GCC 4.2 Warning
    line 486:
    Incompatible pointer types sending ‘NSArray *’ to parameter of type ‘NSDictionary *’
    Incompatible Objective-C types ‘struct NSArray *’, expected struct…

    MY QUESTION:
    Why does the warning happen and what can I do to correct the situation? I will be reading more about lldb asap too.
    If you can suggest specific docs that I should read to understand AR in Objective-C, I would appreciate that too.

    Thanks Again, for making a great tutorial too. I really appreciate it :)

    Best Wishes,
    Monica

  2. nick Post author

    Hi Monica,

    The two remaining warning have to deal with files in the Cocos2D library. You can fix those errors on your own, that’s the beauty of an open source project. The problem is that the object is of the wrong type then what is expected.

  3. James

    Hi Nick
    I found your tutorial at raywenderlich.com

    Downloadable source file you kindly provide builds fine in Xcode but the game is stuck in portrait mode.

    I’m guessing this is because Cocos2d has been changed since you wrote the tutorial, and that we are now on iOS6.

    Could you give me any tips on how to change it into landscape mode? I’ve tried editing AppDelegate.m and RootViewController.m files but the game remains locked in Portrait instead of Landscape, and yes I have tried turning the phone :-)

    Thank you for your time

    J

  4. James

    Hello again Nick
    I managed to fix it by changeing this in the AppDelegate.m

    #if GAME_AUTOROTATION == kGameAutorotationUIViewController
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
    #else
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
    #endif

    to

    #if GAME_AUTOROTATION == kGameAutorotationUIViewController
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeRight];
    #else
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
    #endif

    However the when the spaceships enter the crosshairs they sometimes disappear before you have a change to shoot them…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>