Cocos2d, box2d and retina mode: give up or try to get it to work?

I am trying to get my box2d game to work in retina mode, and I am facing a lot of annoying problems when working in higher resolution mode.

cocos2d correctly displays my graphics in retina mode, but I found that hacking after hacking was necessary to make box2d work the same way as in lower resolution mode. For example, I find that I need to do something similar to get the correct size of the debug form 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); 

This hack (setting all vertex points to CC_CONTENT_SCALE_FACTOR() ), of course, inevitably leads to a density hack so that the movement looks like a lower resolution mode:

 b2FixtureDef fixtureDef; fixtureDef.shape = &polygon; fixtureDef.density = 1.0f * CC_CONTENT_SCALE_FACTOR(); fixtureDef.friction = 0.9f; playerBody->CreateFixture(&fixtureDef); 

And this leads to another hack of settings 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 that I draw in debug mode, scaling my draw debug calls, for example:

 glPushMatrix(); glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1.0f); world->DrawDebugData(); glPopMatrix(); 

What am I fundamentally misunderstanding about how box2d works with retina mode? I use the Steffen Itterheim Box2DHelper class instead of PTM_RATIO , but it just doesn't seem to be enough. Any ideas? At the moment, I'm going to completely abandon the retina mode for my game.

+6
source share
1 answer

I use the GLESDebugDraw class that comes with the Xcode Cocos2D + Box2D template, and the way I do a debug draw to scale when in retina mode is as simple as:

 m_debugDraw = new GLESDebugDraw( PTM_RATIO * CC_CONTENT_SCALE_FACTOR() ); world->SetDebugDraw(m_debugDraw); 

I do not need to use CC_CONTENT_SCALE_FACTOR() anywhere else, because, with the exception of some explicitly named methods / properties of the class, such as contentSizeInPixels , etc., Cocos2D uses a point coordinate space in which the coordinate values ​​remain unchanged in retina mode or mode low resolution, which means that the same PTM_RATIO can be used to create Box2D snap-ins or shapes in both modes.

+7
source

Source: https://habr.com/ru/post/887062/


All Articles