cocos2d、box2d 和视网膜模式:放弃,还是尝试让它工作?
我一直在尝试让我的 box2d 游戏在视网膜模式下运行,但在更高分辨率模式下运行时遇到了很多恼人的问题。
cocos2d 在视网膜模式下正确渲染我的图形,但我发现需要不断地修改才能使 box2d 与低分辨率模式下的工作方式相同。例如,我发现我需要执行类似的操作才能在视网膜模式下获得正确的调试绘制形状大小:
b2Vec2 vertices[4];
vertices[0].Set(-0.5f / CC_CONTENT_SCALE_FACTOR(), 1.0f / CC_CONTENT_SCALE_FACTOR());
vertices[1].Set(-0.5f / CC_CONTENT_SCALE_FACTOR(), -1.0f / CC_CONTENT_SCALE_FACTOR());
vertices[2].Set(0.5f / CC_CONTENT_SCALE_FACTOR(), -1.0f / CC_CONTENT_SCALE_FACTOR());
vertices[3].Set(0.5f / CC_CONTENT_SCALE_FACTOR(), 1.0f / CC_CONTENT_SCALE_FACTOR());
int32 count = 4;
b2PolygonShape polygon;
polygon.Set(vertices, count);
该 hack(通过 CC_CONTENT_SCALE_FACTOR()
调整所有顶点),当然不可避免地会导致密度黑客,以保持与低分辨率模式类似的运动:
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygon;
fixtureDef.density = 1.0f * CC_CONTENT_SCALE_FACTOR();
fixtureDef.friction = 0.9f;
playerBody->CreateFixture(&fixtureDef);
这会在施加力时导致另一种调整黑客:
b2Vec2 force = b2Vec2(10.0f / CC_CONTENT_SCALE_FACTOR(), 15.0f / CC_CONTENT_SCALE_FACTOR());
playerBody->ApplyLinearImpulse(force, playerBody->GetPosition());
请记住,我通过缩放调试绘制调用在调试模式下进行绘制,如下所示:
glPushMatrix();
glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1.0f);
world->DrawDebugData();
glPopMatrix();
这是什么我从根本上误解了让 box2d 在视网膜模式下工作?我正在使用 Steffen Itterheim 的建议 Box2DHelper 类 代替 PTM_RATIO
,但这似乎还不够。有什么想法吗?此时我即将完全放弃游戏的视网膜模式。
I've been trying to get my box2d game to work in retina mode, and I'm running into a lot of irritating issues when running in higher-resolution mode.
cocos2d renders my graphics correctly in retina mode, but I'm finding the need for hack after hack to get box2d to work the same as in lower-resolution mode. For example, I'm finding I need to do something like this to get the debug draw shape size correct in retina mode:
b2Vec2 vertices[4];
vertices[0].Set(-0.5f / CC_CONTENT_SCALE_FACTOR(), 1.0f / CC_CONTENT_SCALE_FACTOR());
vertices[1].Set(-0.5f / CC_CONTENT_SCALE_FACTOR(), -1.0f / CC_CONTENT_SCALE_FACTOR());
vertices[2].Set(0.5f / CC_CONTENT_SCALE_FACTOR(), -1.0f / CC_CONTENT_SCALE_FACTOR());
vertices[3].Set(0.5f / CC_CONTENT_SCALE_FACTOR(), 1.0f / CC_CONTENT_SCALE_FACTOR());
int32 count = 4;
b2PolygonShape polygon;
polygon.Set(vertices, count);
That hack (adjusting all vertex points by CC_CONTENT_SCALE_FACTOR()
), of course inevitably leads to a density hack, to keep movement similar to lower-resolution mode:
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygon;
fixtureDef.density = 1.0f * CC_CONTENT_SCALE_FACTOR();
fixtureDef.friction = 0.9f;
playerBody->CreateFixture(&fixtureDef);
And that leads to another adjustment hack when applying forces:
b2Vec2 force = b2Vec2(10.0f / CC_CONTENT_SCALE_FACTOR(), 15.0f / CC_CONTENT_SCALE_FACTOR());
playerBody->ApplyLinearImpulse(force, playerBody->GetPosition());
Keep in mind, I'm drawing in debug mode by scaling my debug draw calls, like so:
glPushMatrix();
glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1.0f);
world->DrawDebugData();
glPopMatrix();
What is it that I'm fundamentally misunderstanding about getting box2d to work with retina mode? I'm using Steffen Itterheim's suggested Box2DHelper class in place of PTM_RATIO
, but that just doesn't seem to be enough. Any ideas? I'm about to give up entirely on retina mode for my game at this point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在使用 Cocos2D+Box2D Xcode 模板附带的 GLESDebugDraw 类,并且在视网膜模式下进行调试绘制放大的方式就像这样简单:
我不需要使用
CC_CONTENT_SCALE_FACTOR()
任何其他地方,除了一些显式命名的类方法/属性,如contentSizeInPixels
等,Cocos2D 使用基于点的坐标空间,其中坐标值无论在视网膜模式或低分辨率模式,这意味着相同的PTM_RATIO
可用于在两种模式下创建 Box2D 装置或形状。I'm using the
GLESDebugDraw
class that comes with Cocos2D+Box2D Xcode template and the way I make the debug draw to scale up when in retina mode is just as simple as:I don't need to use
CC_CONTENT_SCALE_FACTOR()
anywhere else since, except in some explicitly named class methods/properties likecontentSizeInPixels
etc, Cocos2D uses point-based coordinate space in which coordinate values remain the same whether in retina mode or low-res mode, which means the samePTM_RATIO
can be used to create Box2D fixtures or shapes in both modes.