Box2D Forums

It is currently Thu Jul 24, 2014 9:57 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sat Nov 26, 2011 1:14 pm 
Offline

Joined: Sat Nov 26, 2011 12:36 pm
Posts: 11
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:
#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.


Top
 Profile  
 
PostPosted: Sat Nov 26, 2011 7:45 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1901
Location: Tokyo
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.


Top
 Profile  
 
PostPosted: Sat Nov 26, 2011 8:11 pm 
Offline

Joined: Sat Nov 26, 2011 12:36 pm
Posts: 11
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.


Top
 Profile  
 
PostPosted: Tue Dec 06, 2011 2:52 am 
Offline

Joined: Tue Dec 06, 2011 2:43 am
Posts: 6
Perhaps have a read of this tutorial


Top
 Profile  
 
PostPosted: Tue Dec 06, 2011 9:32 pm 
Offline

Joined: Sat Nov 26, 2011 12:36 pm
Posts: 11
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.


Top
 Profile  
 
PostPosted: Thu Jan 19, 2012 12:04 am 
Offline

Joined: Sat Feb 06, 2010 10:55 am
Posts: 37
How do you get coordinates for the world from screen?

My, oh sorry Cocos2D, code is:
Code:
       
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.


Top
 Profile  
 
PostPosted: Sun Feb 19, 2012 1:32 pm 
Offline

Joined: Sun Feb 19, 2012 1:20 pm
Posts: 4
where can I find some useful and adequate docs then????

App Developers Brisbane


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group