Hey,
Thanks for the help. I think it is important, as the character is basically a rectangle. I don't want him rolling down hills.
I tried making a second body, but I can't get it to show up. Any thoughts on where the problem might be?
Code:
CGSize screenSize = [[CCDirector sharedDirector] winSize];
float randomOffset = CCRANDOM_0_1() * 10.0f - 5.0f;
CGPoint startPos = CGPointMake(screenSize.width - 15 + randomOffset, 80);
NSString* spriteFrameName = @"sprite.png";
CCSprite* tempSprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
b2PolygonShape shape;
float hx = (tempSprite.contentSize.width / PTM_RATIO) * 0.5f;
float hy = (tempSprite.contentSize.height / PTM_RATIO) * 0.5f;
shape.SetAsBox(hx,hy);
// Create a body definition and set it to be a dynamic body
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = [Helper toMeters:startPos];
bodyDef.angularDamping = 1.0f;
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 0.01f;
fixtureDef.friction = 1.0f;
fixtureDef.restitution = 0.3f;
[super createBodyInWorld:world bodyDef:&bodyDef fixtureDef:&fixtureDef spriteFrameName:spriteFrameName];
// Create a body definition and set it to be a dynamic body
b2BodyDef body2Def;
body2Def.type = b2_dynamicBody;
body2Def.position = [Helper toMeters:startPos];
body2Def.fixedRotation = TRUE;
b2Body* body2 = world->CreateBody(&body2Def);
body2->SetUserData(self);
b2MassData md;
body2->GetMassData(&md);
md.mass = 0.01f;
body2->SetMassData(&md);
and createBodyInWorld is:
Code:
-(void) createBodyInWorld:(b2World*)world bodyDef:(b2BodyDef*)bodyDef fixtureDef:(b2FixtureDef*)fixtureDef spriteFrameName:(NSString*)spriteFrameName
{
NSAssert(world != NULL, @"world is null!");
NSAssert(bodyDef != NULL, @"bodyDef is null!");
NSAssert(spriteFrameName != nil, @"spriteFrameName is nil!");
[self removeSprite];
[self removeBody];
CCSpriteBatchNode* batch = [[LevelTerrain sharedLevel] getSpriteBatch];
sprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
[batch addChild:sprite];
body = world->CreateBody(bodyDef);
body->SetUserData(self);
if (fixtureDef != NULL)
{
body->CreateFixture(fixtureDef);
}
}
The first body shows up in the debugdraw, but the second one does not.
Thanks again for all the help,
--Stephanie