Question

Recently I published a question regarding this topic and I received a useful answer, but my experimentation points me in a different way that I don’t understamd.

From the answer is clear that we should use the same PTM_RATIO for retina and non-retina devices. However we may double it from iPhone to iPad if we want to show the same portion of the world. In my case I used 50 for iPhone and 100 for iPad because Box2d simulations works better if the bodies are between 0.1 and 10m and the main sprite is about 2m.

I used Physics Editor to build the fixtures using GB2ShapeCache without success for retina devices. Then I decided to feed Box2D coordinates directly and I reached strange conclusions that I would like to clarify.

I created a debug method (independent from any sprite) to draw a single line from 1/3 of screens height to 1/3 of screens wide.

- (void)debugGround
{
    // iPad: 1024x768
    // iPhone: 480x320
    CGSize winSize = [CCDirector sharedDirector].winSize;    // unit is points
    b2EdgeShape groundShape;
    b2FixtureDef groundFixtureDef;
    groundFixtureDef.shape = &groundShape;
    groundFixtureDef.density = 0.0;

    b2Vec2 left = b2Vec2(0, winSize.height/3/PTM_RATIO);
    b2Vec2 right = b2Vec2(winSize.width/3/PTM_RATIO, winSize.height/3/PTM_RATIO);
    groundShape.Set(left, right);
    groundBody->CreateFixture(&groundFixtureDef);
}

If Box2D takes coordinates in points and converts them dividing by PTM_RATIO, the result should be the same for iPhone and iPad retina and non retina.

The result for iPad non retina is as expected:

enter image description here

But for iPhone retina and iPad retina, the fixtures are doubled!!

enter image description here

The most obvious correction should be divide by 2, this means dividing by CC_CONTENT_SCALE_FACTOR.

I managed to make it work for all devices refactoring the code to:

- (void)debugGround
{
    CGSize winSize = [CCDirector sharedDirector].winSize;
    b2EdgeShape groundShape;
    b2FixtureDef groundFixtureDef;
    groundFixtureDef.shape = &groundShape;
    groundFixtureDef.density = 0.0;

    b2Vec2 left = b2Vec2(0, winSize.height/3/PTM_RATIO/CC_CONTENT_SCALE_FACTOR());
    b2Vec2 right = b2Vec2(winSize.width/3/PTM_RATIO/CC_CONTENT_SCALE_FACTOR(), winSize.height/3/PTM_RATIO/CC_CONTENT_SCALE_FACTOR());

    groundShape.Set(left, right);
    groundBody->CreateFixture(&groundFixtureDef);
}

I also managed to display correctly the lower platforms dividing by the scale the vertex, the offsets and anywhere I use PTM_RATIO to convert to Box2D coordinates.

It is supposed I shouldn’t use CC_CONTENT_SCALE_FACTOR by any means to multiply positions because GL functions already take this into consideration.

Can anyone clarify this behavior? In which concepts I’m wrong?

I hope this helps the community to understand better Box2D coordinate system.

Was it helpful?

Solution

you misunderstood: GL functions (this includes ccDraw* functions!) require multiplication with content scale factor because GL works on pixel resolution, whereas UIKit views and cocos2d nodes use point coordinates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top