b2Body 与 CCSprite 大小不一样?

发布于 2024-12-27 07:53:09 字数 2182 浏览 0 评论 0原文

我注意到在我的游戏中,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2025-01-03 07:53:09

我想到两件事:

  1. 您正在视网膜屏幕上测试它,而没有设置精灵和形状的缩放因子。

  2. 尝试使用boundingBox属性而不是contentSize。

在第一种情况下:

使用 - (CC_CONTENT_SCALE_FACTOR() * PTM_RATIO);

There are two things coming to my mind is :

  1. You are testing it on retina screen without setting the scaling factor for your sprite and shape.

  2. Try to use boundingBox property instead of contentSize.

in First case :

use - (CC_CONTENT_SCALE_FACTOR() * PTM_RATIO);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文