我在游戏中注意到我的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比率但在我的情况下并不重要。
谢谢!
EDIT1 :
- (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);
}
EDIT2 :
- (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);
}
答案 0 :(得分:1)
我想到的有两件事情是:
您正在视网膜屏幕上进行测试,而不为精灵和形状设置缩放系数。
尝试使用boundingBox属性而不是contentSize。
在第一种情况下:
使用 - (CC_CONTENT_SCALE_FACTOR() * PTM_RATIO);