Reputation: 1337
I basically have a rectangular object (like a spear) and I have a ground body. The problem is, when the spear hits the ground, it doesn't bounce back, it falls though and off the screen. Here my physics setup: (Ignore the ball reference, it suppose to be called spear (rectangular))
-(id) init {
if( (self=[super init])) {
CGSize winSize = [CCDirector sharedDirector].winSize;
self.isAccelerometerEnabled = YES;
self.isTouchEnabled = YES;
// Create sprite and add it to the layer
_ball = [CCSprite spriteWithFile:@"SPEAR.png" rect:CGRectMake(0, 0, 100, 10)];
_ball.position = ccp(100, 100);
[self addChild:_ball];
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, -20.0f);
bool doSleep = true;
_world = new b2World(gravity, doSleep);
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = _world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
// Create ball body and shape
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
ballBodyDef.userData = _ball;
b2Body *_body = _world->CreateBody(&ballBodyDef);
b2PolygonShape spearShape;
spearShape.SetAsBox(100/PTM_RATIO, 10/PTM_RATIO);
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &spearShape;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.9f;
ballShapeDef.restitution = 0.89f;
b2Vec2 force;
force.Set(_body->GetLinearVelocity().x+5.0f, _body->GetLinearVelocity().y+10.0f);
_body->SetLinearVelocity(force);
[self schedule:@selector(tick:)];
}
return self;
}
- (void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *ballData = (CCSprite *)b->GetUserData();
ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
Upvotes: 2
Views: 1067
Reputation: 1054
I think only the fixtures are colliding in box2d. But your spear has no fixture. You only create the fixture def but not the fixture itself.
after setting restitution to you fixtureDef, add this line:
_body->CreateFixture(&ballShapeDef);
Everything should then be fine !
A bit late I know..
Upvotes: 1
Reputation: 24846
I don't see anywhere in your code attaching the shapes to ball and to spear. If the body does not have a shape - it will not collide.
Upvotes: 1
Reputation: 8262
Edge fixtures do not collide with each other in Box2D. Make either the ground or the spear a polygon.
Upvotes: 0
Reputation: 18488
I haven't used cocos2d in a while, but I remember you needed to create an space an add the objects to it, so they were aware of each other.
Upvotes: 0