Multiple cpShapes (SpaceManager)

Official forum for the Chipmunk2D Physics Library.
Post Reply
MacSteve
Posts: 9
Joined: Wed Mar 23, 2011 2:48 pm
Contact:

Multiple cpShapes (SpaceManager)

Post by MacSteve »

So I have created two shapes( poly's to be exact) and I need to be able to attach them to a single CCSprite/image, and have them stay in the position that I have created them at. Is there any way to do that? So in short this is what I am trying to achieve. I have a tire (CCSprite/image) and I have created 2 poly's/cpShapes, one for the top and one for the bottom of the tire so that when the user throws a football it can only go through the center of it. So I need to attach the top poly to the top of the tire and the bottom poly to the bottom of the tire, and also be able to use collision on the tire. For instance if the user throws the football and it hits the bottom of the tire, I need to be able to make the tire sway back and forth with both shapes moving with it. So my question is really only how can I attach two cpShapes to one image/CCSprite?

I have created my shapes like so:

cpShape *UpperShape = [game.spaceManager addPolyAt:cpv(70,195) mass:STATIC_MASS rotation:0 numPoints:6 points:cpv(2,12), cpv(28,8), cpv(33,0), cpv(36,-10), cpv(-33,-10), cpv(-20,8)];

cpShape *LowerShape = [game.spaceManager addPolyAt:cpv(70,125) mass:STATIC_MASS rotation:0 numPoints:7 points:cpv(34,8), cpv(31,0), cpv(25,-9), cpv(7,-13), cpv(-20,-8), cpv(-30,0), cpv(-35,8)];
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multiple cpShapes (SpaceManager)

Post by slembcke »

First of all, you can attach as many shapes to a rigid body in Chipmunk as you want. You create one rigid body for the tire and add your top and bottom shapes to that. Presumably the STATIC_MASS part is a SpaceManager thing to mark the shapes as static shapes. Static means they don't move, so you don't want to do that.

I've never actually used SpaceManager, but I believe it has a cpCCNode or something like that which you can use to add your two sprites as children to.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
MacSteve
Posts: 9
Joined: Wed Mar 23, 2011 2:48 pm
Contact:

Re: Multiple cpShapes (SpaceManager)

Post by MacSteve »

Is there any way that you could post some code on how to do that or point me to some other place that shows what you are saying to do? Thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multiple cpShapes (SpaceManager)

Post by slembcke »

I dunno, something like this probably. I've only used Cocos2D for a couple of contract projects, and have never used space manager.

Code: Select all

cpBody *body = cpBodyNew(...);

cpShape *top = cpPolyShapeNew(body, ...);
cpShape *bottom = cpPolyShapeNew(body, ...);

cpCCNode *node = [[cpCCNode alloc] initWithShape:top];

CCSprite *topSprite = [CCSprite spriteWithFile:...];
topSprite.position = ...;

CCSprite *bottomSprite = [CCSprite spriteWithFile:...];
bottomSprite.position = ...;

[node addChild:topSprite];
[node addChild:bottomSprite];
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: Multiple cpShapes (SpaceManager)

Post by mobilebros »

I've just helped MacSteve in an email he sent; just wanted to confirm that the multiple shapes to one body was the way to go... unfortunately SpaceManager isn't very conducive to this type of scenario, it tries to abstract the body away somewhat and the only solution I've come up with is a "combineShapes" method, which is wasteful if you know the correct body properties already....

and yes STATIC_MASS is basically INFINITY.... a funny artifact that came about in the "beginning" as the result as to my being confused about how an infinitely massive body was unable to move :oops:
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests