Learning Diary #8: A*, Cocos2d and Box2d concepts

 

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

    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

  • Box units are in “meters” ranging from 0.1 to 10. Use macro: PTM_RATIO 32.0 to convert pixels to meters, example:
    b2Vec2(winSize.width/PTM_RATIO, 0);
  • World object: manages all the objects and the physics simulation.
  • Bodies: movable and static.

Body

  1. Definition (b2BodyDef): The body definition holds the data needed to create and initialize a body – position, velocity, type.
  2. Object (b2Body): “body factory”: myWorld->CreateBody(&bodyDef);
  3. Shape (b2PolygonShape, b2CircleShape, etc): geometry you wish to simulate.
  4. 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).
  5. Fixture object (b2Fixture): b2Fixture* myFixture = myBody->CreateFixture(&fixtureDef);. A body may have any number of fixtures.

Cocos2d Image credits: Official wiki

Leave one

4 Comments

  1. “Objective-C: You can’t remove an object from an array while you are iterating through it.”

    Yes, you can. If you don’t use fast enumerator but a regular for loop iterated in reverse:
    for (int i = [array count] – 1; i >= 0; i–) { //remove object at index i or higher is valid }

    In that case you *can* remove the current object (the one at index i) because it doesn’t change the order of objects which are still left to iterate over.

    Reply
    • Alfred R. Baudisch

       /  July 6, 2010

      Now everything makes more sense! I was somewhat confused, since C/C++ allows it. I’ll note that on the next post.

      Thanks for the tip!

      Reply
    • Even better, you can do this which is a lot cleaner, at least in my opinion:

      for (id *obj in [objArray reverseObjectEnumerator]) {
      [objArray removeObject:obj];
      }

      Reply
  1. Learning Diary #9: Cocos2D and Tile maps and more Box2D | Karnak Games

Leave a Reply