2012-03-07 9 views
0

私はbox2dを使用していて、2つのオブジェクトが衝突したときに検出するためにb2contactlistenerを使用しています。私の形状の1つが奇妙なのでbox2d PhysicsEditorを使用して形状を実装しました。http://www.physicseditor.de/と今はオブジェクトのb2Fixture b2contactlistenerは...これはすべてphysicsEditorによって処理されるので、今度は付属のクラスと一緒に私の形状のb2Fixtureを見つける方法はわかりません...プレハブクラスへの復帰を実装する

GB2ShapeCache.h

#import <Foundation/Foundation.h> 
#import <Box2D.h> 

/** 
* Shape cache 
* This class holds the shapes and makes them accessible 
* The format can be used on any MacOS/iOS system 
*/ 
@interface GB2ShapeCache : NSObject 
{ 
    NSMutableDictionary *shapeObjects; 
    b2Fixture *ColliderFixture; 
} 

+(GB2ShapeCache *)sharedShapeCache; 

/** 
* Adds shapes to the shape cache 
* @param plist name of the plist file to load 
*/ 
-(void) addShapesWithFile:(NSString*)plist; 

/** 
* Adds fixture data to a body 
* @param body body to add the fixture to 
* @param shape name of the shape 
*/ 
-(void) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape; 

/** 
* Returns the anchor point of the given sprite 
* @param shape name of the shape to get the anchorpoint for 
* @return anchorpoint 
*/ 
-(CGPoint) anchorPointForShape:(NSString*)shape; 


@end 

Gb2ShapeCache.mm

#import "GB2ShapeCache.h" 

#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) 
# define CGPointFromString_ CGPointFromString 
#else 
// well - not nice but works for now 
static CGPoint CGPointFromString_(NSString* str) 
{ 
    NSString* theString = str; 
    theString = [theString stringByReplacingOccurrencesOfString:@"{ " withString:@""]; 
    theString = [theString stringByReplacingOccurrencesOfString:@" }" withString:@""]; 
    NSArray *array = [theString componentsSeparatedByString:@","]; 
    return CGPointMake([[array objectAtIndex:0] floatValue], [[array objectAtIndex:1] floatValue]); 
} 
#endif 

/** 
* Internal class to hold the fixtures 
*/ 
class FixtureDef 
{ 
public: 
    FixtureDef() 
    : next(0) 
    {} 

    ~FixtureDef() 
    { 
     delete next; 
     delete fixture.shape; 
    } 

    FixtureDef *next; 
    b2FixtureDef fixture; 
    int callbackData; 
}; 

/** 
* Body definition 
* Holds the body and the anchor point 
*/ 
@interface BodyDef : NSObject 
{ 
@public 
    FixtureDef *fixtures; 
    CGPoint anchorPoint; 
} 
@end 


@implementation BodyDef 

-(id) init 
{ 
    self = [super init]; 
    if(self) 
    { 
     fixtures = 0; 
    } 
    return self; 
} 

-(void) dealloc 
{ 
    delete fixtures; 
    [super dealloc]; 
} 

@end 


@implementation GB2ShapeCache 


+ (GB2ShapeCache *)sharedShapeCache 
{ 
    static GB2ShapeCache *shapeCache = 0; 
    if(!shapeCache) 
    { 
     shapeCache = [[GB2ShapeCache alloc] init]; 
    } 
    return shapeCache; 
} 

-(id) init 
{ 
    self = [super init]; 
    if(self) 
    { 
     shapeObjects = [[NSMutableDictionary alloc] init]; 
    } 
    return self; 
} 

-(void) dealloc 
{ 
    [shapeObjects release]; 
    [super dealloc]; 
} 

-(void) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape 
{ 
    BodyDef *so = [shapeObjects objectForKey:shape]; 
    assert(so); 

     FixtureDef *fix = so->fixtures; 
    while(fix) 
    { 
     body->CreateFixture(&fix->fixture); 
     fix = fix->next; 
    } 

} 

-(CGPoint) anchorPointForShape:(NSString*)shape 
{ 
    BodyDef *bd = [shapeObjects objectForKey:shape]; 
    assert(bd); 
    return bd->anchorPoint; 
} 

-(void) addShapesWithFile:(NSString*)plist 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:plist 
               ofType:nil 
              inDirectory:nil]; 

    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path]; 

    NSDictionary *metadataDict = [dictionary objectForKey:@"metadata"]; 
    int format = [[metadataDict objectForKey:@"format"] intValue]; 
    float ptmRatio = [[metadataDict objectForKey:@"ptm_ratio"] floatValue]; 

    NSAssert(format == 1, @"Format not supported"); 

    NSDictionary *bodyDict = [dictionary objectForKey:@"bodies"]; 

    b2Vec2 vertices[b2_maxPolygonVertices]; 

    for(NSString *bodyName in bodyDict) 
    { 
     // get the body data 
     NSDictionary *bodyData = [bodyDict objectForKey:bodyName]; 

     // create body object 
     BodyDef *bodyDef = [[[BodyDef alloc] init] autorelease]; 

     bodyDef->anchorPoint = CGPointFromString_([bodyData objectForKey:@"anchorpoint"]); 

     // iterate through the fixtures 
     NSArray *fixtureList = [bodyData objectForKey:@"fixtures"]; 
     FixtureDef **nextFixtureDef = &(bodyDef->fixtures); 

     for(NSDictionary *fixtureData in fixtureList) 
     { 
      b2FixtureDef basicData; 

      basicData.filter.categoryBits = [[fixtureData objectForKey:@"filter_categoryBits"] intValue]; 
      basicData.filter.maskBits = [[fixtureData objectForKey:@"filter_maskBits"] intValue]; 
      basicData.filter.groupIndex = [[fixtureData objectForKey:@"filter_groupIndex"] intValue]; 
      basicData.friction = [[fixtureData objectForKey:@"friction"] floatValue]; 
      basicData.density = [[fixtureData objectForKey:@"density"] floatValue]; 
      basicData.restitution = [[fixtureData objectForKey:@"restitution"] floatValue]; 
      basicData.isSensor = [[fixtureData objectForKey:@"isSensor"] boolValue]; 
      int callbackData = [[fixtureData objectForKey:@"userdataCbValue"] intValue]; 

      NSString *fixtureType = [fixtureData objectForKey:@"fixture_type"]; 

      // read polygon fixtures. One convave fixture may consist of several convex polygons 
      if([fixtureType isEqual:@"POLYGON"]) 
      { 
       NSArray *polygonsArray = [fixtureData objectForKey:@"polygons"]; 

       for(NSArray *polygonArray in polygonsArray) 
       { 
        FixtureDef *fix = new FixtureDef(); 
        fix->fixture = basicData; // copy basic data 
        fix->callbackData = callbackData; 

        b2PolygonShape *polyshape = new b2PolygonShape(); 
        int vindex = 0; 

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

        polyshape->Set(vertices, vindex); 
        fix->fixture.shape = polyshape; 

        // create a list 
        *nextFixtureDef = fix; 
        nextFixtureDef = &(fix->next); 
       } 
      } 
      else 
      { 
       // circles are not yet supported 
       assert(0); 
      } 
     } 

     // add the body element to the hash 
     [shapeObjects setObject:bodyDef forKey:bodyName]; 
    } 
} 



@end 

基本的には、私は新しいb2bodyとb2FixtureDefを追加します。そこには、b2Fixture = "w/e"を追加することができ、そのコードはすべて輪郭を描いたphysicsEditor idkの私の形は私のb2Fixture = "w/e"を設定する場所

答えて

0

申し訳ありません。

あなたは、それらを反復処理することができます

b2Fixture *fixList = body->GetFixtureList(); 

としてあらゆる体の備品を得ることができます。もしあなたが1つしか持っていなければ、あなたはそれを削除することができます。

関連する問題