PTM_RATIO in retina display using Box2D, cocos2d and Physics Editor

♀尐吖头ヾ 提交于 2019-12-11 09:45:06

问题


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.

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

A. PTM_RATIO

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”.

¿Which column is the right one?

B. Physics

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.

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).

My verts:

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
        };

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.

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

The draw method in my layer:

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

Should I use?:

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

I feel really stuck.

Edit:

Pysics Editor combinations and results:

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


回答1:


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).

As for Retina you don't need a different PTM_RATIO!

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.

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

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.

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.

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.



来源:https://stackoverflow.com/questions/19242708/ptm-ratio-in-retina-display-using-box2d-cocos2d-and-physics-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!