Fit world to iPhone screen?

Hi,
This is the first time using Box2D, so please forgive me if I may not fully understand some concepts.

I'm trying to fit my world to the iPhone screen of 320x480, but I'm having some problems.

I'm creating my world boundaries using the following code:

Code: Select all

#define PTM_RATIO 32.0f

 // 320x480
 CGSize screenSize = self.view.bounds.size;
   
 b2BodyDef groundBodyDef;
 groundBodyDef.position.Set(0, 0); // bottom-left corner
   
 b2Body* groundBody = world->CreateBody(&groundBodyDef);
 b2EdgeShape groundBox;
   
 // bottom
 groundBox.Set(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
 groundBody->CreateFixture(&groundBox, 0);
   
 // top
 groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
 groundBody->CreateFixture(&groundBox, 0);
   
 // left
 groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
 groundBody->CreateFixture(&groundBox, 0);
   
 // right
 groundBox.Set(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
 groundBody->CreateFixture(&groundBox, 0);



However, when viewing it on the iPhone simulator, the world seems be offset from the UL corner
about 200 pixels or so:

Screenshot:
http://imageshack.us/photo/my-images/69/box2diphone.png
Image

Changing PTM_RATIO does not help. The offset still stays in the same place.

How can I fit my Box2D world to the iPhone boundaries of 320x480?
Thanks.
This code doesn't show us how you are specifying what part of the world should be shown... is this cocos2d? You might find more help at the cocos2d forums for this.
irresistible force wrote:This code doesn't show us how you are specifying what part of the world should be shown... is this cocos2d? You might find more help at the cocos2d forums for this.


Thanks, I'm just using Box2D and the iOS framework. Not using Cocos2D, OpenGL, or any other libraries.

But, I found my problem. I was using the coords from Box2D to set my object's frame.origin,
where I should have been setting the object's center. This skewed everything.
Perhaps have a read of this tutorial
timbo wrote:Perhaps have a read of this tutorial


That's a tutorial for Cocos2D, so, unfortunately, it wouldn't have helped. I'm not using any library.

The problem is the Box2D docs don't adequately explain how relative/world coordinates are setup for
shape positions, body positions, etc...

For iOS, I haven't come across anything that explains why I should be using the center property
instead of the frame.origin property for Box2D body positions.
How do you get coordinates for the world from screen?

My, oh sorry Cocos2D, code is:

Code: Select all

       
CGSize screenSize = [CCDirector sharedDirector].winSize;

self.screenWidthB2D = screenSize.width/PTM_RATIO;
self.screenHeightB2D = screenSize.height/PTM_RATIO;


So, the only Cocos code here is getting window size of shared director instance. But you can get winsize by yourself, do you? May be here is the clue? Try to evaluate all variables in debugger.
where can I find some useful and adequate docs then????

App Developers Brisbane

Original thread: https://box2d.org/forum/viewtopic.php?t=7888