Im working in a C ++ object. The problem I am facing is that I need to pass std :: vector to the objective c method. Is it possible? Below is my current code, where I have to do my vector calculations in the vector definition method (adding an offset value to the members of the array) before moving to the method as an array of C. Ideally, I would like to set the offset values ββin the second method, dividing by definition (there will be several) The offset will be different from the example below.
So, instead of going through (b2Vec2 *) vect, I want to use vectors
- (void)createterrain1 { using namespace std; vector<b2Vec2>vecVerts; vector<int>::size_type i; vecVerts.push_back(b2Vec2(-1022.5f / 100.0, -20.2f / 100.0)); vecVerts.push_back(b2Vec2(-966.6f / 100.0, -18.0f / 100.0)); vecVerts.push_back(b2Vec2(-893.8f / 100.0, -10.3f / 100.0)); vecVerts.push_back(b2Vec2(-888.8f / 100.0, 1.1f / 100.0)); vecVerts.push_back(b2Vec2(-804.0f / 100.0, 10.3f / 100.0)); vecVerts.push_back(b2Vec2(-799.7f / 100.0, 5.3f / 100.0)); vecVerts.push_back(b2Vec2(-795.5f / 100.0, 8.1f / 100.0)); vecVerts.push_back(b2Vec2(-755.2f/ 100.0, -9.5f / 100.0)); vecVerts.push_back(b2Vec2(-632.2f / 100.0, 5.3f / 100.0)); vecVerts.push_back(b2Vec2(-603.9f / 100.0, 17.3f / 100.0)); vecVerts.push_back(b2Vec2(-536.0f / 100.0, 18.0f / 100.0)); vecVerts.push_back(b2Vec2(-518.3f / 100.0, 28.6f / 100.0)); vecVerts.push_back(b2Vec2(-282.1f / 100.0, 13.1f / 100.0)); vecVerts.push_back(b2Vec2(-258.1f / 100.0, 27.2f / 100.0)); vecVerts.push_back(b2Vec2(-135.1f / 100.0, 18.7f / 100.0)); vecVerts.push_back(b2Vec2(9.2f / 100.0, -19.4f / 100.0)); vecVerts.push_back(b2Vec2(483.0f / 100.0, -18.7f / 100.0)); vecVerts.push_back(b2Vec2(578.4f / 100.0, 11.0f / 100.0)); vecVerts.push_back(b2Vec2(733.3f / 100.0, -7.4f / 100.0)); vecVerts.push_back(b2Vec2(827.3f / 100.0, -1.1f / 100.0)); vecVerts.push_back(b2Vec2(1006.9f / 100.0, -20.2f / 100.0)); vecVerts.push_back(b2Vec2(1023.2fdddddd / 100.0, -20.2f / 100.0)); i = vecVerts.size();
This is my current method, passing in my vector as a C style array.
-(void)stitchterrainvertswith:(b2Vec2 *)verts num:(int)num { //create bodydef b2BodyDef groundBodyDef; //set body as static groundBodyDef.type = b2_staticBody; //set body position groundBodyDef.position.Set(0, 0); //create body using def groundBody = world->CreateBody(&groundBodyDef); //create shapes b2EdgeShape screenEdge; b2ChainShape terrain; terrain.CreateChain(verts, num); groundBody->CreateFixture(&terrain,0); //keeps track of max x value for all the ground pieces that are added to the scene //maxVerts.x += totalXVerts.x; }
I tried using the objc wrapper for std :: vector, but lost my example here:
VecWrap.h #import "Box2D.h" #include <vector> struct VecAcc; @interface VecWrap : NSObject { struct VecAcc* vec; } @end VecWrap.MM #import "VecWrap.h" struct VecAcc { std::vector<b2Vec2>data; }; @implementation VecWrap -(id)init { vec = 0; if (self == [super init]) { vec = new VecAcc; } return self; } -(void)dealloc { delete vec; [super dealloc]; } @end
and then created the following method:
-(void)stitchgroundvectswith:(VecAcc*)vecs num:(int)num;
What doesn't work, is it possible?