如何将 SpaceManager cpShape 存储在类似数组的形式中?
我正在尝试使用 Cocos2d 和 Chipmunk(通过 SpaceManager)创建一个柔软的球,使用一堆链接在一起的矩形,然后用弹簧连接到中心体。
但是为了做到这一点,我想我需要将所有 cpShapes 存储在一个数组中创建它们之后,我可以循环遍历数组,通过约束将它们全部链接在一起。
但是,当我尝试将 cpShapes 放入数组中时,我收到一条错误消息,告诉我这是“不兼容的指针类型”。所以...我要么需要使用数组以外的东西(我已经尝试过一组,但也不起作用),或者我需要对形状做一些事情以使其兼容...但是什么?或者我需要另一种方法。
有什么想法吗?
这是到目前为止的代码应该是相关的......
- (id) init
{
if ( (self = [super init]) ) {
SpaceManager * spaceMgr = [[SpaceManager alloc] init];
[spaceMgr addWindowContainmentWithFriction:1.0 elasticity:1.0 inset:cpv(5, 5)];
// This is a layer that draws all the chipmunk shapes
ChipmunkDrawingLayer *debug = [[ChipmunkDrawingLayer node] initWithSpace:spaceMgr.space];
[self addChild:debug];
int ballPeices = 10; // the number of peices I want my ball to be composed of
int ballRadius = 100;
float circum = M_PI * (ballRadius * 2);
float peiceSize = circum / ballPeices;
float angleIncrement = 360 / ballPeices;
CGPoint origin = ccp(240, 160);
float currentAngleIncrement = 0;
NSMutableArray *peiceArray = [NSMutableArray arrayWithCapacity:ballPeices];
for (int i = 0; i < ballPeices; i++) {
float angleIncrementInRadians = CC_DEGREES_TO_RADIANS(currentAngleIncrement);
float xp = origin.x + ballRadius * cos(angleIncrementInRadians);
float yp = origin.y + ballRadius * sin(angleIncrementInRadians);
// This is wrong, I need to figure out what's going on here.
float peiceRotation = atan2( origin.y - yp, origin.x - xp);
cpShape *currentPeice = [spaceMgr addRectAt:ccp(xp, yp) mass:1 width:peiceSize height:10 rotation:peiceRotation];
currentAngleIncrement += angleIncrement;
[peiceArray addObject:currentPeice]; //!! incompatible pointer type
}
spaceMgr.constantDt = 0.9/55.0;
spaceMgr.gravity = ccp(0,-980);
spaceMgr.damping = 1.0;
}
return self;
}
I'm trying to create a squishy ball with Cocos2d and Chipmunk (via SpaceManager) using a bunch of rects all chained together then joined with springs to a central body.
But in order to do this, i think I need to store all the cpShapes in an array after I've created them so I can then loop through the array to link them all together with constraints.
However, when i try to put cpShapes in an array, I get an error telling me it's an "incompatible pointer type". So... I either need to use something other than an array (I've tried a set, that didn't work either) or I need to do something to the shape to make it compatible... but what? OR I need another aproach.
Any ideas?
Here's the code so far should it be relevant...
- (id) init
{
if ( (self = [super init]) ) {
SpaceManager * spaceMgr = [[SpaceManager alloc] init];
[spaceMgr addWindowContainmentWithFriction:1.0 elasticity:1.0 inset:cpv(5, 5)];
// This is a layer that draws all the chipmunk shapes
ChipmunkDrawingLayer *debug = [[ChipmunkDrawingLayer node] initWithSpace:spaceMgr.space];
[self addChild:debug];
int ballPeices = 10; // the number of peices I want my ball to be composed of
int ballRadius = 100;
float circum = M_PI * (ballRadius * 2);
float peiceSize = circum / ballPeices;
float angleIncrement = 360 / ballPeices;
CGPoint origin = ccp(240, 160);
float currentAngleIncrement = 0;
NSMutableArray *peiceArray = [NSMutableArray arrayWithCapacity:ballPeices];
for (int i = 0; i < ballPeices; i++) {
float angleIncrementInRadians = CC_DEGREES_TO_RADIANS(currentAngleIncrement);
float xp = origin.x + ballRadius * cos(angleIncrementInRadians);
float yp = origin.y + ballRadius * sin(angleIncrementInRadians);
// This is wrong, I need to figure out what's going on here.
float peiceRotation = atan2( origin.y - yp, origin.x - xp);
cpShape *currentPeice = [spaceMgr addRectAt:ccp(xp, yp) mass:1 width:peiceSize height:10 rotation:peiceRotation];
currentAngleIncrement += angleIncrement;
[peiceArray addObject:currentPeice]; //!! incompatible pointer type
}
spaceMgr.constantDt = 0.9/55.0;
spaceMgr.gravity = ccp(0,-980);
spaceMgr.damping = 1.0;
}
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不兼容的指针类型很容易解释:)
[NSMutableArray addObject]
的定义如下:那么什么是
id
呢?好问题!请记住,Objective-C 的核心仍然是 C。根据 Objective-C 编程指南太好了,现在我们知道了
*id
,但是id
本身呢?这就是方法签名中引用的内容。为此,我们必须查看 objc.h< /a>显然,
cpSpace*
不适合这种情况,因此,如果您尝试使用该消息将它们放入NSMutableArray
中,您将得到不兼容的指针类型。The incompatible pointer type is easy to explain :)
[NSMutableArray addObject]
is defined as such:So what is an
id
then? Great question! Remember, Objective-C is still C at it's core. According to the Objective-C Programming GuideThat's great, now we know about
*id
but what aboutid
itself? That's what is referenced in the method signature. For that, we have to look at objc.hClearly,
cpSpace*
doesn't fit that, so you'll be getting incompatible pointer type if you try to put those intoNSMutableArray
using that message.