如何将 SpaceManager cpShape 存储在类似数组的形式中?

发布于 2024-09-09 04:24:54 字数 2157 浏览 10 评论 0原文

我正在尝试使用 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.

Something like these examples

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 技术交流群。

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

发布评论

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

评论(1

却一份温柔 2024-09-16 04:24:54

不兼容的指针类型很容易解释:)

[NSMutableArray addObject] 的定义如下:

- (void)addObject:(id)anObject

那么什么是 id 呢?好问题!请记住,Objective-C 的核心仍然是 C。根据 Objective-C 编程指南

typedef struct objc_object {
    Class isa;
} *id;

太好了,现在我们知道了 *id,但是 id 本身呢?这就是方法签名中引用的内容。为此,我们必须查看 objc.h< /a>

typedef id (*IMP)(id, SEL, ...);

显然,cpSpace* 不适合这种情况,因此,如果您尝试使用该消息将它们放入 NSMutableArray 中,您将得到不兼容的指针类型。

The incompatible pointer type is easy to explain :)

[NSMutableArray addObject] is defined as such:

- (void)addObject:(id)anObject

So what is an id then? Great question! Remember, Objective-C is still C at it's core. According to the Objective-C Programming Guide

typedef struct objc_object {
    Class isa;
} *id;

That's great, now we know about *id but what about id itself? That's what is referenced in the method signature. For that, we have to look at objc.h

typedef id (*IMP)(id, SEL, ...);

Clearly, cpSpace* doesn't fit that, so you'll be getting incompatible pointer type if you try to put those into NSMutableArray using that message.

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