Sprite and attached physics body anchor points differ - cocos2d-iphone

When rehearsing Cocos2d V3 physics with debug mode enabled I noticed that physics body attached to its sprite has different anchor point from that of the sprite itself. Here's how it looks:
And this is how I create a sprite with physics body:
CCSprite *beam=[CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"w272.png"]];
beam.physicsBody=[CCPhysicsBody bodyWithRect:beam.boundingBox cornerRadius:0];
beam.position=ccp(125, 160);
[physicsWorld addChild:beam];
Do you have any idea how to fix this? I don't set any anchor point anywhere.

Physics objects automatically calculate a center of gravity, which is slightly different than an anchor point. Your real problem though is that you are using the sprite's bounding box as the rectangle to create the body and that's expressed in local coordinates. You want to make a text that goes from (0,0) to content size.

Related

Rocket like movement in cocos 2d V3 with chipmunk

On tapping on screen I`m gonna apply impulse and angular rotation to sprite to move it to the sides. But after some time OR parameters of physics body attached to sprite should go back to initial (impulse 0, angular speed = 0). What are the best ways to do that (use CCAction or on update detect parameters of physics body and change them to initial)?
If i were in your position, I would probably start a timer at the first collision of the sprite and then use a CCAction to transition to the initial state to make it look smooth. Don't know if its the best way though.

Cocos2d - hiding, clipping or masking sprite like in game Candy Crush

I have a problem and I saw it also in the game Candy Crush Saga, where they successfully dealt with it. I would like the sprite to show only when it is in the board (see image link below). The board can be of different shapes, like the levels in the mentioned game.
Has anyone some ideas how can this be achieved with Cocos2d?
I will be very glad if someone has some tips.
Thank you in advance.
image link: http://www.android-games.fr/public/Candy-Crush-Saga/candy-crush-saga-bonus.jpg
In Cocos2d you can render sprites at different z levels. Images at a lower z-level will be drawn first by the graphic card and the images (sprites) with a higher z-value will be drawn later. Hence if an image (say A) is at the same position of the other but has a higher z-value you will see only the pixels of image A where the two images intersect.
Cocos2d uses also layers so you can decide to add Sprites to a layer and set the layer to a specific z value. I expect that they used a layer for the board (say at z=1) with a PNG image containing transparent bits in the area where you can see the sprites, and a second layer at z=0 for the sprites. In this way you can only see the sprites when they are in the transparent area.
Does this help?
I found out Cocos2d has a class CCClippingNode which does exatclly what I wanted. First I thought it can clip only rectangular areas, but after some research I found it can clip also paths.

Box2d Rectangular Body with Rounded Corners

I have implemented soft body(circle shape) in Box2d and when this soft body collide with the corner of the rectangle box2d object then it lost its circular shape. I think this can be solved by making corners of rectangle rounded or We can implement this rounded corner using soft body concept.
Can anybody guide me in this scenario ?
To get rounded corner I used Physics editor to make rounded corner physics body.You can get physics editor from here:http://www.codeandweb.com/physicseditor
To add those rounded corner physics in cocos2d iphone , i used GB2ShapeCache to cache those physics shape and then used that from cached memory.
You can get those GB2ShapeCache file bundle from here:http://www.4shared.com/zip/8H6BNDTH/GB2Shape.html
To cached those physics body:
import below file into your code,first import:
#import "GB2ShapeCache.h"
and then,
[[GB2ShapeCache sharedShapeCache] addShapesWithFile:#"rectangleRoundedCornerBody.plist"];
Now rounded corner rectangle is in your cache so to use that you can call them using their names like shown in below.
[[GB2ShapeCache sharedShapeCache] addFixturesToBody:PhysicsBody forShapeName:#"nameOfRectangleBody"];
If any one wants further explanation can comment..

Bending a CCSprite by Skewing it from various points like in photoshop

Hi I am Using Cocos2d with Box2d.
I have a "Stick.png" as CCSprite Now I want to bend it from center downwards and Upwards from the end and start of image.. I am trying to make game like hill Climb that is using only one Green image of a grass strip for the whole vehicle Path/Road used in the game.
I shall be thankful.
Please Help
You can use the skewX skewY properties to achieve this effect. There are also CCSkewBy and CCSkewTo actions to animate skewing.

cocos2D collision on edge shape only

I'm developing an iOS game with cocos2D.
My game is simple, there are levels, and a rotating sprite.
The sprite need to go from the beginning to the end of the level without losing his lives.
So there is two possibilities for me :
1°) Already working good
Tilemap based levels with 2D pixels styles tilesets
Custom collision detection on the edge of the hero's sprite bounding box, and the tilemap collision.
2°) Would be better graphics, and better users experience (without physics, only collision):
map base on vector graphics / SVG
collision detection using the edge of the hero's sprite shape and the map
But, i read the cocos2D/Box2D documentation, and i doesn't found a collision detection on the edge of the sprite's shape ONLY. It's like a pixel perfect collision (already found algo).
I only want to know if one of the 4 edge of my hero's shape is colliding a border of the level, and if yes which shape is colliding (because my sprite is rotating).
Someone have an idea ?
Thank you a lot for your time.
One polygon shape should be attached to your Hero's body via fixture.
To detect a collision point use contacts between dynamic(hero) and static(walls) bodies.
Just take your hero's shape and divide in half to find the pixel width of the hero shape (the radius) and detect collision if the distance between your hero and another sprite is equal to or less than this radius.