Box2d - 非 POD 元素类型“b2Vec2”的可变长度数组;

发布于 2024-11-26 01:45:18 字数 798 浏览 0 评论 0原文

我正在为我的游戏开发一个导入器,它读取 xml,然后为所有内容创建 box2d 主体。

例如,

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

问题出在导出器中,我现在面对多边形部分,我需要在添加顶点并设置它们的位置之前设置一个 b2vec2 数组。

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

但是box2d希望points[5]是一个实际的文字数字(就像points[5]而不是变量points[number],当我有变量count时它输出的错误是:

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

我该如何解决这个问题?我尝试将其设为常量,但这也不起作用(并且对我没有帮助,因为我需要它是动态的)。

I'm working on a importer for a game of mine, it reads an xml and then creates the 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 I'm now facing the polygon part, I need to setup a b2vec2 array before adding the vertices and setting their positions.

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

but box2d wants the points[5] to be an actual literal number (like points[5] instead of a variable points[number], the error it outputs when I have the variable count in there is:

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

How do I solve this? I tried making it a constant but that doesn't work either (and doesn't help me since I need it to be dynamic).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-12-03 01:45:18

您必须在堆上创建数组:

b2Vec2 *array = new b2Vec2[count];

完成后不要忘记手动删除数组。

或者更好地使用 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));

You have to create the array on a heap:

b2Vec2 *array = new b2Vec2[count];

Don't forget to delete the array manually when finished.

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));
伏妖词 2024-12-03 01:45:18

采用更简单的路线并访问 not Should to vars:

polygonShape.m_vertexCount = count;

然后将它们设置在 forloop 中:

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

它工作得很好:)

Took the easier route and accessed the not suppose to vars:

polygonShape.m_vertexCount = count;

and then set them in the forloop:

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

it's working perfectly fine :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文