Learning Diary #9: Cocos2D and Tile maps and more Box2D

 

06/Jul/2010

Tiled (my map is awful, I know)

Tiled (my map is awful, I know)

  • Another day invested reading Ray Wenderlich tutorials! This time, I’ve read his last two tutorials @Box2D and the ones about Cocos2D with tiled maps from Tiled. Pretty easy and clear:
    - How To Create A Breakout Game with Box2D and Cocos2D Tutorial: Part 1 and Part 2.
    - How To Make a Tile Based Game with Cocos2D and Collisions and Collectables: How To Make a Tile Based Game with Cocos2D Part 2.
  • What can I say about these tutorials? Well, I learned almost every concept behind Cocos2D, Box2D, tiled maps and some more concepts (like general game structure, how to put together Physics, general game loop, AI – even if he doesn’t cite AI, and so on). I already had some game development background (I read gamedev books for 6 years already), but even if you are starting, you will get a lot of knowledge reading them.
  • Objective-C: GamingHorror commented on last post that Objective-C can remove an object from an array while iterating through it if using a regular loop (as in C and C++). We just can’t remove an object if using a fast numerator (for..in). Thanks Steffen for the tip!
  • I was reading the source code from C++ STL find iterator and I saw on the documentation:
    // @return The first iterator @c i in the range @p [first,last) such that @c *i == @p val, or @p last if no such iterator exists.

    I was scared for not remembering what [a,b) meant. So, time for some Wikipedia about math and here we have a little refreshment on math sets and intervals notations:

    () exclude
    [] include
    ][ exclude
    Ex: [a,b) = a <= x < b

Cocos2D and Tiled

  • For the sake of curiosity, on 2004 I posted on Gamedev.net (I am the poster Maquiavel) forums a topic with links to sprites' sites: http://www.gamedev.net/community/forums/topic.asp?topic_id=272386. It is still rocking! Although most of the listed sprites are copyrighted, they may be useful to in-house prototyping or just curiosity. I don't know why I made that topic, since I'm only make into game development now, maybe because I love to see those sheets full of colors and life.
  • We get performance if we put all of our animations in one big sprite sheet (max. dimension: 1024x1024). That can be easily accomplished using Zwoptex.
  • Tiled has tile layers and object layers (boxes around portions of the map to specify events).
  • Cocos' convertToNodeSpace: as an example the user clicks in the viewpoint (scrolling camera) at 100,100. But let's say the map is scrolled to 800,800. convertToNodeSpace matches the touch with the scrolling / node zoom / etc.
  • CCTMXTiledMap: it's a CCNode (position, scale, rotation, etc). Its children are layers (CCTMXLayer) of the map generated with Tiled. CCTMXLayer is a CCSpriteSheet, which means we can only have one tileset per layer.
  • Helper function to convert from screen coordinates to tile coordinates:
    // Helper: Position to Tile Coord (upper left, increments each tile)
    // @author http://www.raywenderlich.com
    - (CGPoint)tileCoordForPosition:(CGPoint)position {
        int x = position.x / _tileMap.tileSize.width;
        int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
        return ccp(x, y);
    }

Box2D

  • Box2D

    When giving values to Box2D: pixels/PTM_RATIO

  • When getting values from Box2D: boxvalue * PTM_RATIO (obviously)
  • Mouse joint: makes a body move toward a specific point. Requires:
    - 2 bodies: static or moving body and body you want to move (Box2D manual note: All joints are connected between two different bodies. One body may static. Joints between static and/or kinematic bodies are allowed, but have no effect and use some processing time.).
    - Target: where to move.
    - Collide Connected: when both bodies collide, treat as collision.
    - Max Force: less force, less reaction from our movements.
  • setAwake: for objects that sleep.
  • linearDamping: to reduce speed from a moving body.
  • Contact Listener: it will be called by Box2D when 2 objects begin to touch and stop touching. Btw, we can't store references to the contact points, because Box2D reuses them. We need to copy them inside our contact listener.
  • setAsBox: we have to pass half dimensions.

Leave a Reply