Love this forum. I'm learning Box2D and Cocos2D and this forum has been very useful.
I'm creating B2Bodies with b2EdgeShape during ccTouchesMoved and I would like to have the existing body to be removed when I create a new body. I've spent over a week trying to destroy body or the fixture. I've tried setting existing body to inactive but no luck. I either destroy everything, destroy only new bodies or crash. Any hints or help would be appreciated. Below is the code for ccTouchesMoved to created the freehand draw bodies.
I'm creating B2Bodies with b2EdgeShape during ccTouchesMoved and I would like to have the existing body to be removed when I create a new body. I've spent over a week trying to destroy body or the fixture. I've tried setting existing body to inactive but no luck. I either destroy everything, destroy only new bodies or crash. Any hints or help would be appreciated. Below is the code for ccTouchesMoved to created the freehand draw bodies.
Code:
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint start = [myTouch locationInView: [myTouch view]];
start = [[CCDirector sharedDirector] convertToGL: start];
CGPoint end = [myTouch previousLocationInView:[myTouch view]];
end = [[CCDirector sharedDirector] convertToGL:end];
// brush
NSValue *value= [NSValue valueWithCGPoint:start];
[linePoints addObject:value ];
float distance = ccpDistance(start, end);
if (distance > 1)
{
int d = (int)distance;
b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
b2BodyDef bd;
bd.type = b2_staticBody;
bd.position.Set(0, 0);
_brushBody = _world->CreateBody(&bd);
_oldBrushBody = _brushBody;
for (int i = 0; i < d; i++) {
float difx = end.x - start.x;
float dify = end.y - start.y;
float delta = (float)i / distance;
brush = [[CCSprite spriteWithFile:[NSString stringWithFormat:@"brush.png"]] retain];
brush.scale = 2.0f;
[brush setPosition:ccp(start.x + (difx * delta), start.y + (dify * delta))];
[self addChild:brush];
[lineSprites addObject:brush];
// body
b2EdgeShape brushShape;
b2FixtureDef brushShapeDef;
brushShapeDef.shape = &brushShape;
brushShape.Set(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
_brushFixture = _brushBody->CreateFixture(&brushShapeDef);
}
}
}
UITouch *myTouch = [touches anyObject];
CGPoint start = [myTouch locationInView: [myTouch view]];
start = [[CCDirector sharedDirector] convertToGL: start];
CGPoint end = [myTouch previousLocationInView:[myTouch view]];
end = [[CCDirector sharedDirector] convertToGL:end];
// brush
NSValue *value= [NSValue valueWithCGPoint:start];
[linePoints addObject:value ];
float distance = ccpDistance(start, end);
if (distance > 1)
{
int d = (int)distance;
b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
b2BodyDef bd;
bd.type = b2_staticBody;
bd.position.Set(0, 0);
_brushBody = _world->CreateBody(&bd);
_oldBrushBody = _brushBody;
for (int i = 0; i < d; i++) {
float difx = end.x - start.x;
float dify = end.y - start.y;
float delta = (float)i / distance;
brush = [[CCSprite spriteWithFile:[NSString stringWithFormat:@"brush.png"]] retain];
brush.scale = 2.0f;
[brush setPosition:ccp(start.x + (difx * delta), start.y + (dify * delta))];
[self addChild:brush];
[lineSprites addObject:brush];
// body
b2EdgeShape brushShape;
b2FixtureDef brushShapeDef;
brushShapeDef.shape = &brushShape;
brushShape.Set(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
_brushFixture = _brushBody->CreateFixture(&brushShapeDef);
}
}
}