Box2d - a variable-length array of a non-POD element of type 'b2Vec2'

I work on an importer for my game, it reads xml and then creates box2d bodies for everything.

for example

<polygon vertexCount="3" density="0" friction="0.25" restitution="0.30000000000000004"> <vertice x="6.506500000000001" y="0.4345"/> <vertice x="6.534970527648927" y="0.48385302734375"/> <vertice x="6.478029472351075" y="0.48385302734375"/> </polygon> 

The problem is in the exporter Now I am faced with part of the polygon, I need to configure the b2vec2 array before adding vertices and setting their positions.

 int count = [[childnode attributeForName:@"vertexCount"] intValue]; b2Vec2 points[count]; 

but box2d wants dots [5] to be the actual literal number (for example, dots [5] instead of variable dots [number], the error it throws when I have a variable counter in:

  Variable length array of non-POD element type 'b2Vec2' 

How can i solve this? I tried to make it permanent, but that also does not work (and does not help me, since I need it to be dynamic).

+7
source share
2 answers

Took the easier way and got access to unimportantly to vars:

 polygonShape.m_vertexCount = count; 

and then set them to forloop:

 polygonShape.m_vertices[c].Set(x,y); 

works great:)

-3
source

You need to create an array on the heap:

 b2Vec2 *array = new b2Vec2[count]; 

Remember to delete the array manually when done.

or better use std :: vector:

 a) std::vector<b2Vec2> vertices(count); vertices[0].set(2, 3); vertices[1].set(3, 4); ... b) std::vector<b2Vec2> vertices; vertices.push_back(b2Vec2(2, 3)); vertices.push_back(b2Vec2(3, 4)); 
+19
source

All Articles