嗨朋友们,我有碰撞圈与弹跳圈体和动力箱体的问题。在我的游戏中,bouncung圆体将触及动能体与一些fource有很多次弹跳圆体不与盒体返回弹跳碰撞。有时会发生碰撞。我搜索并尝试了很多我的知识,但没有结果。 Anybudy给了我这个任务的解决方案。提前谢谢。
CCSprite *_ball = [CCSprite spriteWithFile:@"Red Ball.png" ];
_ball.position = ccp(550, 150);
_ball.tag=100;
[self addChild:_ball];
// Create ball body and shape
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(470/PTM_RATIO, 120/PTM_RATIO);
ballBodyDef.userData = _ball;
_body = world->CreateBody(&ballBodyDef);
b2CircleShape circle;
circle.m_radius = 8.0/PTM_RATIO;
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.2f;
ballShapeDef.restitution = 0.8f;
_body->CreateFixture(&ballShapeDef);
_body->SetBullet(true);
_body->ApplyForce( b2Vec2(-80,20), _body->GetWorldCenter());
//Kinetic box body
CCSprite* oneRun=[CCSprite spriteWithFile:@"1RunImage.png"];
[self addChild:oneRun];
oneRun.tag=1;
// Create wickets body and shape
b2BodyDef fnt1runBodyDef;
fnt1runBodyDef.type = b2_kinematicBody;
fnt1runBodyDef.position.Set(size.width/PTM_RATIO, 30.0f/PTM_RATIO);
fnt1runBodyDef.userData = oneRun;
v1Body = world->CreateBody(&fnt1runBodyDef);
b2PolygonShape run1box;
//circle.m_radius = 13.0/PTM_RATIO;
b2FixtureDef box1ShapeDef;
box1ShapeDef.shape = &run1box;
box1ShapeDef.density = .3f;
run1box.SetAsBox(10.0f/PTM_RATIO,38.0f/PTM_RATIO);
//ballShapeDef.friction = 0.2f;
//box1ShapeDef.restitution = 0.5f;
v1Body->CreateFixture(&box1ShapeDef);
对于碰撞Cantact listner
// Loop through all of the Box2D bodies in our Box2D world..
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
// See if there's any user data attached to the Box2D body
// There should be, since we set it in addBoxBodyForSprite
if (b->GetUserData() != NULL) {
// We know that the user data is a sprite since we set
// it that way, so cast it...
CCSprite *sprite = (CCSprite *)b->GetUserData();
// Convert the Cocos2D position/rotation of the sprite to the Box2D position/rotation
b2Vec2 b2Position = b2Vec2(sprite.position.x/PTM_RATIO,
sprite.position.y/PTM_RATIO);
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
// Update the Box2D position/rotation to match the Cocos2D position/rotation
b->SetTransform(b2Position, b2Angle);
}
}
// Loop through all of the box2d bodies that are currently colliding, that we have
// gathered with our custom contact listener...
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = contactlistener->_contacts.begin(); pos != contactlistener->_contacts.end(); ++pos) {
MyContact contact = *pos;
// Get the box2d bodies for each object
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
// Is sprite A a ball and sprite B a wicket? If so, push the bat on a list to be destroyed...
if (spriteA.tag == 100 && spriteB.tag == 1) {
toDestroy.push_back(bodyA);
}
// Is sprite A a wicket and sprite B a ball? If so, push the bat on a list to be destroyed...
else if (spriteA.tag == 1 && spriteB.tag ==100) {
toDestroy.push_back(bodyB);
score=score+1;
[scoreLable setString:[NSString stringWithFormat:@"Score: %i",score]];
}
}
}
// Loop through all of the box2d bodies we wnat to destroy...
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
// See if there's any user data attached to the Box2D body
// There should be, since we set it in addBoxBodyForSprite
if (body->GetUserData() != NULL) {
// We know that the user data is a sprite since we set
// it that way, so cast it...
CCSprite *sprite = (CCSprite *) body->GetUserData();
// Remove the sprite from the scene
[self removeChild:sprite cleanup:YES];
}
// Destroy the Box2D body as well
world->DestroyBody(body);
// [self unschedule:@selector(ballCreation)];
}
// If we've destroyed anything, play an amusing and malicious sound effect! ;]
if (toDestroy.size() > 0) {
// [[SimpleAudioEngine sharedEngine] playEffect:@"hahaha.caf"];
}