Fit world to iPhone screen?

Discuss issues specific to using Box2D on the iPhone
gerald
Posts: 11
Joined: Sat Nov 26, 2011 12:36 pm

Fit world to iPhone screen?

Postby gerald » Sat Nov 26, 2011 1:14 pm

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.

irresistible force
Posts: 1988
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Fit world to iPhone screen?

Postby irresistible force » Sat Nov 26, 2011 7:45 pm

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.

gerald
Posts: 11
Joined: Sat Nov 26, 2011 12:36 pm

Re: Fit world to iPhone screen?

Postby gerald » Sat Nov 26, 2011 8:11 pm

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.

timbo
Posts: 6
Joined: Tue Dec 06, 2011 2:43 am

Re: Fit world to iPhone screen?

Postby timbo » Tue Dec 06, 2011 2:52 am

Perhaps have a read of this tutorial

gerald
Posts: 11
Joined: Sat Nov 26, 2011 12:36 pm

Re: Fit world to iPhone screen?

Postby gerald » Tue Dec 06, 2011 9:32 pm

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.

maaboo
Posts: 37
Joined: Sat Feb 06, 2010 10:55 am

Re: Fit world to iPhone screen?

Postby maaboo » Thu Jan 19, 2012 12:04 am

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.

Alstair
Posts: 4
Joined: Sun Feb 19, 2012 1:20 pm

Re: Fit world to iPhone screen?

Postby Alstair » Sun Feb 19, 2012 1:32 pm

where can I find some useful and adequate docs then????

App Developers Brisbane


Return to “iPhone”



Who is online

Users browsing this forum: No registered users and 0 guests