Andreas Löw physicseditor, cocos2d, tutorial
This demo project contains all you need to get started to use physics shapes created with PhysicsEditor in your cocos2d project using chipmunk physics.
Get the full source from github!
git clone git@github.com:AndreasLoew/PhysicsEditor-Cocos2d-Chipmunk.gitPhysicsEditor-Cocos2d-Chipmunk.zip PhysicsEditor-Cocos2d-Chipmunk.tar.gz
I've spent several time in the last months to work together with Steffen Itterheim on adding some chapters to the update of his cocos2d book.
The new update of the book is called Learn cocos2d Game Development with iOS 5 and is available as eBook and Paperback.
It contains several complete game examples - including chapters on how to create physics enabled games with PhysicsEditor.
Aaron Goselin tutorial, texturepacker, physicseditor, box2d, cocos2d
Goal of this tutorial is to show how to use PhysicsEditor and TexturePacker together on one project. Both tools are not directly related to each other and can also be used independent.
This blog post is a guest post written by Aaron Goselin. Thanks!
Lets get started. I will attempt to keep things as simple as I can. I will assume you know how to start a new Cocos2D with Box2D project so please do that now. You may name your project whatever you like. I am naming mine TPPE_InteractionTut.
You can also download the complete example: TexturePacker and PhysicsEditor Cocos2d Example"
To keep things simple we will be doing much the same thing (programmatically) as the PE only tutorial does, except we will learn how to create a SpriteSheet in TexturePacker and how to apply physics to those sprites using PE.
First we will prepare the project. Please go to your HelloWorldLayer.mm file and inside of your init method, right inside the "if( (self=[super init])) {" braces insert the following (if it isn't already there):
// Define the gravity vector. b2Vec2 gravity; gravity.Set(0.0f, -10.0f); // Do we want to let bodies sleep? // This will speed up the physics simulation bool doSleep = true; // Construct a world object, which will hold // and simulate the rigid bodies. world = new b2World(gravity, doSleep); world->SetContinuousPhysics(true);
The templates do a lot of these things for you by default so you likely don't need to actually write that yourself. These few lines of code create the world for the physics engine to operate in. Floor shapes should also have been created by the template. If they are not already there then please see the complete example program for the code.
git clone git@github.com:AndreasLoew/TexturePacker-PhysicsEditor-Cocos2d.gitTexturePacker-PhysicsEditor-Cocos2d.zip TexturePacker-PhysicsEditor-Cocos2d.tar.gz
This tutorial shows you how to do integrate TexturePacker in your XCode 4 build within less than 5 minutes. The advantage is that TexturePacker now updates the sprite sheets during compile time! No manual work needed! The internal update mechanism in TexturePacker is very smart - it detects changes and only updates sheets that are really changed - saving you time!
Advantages for you:
You don't need to check in the complete sprite sheets in your version management system anymore. Assuming you have a bigger project you end up easily with several megabytes of sprite sheets. Why check them in if they can be built on the fly? There is no need for that. Simply check in your source sprites and let TexturePacker do the work during the build phase.
This is even more useful for git users since git stores the complete repository on your local computer.
While implementing support for gzip compressed pvr textures (.pvr.gz) in TexturePacker I found a simple solution for the loader code which works without reallocating memory!
When you look at the standard loader code implemented in cocos2d it allocates a block of memory and reallocates it if there is more data available in the file - this is because zlib does not deliver the size of the decompresses memory block. So without knowledge about the content of the file there is no other chance to load the data. This is why pvr.ccz was invented - containing the size of the decompressed data.
With a closer look at the PVR header
typedef struct _PVRTexHeader { uint32_t headerLength; uint32_t height; uint32_t width; uint32_t numMipmaps; uint32_t flags; uint32_t dataLength; // this is what we need! uint32_t bpp; uint32_t bitmaskRed; uint32_t bitmaskGreen; uint32_t bitmaskBlue; uint32_t bitmaskAlpha; uint32_t pvrTag; uint32_t numSurfs; } PVRTexHeader;
we see that dataLength already is what we need!
With this knowledge we can improve the loader routine:
In code:
char* loadCompressedPVR(const char *fileName) { // open the gz file gzFile inFile = gzopen(fileName, "rb"); if( inFile == NULL ) { error("can't open file"); } // load the header into structure // byteorder is ok for intel + arm PVRTexHeader header; if(gzread(inFile, &header, sizeof(header)) != sizeof(header)) { gzclose(inFile); error("can't read header"); } // check pvr magic cookie if(header.pvrTag != 559044176) { gzclose(inFile); error("not a pvr file"); } // allocate memory for the complete texture char *data = malloc(header.dataLength+ sizeof(header)); if(!data) { gzclose(inFile); error("out of memory"); } // copy header memcpy(data, &header, sizeof(header)); // read rest of file if(gzread(inFile, data+ sizeof(header), header.dataLength) != header.dataLength) { gzclose(inFile); error("failed to load pvr data"); } gzclose(inFile); return data; }