Skip to content

hemantasapkota/ofxPhysicsEditorBox2D

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ofxPhysicsEditorBox2D

OpenFrameworks addon for loading fixtures generated by Physics Editor(https://www.codeandweb.com/physicseditor).

Features

  • Attach polygons and circles
  • Debug draw facility of all fixtures

Dependencies

Output

The following image shows the Physics Editor file with all the fixtures. The annotated image is concerned with the following:

  • 1 - Shape name: Corresponds to a Box2D Body. May conatain many fixtures.
  • 2 - Anchor Point: This relative position is taken into account while initializing the positions of the bodies.
  • 3 - Fixtures: Polygons and Circles.
  • 4 - PTM Ratio: Pixel-To-Meters ratio. This addon works best with PTM = 30.
  • 5 - Export Format: This addon works with Box2D Generic (PLIST).

Input

The following screenshot was taken from iPAD Mini Retina.

Output

Usage

  • Init Box2D
  box2d.init();
  box2d.setGravity(0, 10);
  box2d.setFPS(60.0);
  • Create a Body
  b2BodyDef bodydef;
  bodydef.type = b2_staticBody; //could be static, dynamic or kinematic
  gameArea = box2d.getWorld()->CreateBody(&bodydef);
  • Load fixture file
    NSString *file = [[NSString alloc] initWithCString:"images/bgFixture.plist"
                                                encoding:NSUTF8StringEncoding];
    [[GB2ShapeCache sharedShapeCache]
    		            //the file exported by Physics Editor
                    addShapesWithFile:file       
                    
                    //With default anchor point, we need to flip the y-coordinate  
                    screenHeight:ofGetHeight()  
                    
                    //Scale Factor
                    //1 means the normal size as designed on the editor
                    //< 1 means the fixtures are shrunk
                    //> 1 means the fixtures are enlarged
                    scaleFactor:1];             
                                     
                         
    [[GB2ShapeCache sharedShapeCache] 
                    addFixturesToBody:gameArea 
                    forShapeName:@"gameArea"];
    
  • Debug Draw Fixtures
  void ofApp::draw() {
      ofPushStyle();
      ofSetLineWidth(3);
      [[GB2ShapeCache sharedShapeCache] drawFixtures];
      ofPopStyle();
  }

See Example for more details.

To-Do

  • Debug Draw support for multiple bodies

About

Follow me on twitter: @hemantasapkota and don't forget to star this project.

About

Load fixtures generated by Physics Editor (https://www.codeandweb.com/physicseditor) in OpenFrameworks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published