Реализовать возврат в сборный класс
Я использую 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"
1 ответ
Извините за поздний ответ.
Вы можете получить крепления любого тела, как
b2Fixture *fixList = body->GetFixtureList();
затем вы можете перебирать их. если у вас есть только один, то вы можете удалить его, потому что это то, что вы хотите.