使用 Box2D、cocos2d 和物理编辑器在视网膜显示中的 PTM_RATIO [英] PTM_RATIO in retina display using Box2D, cocos2d and Physics Editor

查看:25
本文介绍了使用 Box2D、cocos2d 和物理编辑器在视网膜显示中的 PTM_RATIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试在我的精灵上绘制身体形状时遇到了很多问题.我尝试了很多组合,但无法在视网膜显示器上使用.

I’m having a lot of problems trying to draw body shapes on my sprites. I tried a lot of combinations but I’m not able to make it work on retina display.

我只支持 iPhone Retina 和 iPad(Retina 和不 Retina).

I’m supporting iPhone retina only and both iPad (retina and not retina).

A.PTM_RATIO

A. PTM_RATIO

如果我想在所有设备中使用相同的物理特性,第一个问题是每个设备使用哪个 PTM_RATIO.我看了几篇文章,我不知道 PTM 是代表Point To Meter"还是Pixel To Meter".

The first question is which PTM_RATIO use for each device if I want the same physics in all devices. I read several posts and I don’t know if PTM stands for "Point To Meter" or "Pixel To Meter".

¿哪一列是正确的?

B.物理学

我想使用不同形状的不同图像绘制地面,但为此我使用了两种最简单的类型.一张 200x200px 的图片,但 body 只占用了下方 200x80px 的矩形.

I want do draw a ground using different images of different shapes, but for this purpose I’m using two of the simplest type. An image of 200x200px but the body only takes the lower 200x80px rectangle.

为了保持精灵外观,我对 iPhone 和 iPad SD 使用 200x200px 艺术,对 iPad HD 使用 400x400.然后我尝试为红色区域定义一个主体.我使用 Physcs Editor 尝试了所有可能的组合,并尝试使用 Vertex Helper 手动完成.我让它适用于 iPad SD,但不适用于视网膜显示器(总是 x2).

To keep the sprite aspect I use 200x200px art for iPhone and iPad SD and 400x400 for iPad HD. Then I try to define a body for the red area. I tried all possible combinations using Physcs Editor and I tried to do it manually also with Vertex Helper. I made it work for iPad SD but not for retina display (always x2).

我的顶点:

int num = 4;
        b2Vec2 verts[] =
        {
            b2Vec2(-100.0f / 100.0, -20.0f / 100.0),    //A
            b2Vec2(100.0f / 100.0, -20.0f / 100.0), //B
            b2Vec2(100.0f / 100.0, -100.0f / 100.0),    //C
            b2Vec2(-100.0f / 100.0, -100.0f / 100.0),   //D
        };

我除以 100,因为我使用了 200x200 艺术.我试图除以 PTM_RATIO 和 PTM_RATIO * CC_CONTENT_SCALE_FACTOR() 没有有用的结果.我试着用 400x400 的艺术来做……我想我尝试了所有可能的组合.

I divided by 100 because I used the 200x200 art. I tried to divide by PTM_RATIO and by PTM_RATIO * CC_CONTENT_SCALE_FACTOR() without useful results. I tried to do it with the 400x400 art… I think I tried all the possible combinations.

出于调试目的,我使用 cocos2d+Box2d 模板提供的 GLES-Render 类.

For debugging purposes I’m using GLES-Render class provided with cocos2d+Box2d template.

我的图层中的draw方法:

The draw method in my layer:

- (void) draw
{
    [super draw];
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
    kmGLPushMatrix();
    world->DrawDebugData();
    kmGLPopMatrix();
}

我应该使用吗?:

 glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1);

我真的觉得卡住了.

Pysics Editor 组合和结果:

Pysics Editor combinations and results:

宏定义:iPhone 为 50 PTM_RATIO,iPad 2 和 iPad 3 为 100 PTM_RATIO.

Macro definition: 50 PTM_RATIO for iPhone and 100 PTM_RATIO for iPad 2 and iPad 3.

推荐答案

首先:需要 PTM_RATIO:Box2D 使用任意坐标系.据说以米为单位,但这是一个任意定义,因为 Box2D 中的 1 米不能用尺子测量.只有当您将 Box2D 物体的位置转换到屏幕上时,您才能获得可测量的距离.而这个距离取决于屏幕的大小及其像素密度 (ppi).

First things first: the need for PTM_RATIO: Box2D uses an arbitrary coordinate system. It is said to be in meters but this is an arbitrary definition because 1 meter in Box2D can not be measured with a ruler. It is only when you translate the positions of Box2D bodies onto a screen that you get a measurable distance. And that distance depends on the size of the screen and its pixel density (ppi).

对于 Retina,您不需要不同的 PTM_RATIO!

这是因为在 UIKit 和 cocos2d(但不是纯 OpenGL)中,Retina iPhone 与非 Retina iPhone 具有相同的点大小.iPad 和 iPad Retina 也是如此,它们的点坐标大小均为 1024x768 - iPad Retina 在内部仅使用 4 个像素来表示单个点.UIKit 和 cocos2d 都使用点坐标来定位它们的视图,因此您完全不必担心转换为像素或内容比例因子.

This is because in UIKit and cocos2d (but not pure OpenGL) a Retina iPhone has the same point size as a non-Retina iPhone. Same goes for iPad and iPad Retina, they both have a point coordinate size of 1024x768 - internally the iPad Retina simply uses 4 pixels to represent a single point. Both UIKit and cocos2d use point coordinates to position their views so you don't need to worry at all about converting to pixels or the content scale factor.

这意味着对非 Retina 和 Retina 设备使用相同的 PTM_RATIO.

That means use the same PTM_RATIO for non-Retina and Retina devices.

但是,您可能希望在 iPhone 和 iPad 之间更改 PTM_RATIO,例如,如果您不希望 iPad 版本的应用程序显示更大的屏幕部分,而是在场景中提供 2 倍缩放视图,那么您只需在 iPad 上使用两倍的 PTM_RATIO.这意味着在 iPhone 上,其位置时间 PTM_RATIO 转换为 480x320 的主体在 iPad 上将不是 480x320,而是在 960x640 位置,因为 iPad 的 PTM_RATIO 是两倍高.同样,只有当您尝试在 iPhone 和 iPad 屏幕之间使用非常相似的比例(大约大 2 倍)时才是您想要的 - 否则不要更改 PTM_RATIO.

You may however want to change the PTM_RATIO between iPhone and iPad, for example if you don't want the iPad version of your app to show a much larger portion of the screen but instead provide a 2x zoomed view into your scene, then you would simply use twice the PTM_RATIO on iPad. Meaning a body on an iPhone whose position times PTM_RATIO converts to 480x320 will not be at 480x320 on the iPad but rather at position 960x640 because the iPad's PTM_RATIO is twice as high. Again, only if you attempt to use a very similar scale between iPhone and iPad screens (which are roughly 2x bigger) is what you want - otherwise don't change PTM_RATIO.

现在 VertexHelper 并不能帮你解决这个问题,因为它没有考虑或包含 PTM_RATIO,所以它很容易出错(而且它是一个笨重的工具).而是依靠使用 PhysicsEditor 及其形状缓存.确保您已将 PhysicsEditor 中的 PTM_RATIO 设置为您在应用中使用的相同.

Now VertexHelper doesn't help you with this because it doesn't consider or include PTM_RATIO, so its easy to make mistakes with it (beside it's a cumbersome tool). Instead rely on using PhysicsEditor and its shape cache. Make sure you have set the PTM_RATIO in PhysicsEditor to the same you're using in your app.

千万不要在任何地方使用 CC_CONTENT_SCALE_FACTOR 来增加位置.是的,一些 GL 功能需要它,但这些功能已经提供给您,它们可以正常工作,无需任何更改.

By all means don't use CC_CONTENT_SCALE_FACTOR anywhere to multiply positions. Yes, some GL functions need it but those are already given to you and they work properly without requiring any changes.

这篇关于使用 Box2D、cocos2d 和物理编辑器在视网膜显示中的 PTM_RATIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆