我在游戏中注意到,我的b2Bodies与CCSprite的大小不一样。
这是我正在使用的代码:

- (void)addBoxBodyForSprite:(CCSprite *)sprite {
    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(sprite.position.x/CTM_RATIO, sprite.position.y/CTM_RATIO);
    spriteBodyDef.userData = sprite;
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef);
    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.contentSize.width/CTM_RATIO/2, sprite.contentSize.height/CTM_RATIO/2);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteBody->CreateFixture(&spriteShapeDef);
}


我将如何编辑该代码以完全忽略CTM(PTM)比率,而大小恰好等于CCSprite?我知道我应该使用PTM比率,但就我而言,这并不重要。

谢谢!

编辑1:

- (void)addBoxBodyForSprite:(CCSprite *)sprite {
    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(sprite.position.x/(CC_CONTENT_SCALE_FACTOR() * CTM_RATIO), sprite.position.y/(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO));
    spriteBodyDef.userData = sprite;
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef);
    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.boundingBox.size.width/(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO)/2, sprite.boundingBox.size.height/(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO)/2);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteBody->CreateFixture(&spriteShapeDef);
}


编辑2:

- (void)addBoxBodyForSprite:(CCSprite *)sprite {
    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(sprite.position.x/CTM_RATIO, sprite.position.y/CTM_RATIO);
    spriteBodyDef.userData = sprite;
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef);
    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.boundingBox.size.width/CTM_RATIO, sprite.boundingBox.size.height/CTM_RATIO);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteBody->CreateFixture(&spriteShapeDef);
}

最佳答案

我想到的两件事是:


您正在视网膜屏幕上对其进行测试,而未设置精灵和形状的缩放比例。
尝试使用boundingBox属性而不是contentSize。


在第一种情况下:

使用-(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO);

关于ios - b2Body与CCSprite的大小不同吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8876082/

10-11 14:34