I am new in Box2d and learning myself. I am learning Box2d from here
I want to make a body and want to give b2body a angle so that it will look like body is stand like skew line, like slope.
This is my code.
for(int i = 0; i < 4; i++) {
static int padding=20;
// Create block and add it to the layer
CCSprite *block = [CCSprite spriteWithFile:@"slope.png"];
int xOffset = padding+block.contentSize.width/2+ ((block.contentSize.width+padding)*i);
block.position = ccp(xOffset, 250);
block.tag = 2;
[self addChild:block];
// Create block body
b2BodyDef blockBodyDef;
blockBodyDef.type = b2_staticBody; //b2_dynamicBody
blockBodyDef.position.Set(xOffset/PTM_RATIO, 250/PTM_RATIO);
blockBodyDef.userData = block;
b2Body *blockBody = _world->CreateBody(&blockBodyDef);
// Create block shape
b2PolygonShape blockShape;
blockShape.SetAsBox(block.contentSize.width/PTM_RATIO/2,
block.contentSize.height/PTM_RATIO/2);
// Create shape definition and add to body
b2FixtureDef blockShapeDef;
blockShapeDef.shape = &blockShape;
blockShapeDef.density = 10.0;
blockShapeDef.friction = 0.0;
blockShapeDef.restitution = 0.1f;
blockBody->CreateFixture(&blockShapeDef);
}
In this code I made 4 b2body horizontically. Now I want these in vertical and slightly skew, like a slope.
I am not able to do this yet.
I dont know I have to give angel to the body or something else. Not able to find sample code and good tutorials of Box2d.
Is there any site available where I can find some xcode Box2d demo for beginners? Or some code like applications we have UIButtons, UILabel, UITextfiled programatically, can I find Box2d codes like this?
In this demo I am trying to make 4 static slope image and from the left upper side corner I have to push ball(b2dynamicbody). The ball will go through these 4 static body and the ball will turn according to the slope.
Any Idea or suggestions would be highly welcome.
Replace this code in your sample and get a sloppy body
// Create block body
b2BodyDef blockBodyDef;
blockBodyDef.type = b2_staticBody; //b2_dynamicBody
blockBodyDef.position.Set(200/PTM_RATIO, 250/PTM_RATIO);
blockBodyDef.userData = block;
b2Body *blockBody = _world->CreateBody(&blockBodyDef);
// Create block shape
b2PolygonShape blockShape;
int num = 3;
b2Vec2 verts[] = {
b2Vec2(-27.5f / PTM_RATIO, -296.6f / PTM_RATIO),
b2Vec2(-14.3f / PTM_RATIO, -127.2f / PTM_RATIO),
b2Vec2(-281.0f / PTM_RATIO, -288.4f / PTM_RATIO)
};
blockShape.Set(verts, num);
// Create shape definition and add to body
b2FixtureDef blockShapeDef;
blockShapeDef.shape = &blockShape;
blockShapeDef.density = 10.0;
blockShapeDef.friction = 0.0;
blockShapeDef.restitution = 0.1f;
blockBody->CreateFixture(&blockShapeDef);
Good luck, feel free to ask your queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.