Multiple cpShapes (chipmunk) - cocos2d-iphone

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.
I need to attach the top poly to the top of the tire and the bottom poly to the bottom of the tire
I also need to 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)];
P.S I am using spaceManager+chipmunk

Basically, you create a single cpBody, and attach the 2 cpShape's to it. Using ccPhysicsSprite allows you to attach a sprite to a body, which is the result you're trying to get.

Unless spaceManager is doing something for you, it's up to you to determine how cpShapes and CCSprites attach. All I would do is subclass CCSprite and override -draw. In the -draw function update the position and rotation of the sprite to the center of the two shapes combined.
It would probably be easier to make two sprites and attach one sprite to one shape though.

Related

Particles around a sprite

I'm new to the CoCos2D development. I have checked out some tutorials and went on trying to develop a little idea.
I have checked some partical tutorials and it all looks nice but I still have some questions about it. I have seen how to call a particle and let it follow wherever you click on the layer and so on but I need something more.
Let's say I have several circle shaped sprites on my Layer. Is it possible to do the following: click on a sprite and then a partical trail should go around the sprite so you as a player can see it is selected. The particale should keep circling around the sprite until I select another circle shaped sprite which result in dissappearing of the previous particle trail and appearing onto the new selected sprite.
Is this possible with the particle system or should I look for another way of doing this ? Any type of help is appreciated.
kind regards

Cocos2d CCLabelBMFont how to add a background to string

I am wondering how can I add a border & background to labels generated via CCLabelBMFont class in cocos2d.
I don't want to use sprites because my labels are generated on the fly and will keep changing and the size of labels are also different.
Also, I want user to touch and move these labels across the screen. When user picks a label it wiggles a bit like in free air. In that case, I wish to keep low complexity and preserve memory and cpu calculations.
Anyone knows best ways to achieve this?
IOS app LetterPress has similar effects.
Create your own class, that will incapsulate creation of complex node.
It will have several layers, for example, the first layer can be simple CCLayerColor of given rect with zOrder -2, the next layer will be your CCLabelBMFont with zOrder -1 and then you can overload draw method to draw border over your control. All that you draw in this method will be drawn with zOrder 0.
Then you can encapsulate any effects in this class. For example, you can rotate it a bit with method pick, etc. Whatever you want.

ccSpriteBatchNode child bounding box transformation

I'm using cocos2d 1.0.1.
I've created a CCSpriteBatchNode, it includes a CCSprite (let's name it parentLayer) which includes some X number of childs(CCSprites).
The problem is - when I rotate parentLayer all sprites (childs) correctly displayed, however bounding boxes are at the same place (where they've been before rotation), so world coordinates of those sprites won't be changed.
Off course, all of the above works great without CCSpriteBatchNode. But, I would like to use batch node due to amount of sprites involved.
The question is, is there any way to update bounding boxes & child positions correspondingly?
How many sprites are we talking about? I would just go with a fast enumeration call to rotate each one individually. I nave never noticed a performance hit when doing this, have you?
CCArray *listOfChildren = [parentLayer children];
for (CCSprite *sprite in listOfChildren) {
[sprite setRotation:someValue];
}

Selecting different regions of a worldmap

In cocos2d for iPhone, how can I turn a selected portion of the screen into a menu item with selector functionality?
Imagine a worldmap with curved borders between different regions. When the player touches one of these regions, there should be a selector / callback for it. Sort of like a standard menu, but without making a label or an image selectable. Instead, what I want is to be able to specify the clickable area manually.
How about making a CCLayer that will contain this custom region? CCLayer already implements the touch delegates, and you could easily start capturing the regions by setting self.isTouchEnabled to true.
Define the areas of your world map manually, ideally as rectangles for polygons you'll need to look up polygon intersection test. Then just use CGRectContainsPoint with all rectangles and the touch point. If the touch is inside a world area, run whatever code needs to run.

Chipmunk + spaceManager

Hello to all I have searched many forums and cannot seem to find an answer to my question. I know that this forum is not particularly for chipmunk or spaceManager but I figured that I would see if I could get any help. My question is, I am making a simple game were the user tries to throw the football threw the center of the tire. I have created two poly shapes for the top and the bottom of the tire which I am wanting to attach to a single tire sprite. I don't know how to attach two shapes/body's to one sprite and have it attach to the top and bottom and still be able to move the sprite and use it for collision? If possible some sample code would be of great help! thanks. Here is how I am creating my shapes and sprites:
//UPPER HALF TIRE
[smgr 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)];
//LOWER HALF TIRE
[smgr 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)];
//Adding the Tire
CCSprite *backTire = [CCSprite spriteWithFile:#"TractorTireBack.png"];
backTire.position = ccp(70,160);
[self addChild:backTire z:1];
I know that I can attach one image to a shape like this:
cpShape *shape = [smgr 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)];
[super initWithShape:shape file:#"TractorTireBack.png"];
But how would I go about attaching two shapes to this one image? WIth one of the shapes being at the top and the other at the bottom.
do you want the tire to move? If the answer is no you could just leave the two cpShapes as they are and just add a standard CCSprite and position it in place.
Use
[self addChild:[smgr createDebugLayer]];
to make the cpShapes visible.