Box2D Forums

It is currently Mon May 20, 2013 2:49 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Feb 24, 2012 11:59 pm 
Offline

Joined: Fri Feb 24, 2012 10:18 pm
Posts: 2
Hi, in my iOS game, I'm storing the fixture definition as a property of a game object class , which means each game object has a physical representation. Then when the game starts, I retrieve the fixture definition from the game objects and create physical bodies accordingly. My code is like this:
Code:
@interface GameObject : NSObject
@property b2FixtureDef fixtureDef
@property CGSize size
...
(void)setFixtureDef {
  fixtureDef.density = 1.0f;
  fixtureDef.friction = 0.3f;
  fixtureDef.restitution = 0.1f;
  b2PolygonShape shape;
  shape.setAsBox(size.width, size.height);
  fixtureDef.shape = &shape;
}

Then in GameLogic, I loop through all the fixtures of the game objects and create the associating physical bodies.

But it gives me exc_bad_access when I do body->createFixture(fixtureDef), in particular, the error is at line
Code:
m_shape = def->shape->Clone(allocator);

in b2Fixture::Create. I think the problem is my shape object is not retained when I retrieve the fixture definition in GameLogic. I am doing something wrong. I'm not familiar with C++, so I hope somebody can point me to how I can achieve the above-described task.


Top
 Profile  
 
PostPosted: Sat Feb 25, 2012 9:59 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
You'll need to store the shape as well, because after the setFixtureDef function finishes, the shape is destroyed and the pointer you set in fixtureDef.shape is invalid.


Top
 Profile  
 
PostPosted: Sat Feb 25, 2012 10:38 am 
Offline

Joined: Fri Feb 24, 2012 10:18 pm
Posts: 2
I've thought of that but don't know how to store the shape property in a way that allows inheritance. I have multiple game objects that inherit from the GameObject class, and each of them can define its own shape. How can I do it when b2Shape is an abstract class?


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 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