我正在尝试使用 Cocos2d 和 Chipmunk(通过 SpaceManager)创建一个柔软的球,使用一堆 rects 全部链接在一起,然后用弹簧连接到一个中央主体。
但是为了做到这一点,我认为我需要在创建它们之后将所有 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;
}