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.
Related
i`m working on a platformer game , i have A character who jumps on different platforms , until he reaches the end of the level.
one type of platform is a tree log floating on water. the log slowly moves up and down along the tide of water .
each platform is actually a b2body. here is how i define a platform :
b2Body *platformbody;
b2BodyDef platforbodydef;
b2FixtureDef platformfixdef;
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.5f, 0.5f);
platforbodydef.type=b2_staticBody;
platforbodydef.position.Set(BlockPlatX[i][j], BlockPlatY[i][j]);
platformbody= world->CreateBody(&platforbodydef);
platformfixdef.shape=&dynamicBox;
platformsprite=[CCPhysicsSprite spriteWithFile:#"TreeLog.png"];
dynamicBox.SetAsBox(platformsprite.texture.contentSize.width/PTM_RATIO/2,platformsprite.texture.contentSize.height/PTM_RATIO/2);
platformfixdef.friction=1;
platformfixdef.density=1;
platformfixdef.restitution=0;
platformbody->CreateFixture(&platformfixdef);
if(platforbodydef.position.y < watersprite.contentSize.height/2)
{
platforbodydef.position.Set(prevPlatX + 300, watersprite.contentSize.height/2 + 10);
CGPoint point=CGPointMake(platforbodydef.position.x,watersprite.contentSize.height/2);
CCMoveTo *waterMove=[CCMoveTo actionWithDuration:3 position:point];
point=CGPointMake(platforbodydef.position.x,watersprite.contentSize.height/2+ 10);
CCMoveTo *waterMoveBack=[CCMoveTo actionWithDuration:3 position:point];
CCSequence* sequence = [CCSequence actions:waterMove,waterMoveBack, nil];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];
[platformsprite runAction:repeat];
}
[platformsprite setPTMRatio:PTM_RATIO];
[platformsprite setB2Body:platformbody];
[platformsprite setPosition:CGPointMake(platforbodydef.position.x, platforbodydef.position.y)];
[self addChild:platformsprite z:4 tag: 10000 + i*100 + j];
i am using a loop ,so i create more than one of these platforms .
but the problem is that when the sprite runs the action sequence , the position of the b2body associated with it does`nt change and this obviously causes lots of problems.
is there anyway i can access the b2body of the sprite using its tag and change the position of the body instead?
First thing is that the sprite follows the body and not the other way round. When you are repositioning the b2Body via setTransform you have to be very careful as you can find some really odd behaviours. The collision is not being performed in the proper fashion, so if the moved body will intersect with something else some crazy stuff can happen.
Why are you using sequence to move the body on the water. You are much better off using a prismatic joint to push it along and at the end of the track place a sensor which will revert the motor of the joint to move it in the other direction. Let the physics engine deal with the movement. This way you allow the system to work in the way it was intended and the sprite will updates its position to the body automatically.
Edit: I would also advised using a visual box2d editor. You are using Cocos2d as an engine, so there is quite few to choose from. I would personally recommend "Really Useful Box2D Editor" or "R.U.B.E." in short.
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];
}
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.
I am not an expert in cocos2d....as cocos2d draw a 2d graphics than how can anyone identify one object is nearer than another from viewer??
can anyone help me....i have basic opengl graphics idea....
thanks
Rony
When you add a node to a parent, you can set its z order:
[scene addChild:aNode z:1];
A higher z order will be drawn first.
If you need to re-order a node after it's been added, you can call reorderChild:
[scene reorderChild:aNode z:3];
Hey all
Basically all i want is to create CCTexture2D objects from a spritesheet. I can make individual sprites from
charSpriteCur = [CCSprite spriteWithTexture:charSheet.texture rect:CGRectMake(136, 0, 136, 223)];
but i want to get individual textures from a spritesheet so that i can use
[mySprite setTexture:tex];
to change the sprite as required. I dont need it to be an animated sprite i just want to be able to change its texture when i want using a spritesheet.
any ideas with this or what is the best approach?
thanks
g
I don't think this is possible. When I need to do this I instead remove the CCSprite node (which I have setup as a property in my class) and then make a new one. Here is an example:
[self removeChild:[self mySprite] cleanup:YES];
[self setMySprite:[CCSprite spriteWithSpriteFrameName:#"image.png"]];
[mySprite setAnchorPoint:ccp(0,1)];
[mySprite setPosition:ccp(623,872)];
[self addChild:mySprite z:5];