Physics Editor - Integrating into Pixelwave
  • sumiguchisumiguchi November 2011
    Posts: 72

    When purchasing Texture Packer I was offered a deal to save some money if I bundle in Physics Editor (http://www.physicseditor.de/).  It looked like a good tool and I wasn't sure if I could use it, but I bought it anyways.  It turns out I can use it and it was a snap to integrate into our PixelWave project.


    There are a couple of things to do:  
    Physics editor contains a helper class called GB2ShapeCache.  The method that loads the physics definitions from a plist file into a dictionary but it doesn't match up with Pixelwave's coordinate system.  I solved this as follows, but also opened a feature request for Physics Editor and the author suggested an alternate fix - see it here: https://codeandweb.fogbugz.com/default.asp?1298_1o3lsb8kh3sh174b

    In the function: -(void) addShapesWithFile:(NSString*)plist

    change this code:
      for (NSString *pointString in polygonArray)
                        {
                            CGPoint offset = CGPointFromString_(pointString);
                            vertices[vindex].x = (offset.x / ptmRatio) ; 
                            vertices[vindex].y = (offset.y / ptmRatio) ; 
                            vindex++;
                        }

    to this:

      for (NSString *pointString in [polygonArray reverseObjectEnumerator])
                        {
                            CGPoint offset = CGPointFromString_(pointString);
                            vertices[vindex].x = (offset.x / ptmRatio) ; 
                            vertices[vindex].y = -(offset.y / ptmRatio) ; 
                            vindex++;
                        }

    Then in the Box2DUtils class from the Pixelwave examples for box2d I added these two helper methods:

    + (b2Body *) staticBodyInWorld:(b2World *)physicsWorld
                    withPEName:(NSString *)name
                         forObject:(PXSprite *)obj
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.userData = obj;
      
        b2Body *body = physicsWorld->CreateBody(&bodyDef);
    [[GB2ShapeCache sharedShapeCache] addFixturesToBody:body forShapeName:name];
        
    return body;
        
    }

    + (b2Body *) dynamicBodyInWorld:(b2World *)physicsWorld
                        withPEName:(NSString *)name
                         forObject:(PXSprite *)obj
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_dynamicBody;
        bodyDef.userData = obj;
        
        b2Body *body = physicsWorld->CreateBody(&bodyDef);
    [[GB2ShapeCache sharedShapeCache] addFixturesToBody:body forShapeName:name];
         
    return body;
        
    }


    Then in your code you can just implement something like this:
        b2Body *body = NULL;
        body = [Box2DUtils staticBodyInWorld:physicsWorld withPEName:myObject.name forObject:myObject];
     

    That's it - Cheers!


  • oztuneoztune November 2011
    Posts: 177

    Awesome. I'm sure this will come in handy for other devs.

    Pixelwave team
    // Founder

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Twitter Sign In with OpenID

Sign In Apply for Membership

In this Discussion

Tagged

Top Posters