views:

307

answers:

2

Hey there,

I am not sure if many of you are familiar with the box2d physics engine, but I am using it within cocos2d and objective c.

This more or less could be a general objective-c question though, I am performing this:

NSMutableArray *allShapes = [[NSMutableArray array] retain]; 
b2PolygonShape shape;
..
..
[allShapes addObject:shape];

and receiving this error on the addObject definition on build:

cannot convert 'b2PolygonShape' to 'objc_object*' in argument passing

So more or less I guess I want to know how to add a b2PolygonShape to a mutable array. b2PolygonShape appears to just be a class, not a struct or anything like that. The closest thing I could find on google to which I think could do this is described as 'encapsulating the b2PolygonShape as an NSObject and then add that to the array', but not sure the best way to do this, however I would have thought this object should add using addObject, as some of my other instantiated class objects add to arrays fine.

Is this all because b2PolygonShape does not inherit NSObject at it's root?

Thanks

+3  A: 

b2PolygonShape is a C++ class, not an ObjC class. You can only put ObjC instances into "NS-containers".

Since you need C++ anyway, it's better to use a std::vector<b2PolygonShape>.

KennyTM
Ok thanks, so something like this? std::vector<b2PolygonShape> shape; But now how can I use shape.m_vertexCount = [listOfPoints count]; like I had before? 'has no member' error.. sorry my c++ knowledge is pretty limited :(
GONeale
@GONeale: No, I mean `std::vector<b2PolygonShape> allShapes; b2PolygonShape shape; ...; allShapes.push_back(shape);`. To get the `-count`, use `allShapes.size()`. There is documentation in http://www.cplusplus.com/reference/stl/vector/. (Don't change the `m_vertexCount` directly, use the `Set` method.)
KennyTM
Thanks for that Kenny. Might be the way to go.
GONeale
+1  A: 

NS-container classes can (as KennyTM pointed out) only store NSObjects. This can be a bit of a pain sometimes. But there are plenty of alternatives to NS-containers.

You can write Objective-C wrapper classes (or use NSValue), and store these in an NSArray.

You could use a plain old C array (though, that may not serve your needs, if the array size is undefined and shrinks and grows)

You could use a hash table to store your references.

A linked list of structs can also come in handy, and is fairly easy to create and maintain.

Should you decide to stick to std::vector, which is as good a solution as any, you can read more about that at: http://www.cplusplus.com/reference/stl/vector/

nash
Yes, thanks I have discovered they are a bit frustrating:) But I have decided to go for a wrapper method for now (custom class exposing my C++ object), but I might look into the vector method.
GONeale