I have two objects...One static and one dynamic (it is a ball). Static object have polygon shape. Sommethimes when I hit the ball, ball passes through static object :(. I have played with density and other properties, but with no success. Do somebody knows how i can make static body to be impenetrable.
Here is my code
// Create ball body
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(110 / 2 / PTM_RATIO, 300 / PTM_RATIO);
ballBodyDef.userData = playerCartoonSprite;
ballBodyDef.bullet = YES;
playerCartoonBody = localWorld->CreateBody(&ballBodyDef);
// Create circle shape
b2CircleShape circle;
circle.m_radius = 10.0 / PTM_RATIO;
// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 0.0f;
ballShapeDef.friction = 0.3f;
ballShapeDef.restitution = 1.f;
playerCartoonFixture = playerCartoonBody->CreateFixture(&ballShapeDef);
// static polygon object
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
int num = 6;
b2Vec2 verts[] = {
b2Vec2(8.6f / PTM_RATIO, 89.3f / PTM_RATIO),
b2Vec2(5.0f / PTM_RATIO, 81.7f / PTM_RATIO),
b2Vec2(10.5f / PTM_RATIO, 61.9f / PTM_RATIO),
b2Vec2(13.1f / PTM_RATIO, 9.1f / PTM_RATIO),
b2Vec2(17.3f / PTM_RATIO, 9.9f / PTM_RATIO),
b2Vec2(12.7f / PTM_RATIO, 70.6f / PTM_RATIO)
};
b2PolygonShape boxShape;
boxShape.Set(verts, num);
b2FixtureDef fixtureDef;
fixtureDef.shape = &boxShape;
bodyDef.position.Set(teeterSprite.position.x / PTM_RATIO, teeterSprite.position.y / PTM_RATIO);
fixtureDef.shape = &boxShape;
m_bodyA = world->CreateBody( &bodyDef );
m_bodyA->CreateFixture( &fixtureDef );
I think this could be solution for my problem http://www.emanueleferonato.com/2008/12/19/understanding-custom-polygons-in-box2d/
Related
lately I started playing with box2d and tried abstracting it to class
RigidBody::RigidBody() {
}
RigidBody::RigidBody(const RigidBody& other) {
m_fixture = other.m_fixture;
b2BodyDef bodyDef;
bodyDef.position = other.m_body->GetPosition();
bodyDef.type = other.m_body->GetType();
m_body = Runtime::PhysicsWorld.CreateBody(&bodyDef);
b2FixtureDef fixtureDef;
fixtureDef.shape = m_fixture->GetShape();
fixtureDef.density = m_fixture->GetDensity();
fixtureDef.friction = m_fixture->GetFriction();
fixtureDef.restitution = m_fixture->GetRestitution();
fixtureDef.restitutionThreshold = m_fixture->GetRestitutionThreshold();
m_fixture = m_body->CreateFixture(&fixtureDef);
}
RigidBody::RigidBody(sf::Vector2f pos, BodyType type) {
pos /= Constants::PPM;
b2BodyDef bodyDef;
bodyDef.position = pos;
bodyDef.type = (b2BodyType)type;
m_body = Runtime::PhysicsWorld.CreateBody(&bodyDef);
sf::Vector2f size(50.0f, 50.0f);
size /= 2.0f;
size /= Constants::PPM;
b2PolygonShape shape;
shape.SetAsBox(size.x, size.y);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.5f;
fixtureDef.restitution = 0.0f;
fixtureDef.restitutionThreshold = 0.5f;
m_body->CreateFixture(&fixtureDef);
}
RigidBody::~RigidBody() {
Runtime::PhysicsWorld.DestroyBody(m_body);
}
but vector is behaving really weird with it i know it's probably becasue of copy constructor or destructor but I can't figure this out
std::vector<hv::RigidBody> m_Bodies;
the problem is with vector erase when I call m_Bodies.erase(m_Bodies.begin()) for some reason it deletes the last objects
m_Bodies.erase(m_Bodies.begin());
And after I call m_Bodies.erase(m_Bodies.begin()) second time I get this
Also it doesn't matter how many objects is in vector if I call m_Bodies.erase(m_Bodies.begin() + 3) it will always delete the last one
*edit corrected question
The problem was I didn't define operator =
resolved it by
RigidBody* RigidBody::operator=(const RigidBody& other) {
if(m_body)
Runtime::PhysicsWorld.DestroyBody(m_body);
m_fixture = other.m_fixture;
b2BodyDef bodyDef;
bodyDef.position = other.m_body->GetPosition();
bodyDef.type = other.m_body->GetType();
m_body = Runtime::PhysicsWorld.CreateBody(&bodyDef);
b2FixtureDef fixtureDef;
fixtureDef.shape = m_fixture->GetShape();
fixtureDef.density = m_fixture->GetDensity();
fixtureDef.friction = m_fixture->GetFriction();
fixtureDef.restitution = m_fixture->GetRestitution();
fixtureDef.restitutionThreshold = m_fixture->GetRestitutionThreshold();
m_fixture = m_body->CreateFixture(&fixtureDef);
return this;
}
I'm using the following to create sprites with b2bodys
but I cant get the sprites tag to set. why isn't [sprite setTag:3]; working?
When I detect a collision with one of these sprites it says the sprite tag is 0
-(void) addNewSpriteAtPosition:(CGPoint)p
{
// CCLOG(#"Add sprite %0.2f x %02.f",p.x,p.y);
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
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 = 2;
fixtureDef.friction = 0.2f;
body->CreateFixture(&fixtureDef);
CCNode *parent = [self getChildByTag:kTagParentNode];
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
int idx = (CCRANDOM_0_1() > .5 ? 0:1);
int idy = (CCRANDOM_0_1() > .5 ? 0:1);
CCPhysicsSprite *sprite = [CCPhysicsSprite spriteWithTexture:spriteTexture_ rect:CGRectMake(32 * idx,32 * idy,32,32)];
[parent addChild:sprite];
[sprite setPTMRatio:PTM_RATIO];
[sprite setB2Body:body];
[sprite setPosition: ccp( p.x, p.y)];
[sprite setTag:3];
}
In the game I'm trying to make, I have a ball sprite which bounces thanks to box2d. Here's how my current code looks:
-(id)init
{
ball = [CCSprite spriteWithFile:#"ball.png"];
ball.position = ccp(150, winSize.height * 0.78);
[self addChild:ball];
ball.tag = 2;
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(150/PTM_RATIO, 450/PTM_RATIO);
ballBodyDef.userData = ball;
_body = _world->CreateBody(&ballBodyDef);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 0.5f;
ballShapeDef.friction = 1.0f;
ballShapeDef.restitution = 1.0f;
_ballFixture = _body->CreateFixture(&ballShapeDef);
b2Vec2 force = b2Vec2(160, 375);
_body->ApplyLinearImpulse(force, ballBodyDef.position);}
- (void)update:(ccTime) dt {
if(_isPaused == FALSE)
{
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if(sprite.tag == 2)
{
sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}}
Bouncing itself works fine, my problem is there are instances wherein the ball would bounce on a straight line so to speak, either vertically or horizontally continuously which I am trying to avoid. So my question is, how can I make my ball sprite bounce at an angle instead of a straight line so it wouldn't get stuck bouncing infinitely in the same direction?
You could apply a tiny force or gravity change to the body or the world, "randomly" or at equal intervals.
I am trying to test/create a sample game using Cocos2d 2.0 and box2d. I have a bunch of sprites on the screen and when I press the Sprite, I want a body to be automatically attached to that Sprite. I tried to use the TouchesEnd method but it doesn't seem to work.
Can someone push me in the right direction?
Try this way...
-(void)createB2Body
{
b2PolygonShape shape;
float xDist = (sprite.contentSize.width*0.5f)/PTM_RATIO ;
float yDist = (sprite.contentSize.height*0.5f)/PTM_RATIO ;
shape.SetAsBox(xDist, yDist);
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.userData = sprite;
bd.linearDamping = 0.5f;
bd.angularDamping = 0.5f;
bd.position.Set(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
b2FixtureDef fixDef;
fixDef.shape = &shape;
fixDef.density = 1.0f;
fixDef.friction = 0.1f;
fixDef.restitution = 1.0f;
fixDef.isSensor = true;
self.body = self.world->CreateBody(&bd);
self.body->CreateFixture(&fixDef);
}
Only on touch? then use ccTouchesBegan.
Can you help. Want to draw a polygon (beams at different angles) and apply box 2d body to it. Can you please let me know how to create a CCSprite with a polygon shape
Any examples would help
Cheers
Create Polygon body.
-(void) createDynamicPoly {
b2BodyDef bodyDefPoly;
bodyDefPoly.type = b2_dynamicBody;
bodyDefPoly.position.Set(3.0f, 10.0f);
b2Body *polyBody = world->CreateBody(&bodyDefPoly);
int count = 8;
b2Vec2 vertices[8];
vertices[0].Set(0.0f / PTM_RATIO,0.0f / PTM_RATIO);
vertices[1].Set(48.0f/PTM_RATIO,0.0f/PTM_RATIO);
vertices[2].Set(48.0f/PTM_RATIO,30.0f/PTM_RATIO);
vertices[3].Set(42.0f/PTM_RATIO,30.0f/PTM_RATIO);
vertices[4].Set(30.0f/PTM_RATIO,18.0f/PTM_RATIO);
vertices[5].Set(18.0f/PTM_RATIO,12.0f/PTM_RATIO);
vertices[6].Set(6.0f/PTM_RATIO,18.0f/PTM_RATIO);
vertices[7].Set(0.0f/PTM_RATIO,30.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);
}
Create your sprite
Attach your sprite to the Polygon body via Fixture and UserData
fixtureDefPoly.SetUserData() = spriteObject;
b2Fixture *fixture;
fixture = circleBody->CreateFixture(&fixtureDefPoly);
fixture->SetUserData(#"spriteObject");
Then Iterate the sprite to the body in your update method.
The easiest way is to open an image editor (such as paint for example or photoshop) and create the image you want. The use it in your program.
Also there is a helloWorld scene when creating an xcode application using cocos2d box2d template. It creates a set of squares with a texture.
CGPoint startPt = edge.start ;
CGPoint endpt = edge.end ;
//length of the stick body
float len = abs(ccpDistance(startPt, endpt))/PTM_RATIO;
//to calculate the angle and position of the body.
float dx = endpt.x-startPt.x;
float dy = endpt.y-startPt.y;
//position of the body
float xPos = startPt.x+dx/2.0f;
float yPos = startPt.y+dy/2.0f;
//width of the body.
float width = 1.0f/PTM_RATIO;
b2BodyDef bodyDef;
bodyDef.position.Set(xPos/PTM_RATIO, yPos/PTM_RATIO);
bodyDef.angle = atan(dy/dx);
NSLog([NSString stringWithFormat:#"Setting angle %f",bodyDef.angle]);
CCSprite *sp = [CCSprite spriteWithFile:#"material-wood.png" rect:CGRectMake(0, 0, 12, 12)];
//TODO: fix shape
[self addChild:sp z:1 ];
bodyDef.userData = sp;
bodyDef.type = b2_dynamicBody;
b2Body* body = world->CreateBody(&bodyDef);
b2PolygonShape shape;
b2Vec2 rectangle1_vertices[4];
rectangle1_vertices[0].Set(-len/2, -width/2);
rectangle1_vertices[1].Set(len/2, -width/2);
rectangle1_vertices[2].Set(len/2, width/2);
rectangle1_vertices[3].Set(-len/2, width/2);
shape.Set(rectangle1_vertices, 4);
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 1.0f;
fd.friction = 0.300000f;
fd.restitution = 0.600000f;
body->CreateFixture(&fd);