Box2D Forums

It is currently Sat May 18, 2013 2:06 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Nov 24, 2009 3:05 pm 
Offline

Joined: Sun Feb 08, 2009 9:17 am
Posts: 7
Hi, I am using cocos2d and am trying to initialize an actor class with body, and fixture already set when adding to world. I have the body working, but the shape of the fixture doesn't seem to get recognized. Does anyone know why this would be?
Thanks

Code:
/// SolidGameObject.h
@interface SolidGameObject : Layer
{
   GameObject *object; // for textures
   b2Body *body;
   b2BodyDef *bodyDef;
   b2CircleShape *shapeDef;
   b2FixtureDef *fixtureDef;
}
- (id)initWithWorld:(b2World *)wrld andCoords:(CGPoint)p;

@property (nonatomic, retain) GameObject *object;
@property (nonatomic, assign) b2Body *body;
@property (nonatomic, assign) b2BodyDef *bodyDef;
@property (nonatomic, assign) b2CircleShape *shapeDef;
@property (nonatomic, assign) b2FixtureDef *fixtureDef;
@end

/// SolidGameObject.mm

#define PTM_RATIO 32
@implementation SolidGameObject
@synthesize object, body, bodyDef, shapeDef, fixtureDef;

- (id)initWithWorld:(b2World *)wrld andCoords:(CGPoint)p
{
   if( (self=[super init]) )
   {
      self.relativeAnchorPoint = NO;
      
      object = [GameObject gameObject];
      [self addChild:object z:1 tag:10];
      
      // THE BODY DEF
      [self setBodyDef:new b2BodyDef];
      [self bodyDef]->isBullet = true;
      [self bodyDef]->fixedRotation = true;
      [self bodyDef]->position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
      
      // THE ACTUAL BODY
      body = wrld->CreateBody([self bodyDef]);
      
      // THE CIRCLE SHAPE
      [self setShapeDef:new b2CircleShape];
      [self shapeDef]->m_radius = 3.0f/PTM_RATIO;
      
      // THE FIXTURE
      [self setFixtureDef:new b2FixtureDef];
      [self fixtureDef]->shape = shapeDef;
      [self fixtureDef]->friction = 0.0f;
      [self fixtureDef]->density = 0.0f;
      [self fixtureDef]->restitution = 0.8f;
      
      body->CreateFixture([self fixtureDef]);
      body->SetUserData(object);
   }
   return self;
}
@end

///GameLayer.mm

SolidGameObject *solid = [[[SolidGameObject alloc] initWithWorld:world andCoords:CGPointMake(240,40)] autorelease];
[self addChild:solid z:4 tag:10];



Top
 Profile  
 
PostPosted: Tue Nov 24, 2009 3:27 pm 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
Try doing body.setMassFromShapes()
edit: nevermind, i'm not sure


Top
 Profile  
 
PostPosted: Wed Nov 25, 2009 1:31 am 
Offline

Joined: Sun Feb 08, 2009 9:17 am
Posts: 7
Ok, I got it working correctly now. My problem is that I am still relatively new to programming. I tried out someone else's code http://www.cocos2d-iphone.org/forum/topic/2535 thinking I knew what was going on, without stopping to think about what the code was doing. Please disregard.


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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