Skip to content

simdm/box2demo

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
Basic Box2D Demo iOS App

Based on this tutorial: http://www.cocoanetics.com/2010/05/physics-101-uikit-app-with-box2d-for-gravity
But, hacked to work on iOS 4.3 and Xcode 4.2 with ARC.
Should be deployable on iOS 4.3 and 5.0.

What I Did:
- Read the Cocoanetics tutorial listed above.
- Download Box2D (the version I used is in the Box2D directory of this Github repo).
- Add the Box2D directory to this project (I copied the files in and selected the dir).
- Add Box2D to the project's "Header Search Paths," by adding and entry for "${PROJECT_DIR}" and be checking the "Recursive" checkbox.
- Also, while you're looking at the project properties, make sure "Always Search User Paths" is checked.
- Change the Xcode "File Type" for any files that C++ (Box2D is written in C++) or include the Box2D header.

I changed my .m files to "Objective-C++ source," which is a listed, but not recommended, by the tutorial.  See the tutorial for other options.  The following files were changed:
- AppDelegate.m
- ViewController.m

NOTE: The I changed these .m files because they include the ViewController.h header file.  This is the header file in which I added the #include <Box2D/Box2D.h> line.  In your app, make sure you change the file types of any .m files which include similar (Box2D-including) headers.  If you fail to do so, you will not get a friendly warning.  In this case, you'll probably get a message from the compiler saying it can't locate things you don't recognize (but having to do with Box2D, like cassert, etc.)

Also, if your application delegate .m file doesn't include the view controller .h file where you're including Box2D, there is no need to change its file type.

If you don't know where to find the File Type in Xcode:
"File Type" is listed under the "Identity and Type" accordion section of the "File Inspector" for each file.  To find it for a file, click on the file in the project navigator, then in the right hand side "Utilities" area, click on the "File Inspector" tab (looks like a document).  Expand the "Identity and Type" section (if not already expanded).  You should now see the "File Type" drop down list.

- I replaced the use of the demo's NSTimer to CADisplayLink.  In order to use this, you must link the QuartzCore framework.
- Build and run.  You should be error free.


Notes on the tutorial:
- In createPhysicsWorld, change these lines:
    // Construct a world object, which will hold and simulate the rigid bodies.
    world = new b2World(gravity, doSleep);

  To this:
    // Construct a world object, which will hold and simulate the rigid bodies.
    world = new b2World(gravity);
    world->SetAllowSleeping(doSleep);
  
- In addPhysicalBodyForView:physicalView, change this line:
  	bodyDef.userData = physicalView;
  	
  To this:
  	bodyDef.userData = (__bridge void *) physicalView;

- In tick:timer, change this line:
    UIView *oneView = (UIView *)b->GetUserData();
    
  To this:
		UIView *oneView = (__bridge UIView *)b->GetUserData();
		
- I don't recommend creating the Box2D world in viewDidLoad.  I recommend adding it later (like in viewWillAppear, or later).  This is because the size of the main view might change, for example due to the addition of a navigation bar.

- I also recommend removing the hard coded screen height in addPhysicalBodyForView:physicalView, by changing this line:
    bodyDef.position.Set(p.x/PTM_RATIO, (460.0 - p.y)/PTM_RATIO);

  To this:
    bodyDef.position.Set(p.x/PTM_RATIO,
                           (CGRectGetHeight(self.view.bounds) - p.y)/PTM_RATIO);

About

Box2D iOS Demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published