Box2D Forums

It is currently Fri May 17, 2013 11:19 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Jun 17, 2011 6:52 pm 
Offline

Joined: Fri Jun 17, 2011 6:42 pm
Posts: 4
Hi, newb here. I could really appreciate some help. I've never used any physics engine before.

I have my character as a dynamic body. The level is a platformer. I want him to be able to rotate a bit for sloped terrain, but never past his corner (on to his side or upside down).

What is the preferred method for achieving this?

Thanks so much,
--Stephanie


Top
 Profile  
 
PostPosted: Fri Jun 17, 2011 7:16 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
Is the rotation of the body important for the game? For example if it has a circle shape it makes no difference how it is rotated. In that case you could just draw the player at the angle you want without changing any Box2D stuff.

If it is important, you could make your character from two bodies, one of them being the body you already have, and the other one being a fixed rotation body. The fixed rotation one doesn't need to have any fixtures, just give it a small mass using SetMassData(). Joining these two bodies with a revolute joint would allow you to set limits on how far the main body can rotate.


Top
 Profile  
 
PostPosted: Sat Jun 18, 2011 9:31 am 
Offline

Joined: Fri Jun 17, 2011 6:42 pm
Posts: 4
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


Top
 Profile  
 
PostPosted: Sat Jun 18, 2011 1:34 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
Bodies don't have any visual characteristics, so unless they have fixtures attached, there will be nothing to show up. If it helps you visualize things you could add one to it just to have something to look at, or you could set the b2Draw::e_centerOfMassBit flag in the debug draw flags so that the center of mass display shows (the small x/y axes marker as in the testbed).
If you do add a fixture to the body, remember that doing so will alter the body's mass settings, so put the SetMassData after the fixture creation if you want to keep your customized mass.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group