你能帮我将 b2Vec2 存储在 NSArray 中吗?
我制作了一个 NSArray,其中包含一些 b2Vec2 (C++) 对象。我使用了以下代码:
NSMutableArray *array = [[NSMutableArray alloc] init];
b2Vec2 verts[] = {
b2Vec2(28.3f / PTM_RATIO, 80.3f / PTM_RATIO),
b2Vec2(7.1f / PTM_RATIO, 89.4f / PTM_RATIO)
};
[array addObject:[NSValue valueWithPointer:verts]];
然后,我从 NSArray
中读出了 b2Vec2
对象,如下所示:
NSValue *val = [array objectAtIndex:0];
b2Vec2 *verts;
[val getValue:&verts];
b2Vec2 vec = verts[0];
我看到了 x
和 y< /code> 来自
vector
对象的值,但它的值令人困惑。
例如,PTM_RATIO = 32
,因此 28.3f / PTM_RATIO = 0.884375
,但向量的 x 值为 4.3629125e-38。
我想以原始 C++ 形式返回 verts
对象,作为 b2Vec2
数组。我该怎么做?
I made an NSArray
which has some b2Vec2
(C++) objects in it. I used the following code:
NSMutableArray *array = [[NSMutableArray alloc] init];
b2Vec2 verts[] = {
b2Vec2(28.3f / PTM_RATIO, 80.3f / PTM_RATIO),
b2Vec2(7.1f / PTM_RATIO, 89.4f / PTM_RATIO)
};
[array addObject:[NSValue valueWithPointer:verts]];
Then, I read out b2Vec2
objects from the NSArray
like so:
NSValue *val = [array objectAtIndex:0];
b2Vec2 *verts;
[val getValue:&verts];
b2Vec2 vec = verts[0];
I saw the x
and y
values from the vector
object, but it has confusing values.
For example, PTM_RATIO = 32
so 28.3f / PTM_RATIO = 0.884375
but the vector has an x value of 4.3629125e-38.
I want to return the verts
object in its original C++ form, as a b2Vec2
array. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将顶点作为 CGPoints 存储在 NSValue 中,并将其存储在数组中。然后在需要时转换为 b2Vec2。
//
然后检索
为了好玩,并与我们分享,您可以尝试将
b2Vec2
直接存储到NSValue
对象中,考虑到它们下面的结构相同。至于将结构数组直接存储到 NSValue 中,我以前从未见过这样做
You can store your verts as in an NSValue as CGPoints and store that in the array. Then translate to a b2Vec2 when you need it.
//
then retrieve
For fun, and share back with us, you can try storing the
b2Vec2
directly into theNSValue
objects considering they are the same structures underneath.As far as storing an array of structures directly into the NSValue, I have never seen that done before
将指针放入 NSArray 可能不是一个好主意,因为您只是添加指向数组的指针而不是实际数据,请尝试将 b2Vec2 数组复制或初始化到 NSData 或 NSMutableData 中,然后将它们添加到数组中。
...
putting pointer into a NSArray is probable not a good idea because you are adding just the pointers to the array no the actual data, try copying or initialising your b2Vec2 array into a NSData or NSMutableData and then add them to your array.
...