我正在使用box2d并使用b2contactlistener来检测两个对象何时发生碰撞,我的一个形状很奇怪所以我使用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 :(得分:0)
很抱歉回答迟到。
你可以获得任何身体的固定装置
b2Fixture *fixList = body->GetFixtureList();
然后你可以迭代它们。如果你只有一个,那么你可以删除它bcoz这就是你想要的。