Wow! It’s been a long time (2 months and 2 days) since I posted and a lot of things happened meanwhile. I can’t exactly cite all the stuff I learned – following the method from earlier diary entries – so I’ll write just some points, since I didn’t keep track of everything learned on these last 62 days. Also, I’m already on the first position of #iDevBlogADay waiting list, so the next posts will be “real” posts.
And how is the game programming going? I’m working on 3 prototypes (less talk, more rock, huh?), which I plan to announce here soon! :)
Learning Diary #11 entries?
Objective-C
iPad
- Finally got an iPad and learned what the iOs SDK brings exclusively to the iPad. Also at the moment I’m working on a nice client contract work, doing a magazine iPad App :). It’s something exciting and lovely to work with.
Flash and Flex
- This is something I’d been delaying forever too, so I’m into ActionScript, finally! Thanks to the amazing Cartoon Smart tutorials.
Box2D
- These notes are from Ray’s tutorial: How To Use Box2D For Just Collision Detection with Cocos2D iPhone: when defining Box2D custom shapes, make sure that your polygon is convex and have at max (default) 8 vertices.
- Also, when setting up the vertices: “it’s better to use the “Initialization” style and the b2PolygonShape::Set method rather than the “Assignment” style so that Box2D can automatically compute the centroid and the normals for the shape for us” (source).
Art Side
I’ve been studying art and I started drawing, “pixeling” and so on and I have a lot to say about this subject. You can wait some posts around these subjects around here now and not just programming. I have some tips on how to go from a terrible artist to start doing something reasonable and they will be published on the next article.
12/Jul/2010
Using Lua and a Socket Server to test and debug iPhone Games

Damn, too many balloons. Ops, just change the Lua code real time!
I already talked about #iDevBlogADay. But let’s say it again: I just love this “group”! The first thing I do when I wake up is checking the feed for the day’s articles. And how sad I get if an author hadn’t published his take yet.
Everyday we get 2 new nice articles from fellow indie (or established or beginners – like me) game developers. One of these articles “Tweak Away” from Mystery Coconut brings Lua and socket servers into your iPhone game development.
Main points you’ll learn from the article:
- How to open a debug server for your game.
- How to integrate Lua into your game and how it can access your game elements.
- Change your game behaviors and state via command line (e.g.: no more re-compiling to change the speed of a ball).
- Scripting all of your game content and behavior.
And yes, Miguel makes everything that easy for us :)
Path Finding
- Path finding class for a RTS game: ASIPathFinder. “ASIPathFinder is a complete implementation of a cooperative path finding algorithm, and will probably be most useful for writing Real Time Strategy games. It is written in Objective-C, and is compatible with Mac OS and iPhone OS.”
- Cocos2D, Path finding and Tile maps – sample app: cool working code which I found on Cocos2D forums.
Cocos2D
- RenderTexture: let’s explain via an example: for a shooter game which leaves bullet holes, we could use sprites for these holes, but a RenderTexture is memory saver.
a) Sprites: 500 holes – 500 sprites – 1000 triangles – 2000 vertexes.
b) RenderTexture: will draw only one sprite – it takes a picture and work in that picture.
- EAGLView: create the OpenGL context.
Drawing with Quartz2D
- Save current context
- Perform a single combined matrix transformation
- Draw graphics to this transformation
- Restore context
Source: Beginning iPhone Games Development
Quaternion
- Sum of a scalar and a vector
- The quotient of two vectors
- Imaginary number: i * i = -1
- A quaterion is a complex number extension: 3 numbers all square roots of -1: i, j, k
- j * j = -1, k * k = -1
- q = w + xi + yj + zk (w = real, x, y, z = complex).
- or q = [w, v] (w = scalar, v = (x,y,z) – vector)
Source: http://www.gamedev.net/reference/articles/article1095.asp, http://www.paradeofrain.com/2010/07/lessons-learned-in-tilt-controls/
05/Jul/10
A* Search algorithm
Cocos2D
- Transform anchor: coordinates used by Cocos to rotate objects on screen. For sprites, the default is the middle of the image.
- ccp(x, y) is Cocos shortcut macro to CGPointMake.
- Scene: a container of all the stuff you’re going to show on screen in a specific part of your game: Main menu, level, help, highscores (each one is a different scene).
- Hierarchy: Scene : Layer : Node : Node.

Cocos2D Scene and Layers
- A node can be a sprite, label, etc. Note that a sprite may have a child sprite.
- Add nodes to a layer with addChild method.
- Scenes have depth ordering (z): lower Z objects appear behind higher Z ones (er, ok, very old thing, we learn this when we are 8? but always worth noting :) ).
- Cocos auto dealloc all children.
- Run an action:
[node (sprite, etc) runAction:[CCSequence (or CCSpawn) actions:action1, action2, CCCallFunN (callback on the object when the actions are performed)]].
- How to load a sprite:
CCSprite *sprite = [CCSprite spriteWithFile:@"image.png" rect:CGRectMake(0, 0, w, h)];
sprite.position = ccp(x, y);
Coordinates
- UIKit: 0,0 from top left.
- Cocos2d: 0,0 from bottom left.
- Sprites: 0,0 from bottom left.
Box2D
Body
- Definition (b2BodyDef): The body definition holds the data needed to create and initialize a body – position, velocity, type.
- Object (b2Body): “body factory”: myWorld->CreateBody(&bodyDef);
- Shape (b2PolygonShape, b2CircleShape, etc): geometry you wish to simulate.
- Fixture definition (b2FixtureDef): binds a shape to a bodys. Shapes don’t know about bodies, that’s why we need fixtures. Defines:
4.1) Density (more dense, more mass = harder to move).
4.2) Friction (0…1 – how hard for objects to slide against each other).
4.3) Restitution (how “bouncy” is an object. 0 = no bounce, 1 = perfectly elastic).
- Fixture object (b2Fixture): b2Fixture* myFixture = myBody->CreateFixture(&fixtureDef);. A body may have any number of fixtures.
Cocos2d Image credits: Official wiki
30/Jun/10
- I bought an iPod Touch and already got my Apple Developer Certificate.

Box2D
- Started playing around with Cocos2D test codes. It comes packed with a lot of examples, being pretty easy and straightforward to learn. I’m loving it. It makes everything easy.
- Decided I’ll use Box2D to the physics. It has a complete documentation, as I read it is more mature, and it is C++ (which I prefer instead of straight C). Also, I understood easier its code comparing to Chipmunk.
- Spent the rest of the day trying to fix the timestep from Box2D examples which come with Cocos2D. I was following the famous Gaffer’s article and managed to put the code and make it worked, but I don’t know where I failed that the code started running at 50-60 fps, and then when I added 15 boxes the fps dropped to 0.5 ~ 1.0! Until now I don’t know how to fix it.
- At least I got a good Box2D understanding and got some more knowledge about Game performance and when dealing with a lot of objects in the same screen.
- Also downloaded some examples using Box2D, but none of them have fixed timestep.
Source Code from Box2D example with fixed timestep:
-(void) tick: (ccTime) dt
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
float fixedTimeStep = 1.0/60.0f;
float timeToRun = dt + timeAccumulated;
while(timeToRun >= fixedTimeStep) {
world->Step(fixedTimeStep, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
timeToRun -= fixedTimeStep;
}
timeAccumulated = timeToRun;
}
Note that I implemented the accumulator. I think I’m going wrong when stepping Box2D. But I’ll discover that later.
Links:
Resources:
Image credits: Box2D logo, from http://box2d.org/
24/Jun/10
Objective-C
I’ve studied Objective-C on March, so I still have the notes and the mind map I made. It was easy to refresh the mind about it. I like this language – it makes me feel “warm and safe”.
- NSCoding – serializer.
- NSKeyedArchiver
- NSUserDefaults
[NSUserDefaults standardUserDefaults]
- Selectors: function pointers. The type is SEL.
- SEL compare Selector = @selector(compare:options:)
- SEL name = NSSelectorFromString / NSStringFromSelector
- To test:
[object respondsToSelector:selectorVar]
- To call:
[object performSelector:selectorVar]
Books: