b2Body 与 CCSprite 大小不一样?
我注意到在我的游戏中,我的 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);
}
I have noticed in my game that my b2Bodies are not the same size as my CCSprite's.
This is the code I am using:
- (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);
}
How would I edit that code to just ignore the CTM (PTM) Ratio all together and just be the exact size as the CCSprite? I know I am supposed to use a PTM Ratio but in my case it does not matter.
Thanks!
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到两件事:
您正在视网膜屏幕上测试它,而没有设置精灵和形状的缩放因子。
尝试使用boundingBox属性而不是contentSize。
在第一种情况下:
使用 -
(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO);
There are two things coming to my mind is :
You are testing it on retina screen without setting the scaling factor for your sprite and shape.
Try to use boundingBox property instead of contentSize.
in First case :
use -
(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO);