Cocos2d Box2d multiple b2circle shape fixture for one body with positioning - cocos2d-iphone

Anybody knows how to add 2 circle fixture to one b2body with desired positioning?
I know how to add two polygon fixture to one body by using m_centroid. But how can I do it for circle fixtures.
Any answer will be appreciated. I want to stick some object together. I tried joints but they all are elastic. I want distance static.
Thanks everyone!

You should create two fixtures for your body and the shapes of those fixtures should be b2CircleShape
//Create a body. You'll need a b2BodyDef, but I've assumed you know how to use these since you say you've created bodies successfully before.
b2Body* body = world->CreateBody(&bodyDef);
//Create the first circle shape. It's offset from the center of the body by -2, 0.
b2CircleShape circleShape1;
circleShape1.m_radius = 0.5f;
circleShape1.m_p.Set(-2.0f, 0.0f);
b2FixtureDef circle1FixtureDef;
circle1FixtureDef.shape = &circleShape1;
circle1FixtureDef.density = 1.0f;
//Create the second circle shape. It's offset from the center of the body by 2, 0.
b2CircleShape circleShape2;
circleShape2.m_radius = 0.5f;
circleShape2.m_p.Set(2.0f, 0.0f);
b2FixtureDef circle2FixtureDef;
circle2FixtureDef.shape = &circleShape2;
circle2FixtureDef.density = 1.0f;
//Attach both of these fixtures to the body.
body->CreateFixture(&circle1FixtureDef);
body->CreateFixture(&circle2FixtureDef);

Related

using sprite for multiple bodies

In my game I have a block sprite. With this sprite I've made a blockbody so that my character can't walk through it.
cocos2d::CCSprite* block = cocos2d::CCSprite::create("Block.png");
block->setPosition(ccp(5,20));
this->addChild(block);
b2BodyDef blockbodydef;
blockbodydef.type = b2_kinematicBody;
blockbodydef.position.Set(5/PTM_RATIO,20/PTM_RATIO);
blockbodydef.userData = block;
b2Body *blockbody = world->CreateBody(&blockbodydef);
b2PolygonShape blockPoly;
blockPoly.SetAsBox(37.5/PTM_RATIO , 37.5 / PTM_RATIO);
b2FixtureDef blockshapedef;
blockshapedef.shape = &blockPoly;
blockshapedef.density = 2.0f;
blockshapedef.friction = 0.2f;
blockshapedef.restitution = 0.8f;
blockbody->CreateFixture(&blockshapedef);
However, I want to have multiple of these blocks. Is there a way to like change the position of the bodydef and sprite without the original sprite disappearing or do I have to create a different sprite for each block?
You have to create another sprite for another block. Textures are cached inside cocos2dx, so it will be loaded from file only once.

How can I prevent fixtures with isSensor = true from passing through ground fixture?

I am creating a border around the view-able area. Within this area I am creating other fixtures which are sensor enabled for collision detection. It seems that fixtures with isSensor = true pass through the window border. How can I prevent this from happening? Thanks!
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixtureDef.isSensor = true; //causes fixtures to fall through border
body->CreateFixture(&fixtureDef);
Window border:
CGSize screenSize = [CCDirector sharedDirector].winSize;
float widthInMeters = screenSize.width / PTM_RATIO;
float heightInMeters = screenSize.height / PTM_RATIO;
b2Vec2 lowerLeftCorner = b2Vec2(0, 0);
b2Vec2 lowerRightCorner = b2Vec2(widthInMeters, 0);
b2Vec2 upperLeftCorner = b2Vec2(0, heightInMeters);
b2Vec2 upperRightCorner = b2Vec2(widthInMeters, heightInMeters);
b2BodyDef screenBorderDef;
screenBorderDef.position.Set(0, 0);
b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
b2EdgeShape screenBorderShape;
screenBorderShape.Set(lowerLeftCorner, lowerRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(lowerRightCorner, upperRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperRightCorner, upperLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperLeftCorner, lowerLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
Make it isSensore false and check collision in the PostCollision method
One way would be to add a second fixture to the body that has the sensor on it, and set the collision filter so that it only collides with the border. You will need to learn a bit about how collision filter settings are used, and it can be a bit tricky at first. This might be helpful: http://www.iforce2d.net/b2dtut/collision-filtering
By default the category bits value is 1, so unless you have changed anything, all fixtures in your scene have category 1. To differentiate between the border and other fixtures, you would have to give them different categories. Let's say you make the ground fixture category 2:
screenBorderFixtureDef.filter.categoryBits = 2;
The default value for mask is 0xFFFF so all of the existing fixtures will still collide with the border as before, even when the category is changed like this. Then to make the newly added second fixture ignore all but the borders, you would set the mask to only collide with the borders:
fixtureDef.filter.maskBits = 2;
Another way would be to make the existing sensor fixture not a sensor, to have it collide with the borders. But of course then it will also collide with everything else. You can use the PreSolve callback of the contact listener to say that certain contacts should not do any collision response:
//in PreSolve
if ( this is NOT a contact between border and sensor )
contact->SetEnabled( false );

Cocos2D+Box2D: Stack bodies that doesn´t fall?

EDIT 2: Problem solved! I can´t promise it will work with different settings, but by putting my block´s body density to 0, the stack of blocks did not fall when new blocks are added.
I´m sorry about the poor title of the question, I´ll explain my problem closer here:
So, I´ve used Box2D and cocos2D to setup a simple project where two boxes stacks on top of each other (I´m planning to expand to 8-10 boxes). Right now, using a friction of 10.0f on each box, the box at the top still moves around a little. If I would add more boxes, the "tower" would fall and I don´t want that.
I want the boxes to use the gravity to move down, but I never ever want them to change there start x-value.
So, how could I prevent my tower of boxes to fall over or prevent my boxes from moving in x-direction?
EDIT: Posting some code
This code creates on of the boxes, the other one just have a different sprite file.
CCSprite *block = [CCSprite spriteWithFile:#"red.png"];
block.position = ccp(200,380);
[self addChild:block];
//Body definition
b2BodyDef blockDef;
blockDef.type = b2_dynamicBody;
blockDef.position.Set(200/PTM_RATIO, 200/PTM_RATIO);
blockDef.userData = block;
b2Body *blockBody = _world->CreateBody(&blockDef);
//Create the shape
b2PolygonShape blockShape;
blockShape.SetAsBox(block.contentSize.width/PTM_RATIO/2, block.contentSize.height/PTM_RATIO/2);
//Fixture defintion
b2FixtureDef blockFixtureDef;
blockFixtureDef.shape = &blockShape;
blockFixtureDef.restitution = 0.0f;
blockFixtureDef.density = 10.0f;
blockFixtureDef.friction = 10.0f;
_redBlockFixture = blockBody->CreateFixture(&blockFixtureDef);
Nothing fancy.
Regards.
You could setup a 2 (1 pixel wide) walls in box2D to the left and right of the block. Here's some sample code for the left wall. To create the right wall, just copy and past the code and change the variable names and the position of the BodyDef.
// Constant you'll need to define
float wallHeight;
// Create wall body
b2BodyDef wallBodyDef;
wallBodyDef.type = b2_dynamicBody;
wallBodyDef.position.Set(200 - block.contentSize.width/PTM_RATIO/2, 0);
b2Body *wallBody = _world->CreateBody(&wallBodyDef);
// Create wall shape
b2PolygonShape wallShape;
wallShape.SetAsBox(1, wallHeight);
// Create shape definition and add to body
b2FixtureDef wallShapeDef;
wallShapeDef.shape = &wallShape;
wallShapeDef.density = 100.0f;
wallShapeDef.friction = 0.0f;
wallShapeDef.restitution = 0.0f;
b2Fixture *wallFixture = wallBody->CreateFixture(&wallShapeDef);
I solved this problem by adjusting the restitution (bounce) of the static surface upon which the blocks are stacked. For example, if the floor has a restitution of .2, a stack of five blocks will look like they are compressing into each other, and eventually topple:
Set the restitution of the floor to 0, and the blocks stay stacked the way you would expect:

Changing the polygon vertices in a dynamic polygon in cocos2d

I am new to cocos2d and first I learned how to make a circle then a square and now I learned how to create a polygon with an amount of vertices that I pick with a "b2polygonshape polygon" and here is my code for that
-(void) createDynamicPoly:(CGPoint)p;
{
b2BodyDef bodyDefPoly;
bodyDefPoly.type = b2_dynamicBody;
bodyDefPoly.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *polyBody = world->CreateBody(&bodyDefPoly);
int count = 8;
b2Vec2 vertices[8];
vertices[0].Set(10.0f/PTM_RATIO,0.0/PTM_RATIO);
vertices[1].Set(20.0f/PTM_RATIO,0.0f/PTM_RATIO);
vertices[2].Set(30.0f/PTM_RATIO,10.0f/PTM_RATIO);
vertices[3].Set(30.0f/PTM_RATIO,20.0f/PTM_RATIO);
vertices[4].Set(20.0f/PTM_RATIO,30.0f/PTM_RATIO);
vertices[5].Set(10.0f/PTM_RATIO,30.0f/PTM_RATIO);
vertices[6].Set(00.0f/PTM_RATIO,20.0f/PTM_RATIO);
vertices[7].Set(0.0f/PTM_RATIO,10.0f/PTM_RATIO);
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef fixtureDefPoly;
fixtureDefPoly.shape = &polygon;
fixtureDefPoly.density = 1.0f;
fixtureDefPoly.friction = 0.3f;
polyBody->CreateFixture(&fixtureDefPoly);
}
My question is how can I actively change the vertices of this polygon and it change the shape on my screen, without drawing a new shape. My over all goal is to create a free flowing blob.
Thankyou
Modify your last statement to return a pointer to the resulting b2Fixture object. This can be kept as a class variable (ie. b2Fixture* fixture in your class interface).
fixture = polyBody->CreateFixture(&fixtureDefPoly);
Then, wherever you want to change the vertices of the polygon shape, grab a pointer to the shape object associated with your fixture:
b2PolygonShape* shape = (b2PolygonShape*) fixture->GetShape();
And modify the vertices as appropriate:
shape->m_vertices[0].Set(new_x0/PTM_RATIO,new_y0/PTM_RATIO);
shape->m_vertices[1].Set(new_x1/PTM_RATIO,new_y1/PTM_RATIO);
shape->m_vertices[2].Set(new_x2/PTM_RATIO,new_y2/PTM_RATIO);
...
shape->m_vertices[7].Set(new_x7/PTM_RATIO,new_y7/PTM_RATIO);
Good luck!

Box2d multiple fixtures and positioning

I'm attempting to create a "U" shape in Box2d (in Cocos2d) by joining 3 rectangles like so: |_|
It sounds like joints are not the correct solution here since I don't want any movement so I have created a main body which is the middle bit and 2 fixtures for the sides. I've added the two sides to the middle bit like this:
mainBody->CreateFixture(&leftFixtureDef);
mainBody->CreateFixture(&rightFixtureDef);
This works, however both side fixtures get added to the center of the mainBody. I can't seem to work out how to position the fixtures relative to the main body. Attaching a sprite/node to the fixture and changing the position doesn't seem to make a difference.
Any ideas?
Many thanks.
it's the property of a shape. I did not find such property for b2CircleShape, but for b2PolygonShape has m_centroid paramter - it's the shape center coordinates relative to the body. Specify it to have a valid position of a shape.
For b2PolyganShape there is a method setAsBox(w, h) but alos there is more complex one:
setAsBox(float32 width, float32 height, const b2Vec2 &center, float32 rotation)
Use this method or specify the centroid manualy.
Here is the code for the U shape
b2BodyDef bDef;
bDef.type = b2_dynamicBody;
bDef.position = b2Vec2(0, 0);
b2Body *body = world_->CreateBody(&bDef);
b2PolygonShape shape;
const float32 density = 10;
shape.SetAsBox(1, 0.1);
body->CreateFixture(&shape, density);
shape.SetAsBox(0.1, 1, b2Vec2(-1 + 0.1, 1), 0);
body->CreateFixture(&shape, density);
shape.SetAsBox(0.1, 1, b2Vec2(1 - 0.1, 1), 0);
body->CreateFixture(&shape, density);