Class Box2DMapObjectParser

java.lang.Object
net.dermetfan.gdx.physics.box2d.Box2DMapObjectParser

public class Box2DMapObjectParser
extends java.lang.Object
Parses MapObjects from a Map and generates Box2D Bodies, Fixtures and Joints from them.
Just create a new Box2DMapObjectParser and call load(World, MapLayer) to load all compatible objects (defined by the the aliases) into your World.

If you only want specific Fixtures or Bodies, you can use the createBody(World, MapObject) and createFixture(MapObject) methods.

How you define compatible objects in the TiledMap editor:
In your object layer, right-click an object and set its properties to those of the Body/Fixture/both (in case you're creating an object) you'd like, as defined in the used Box2DMapObjectParser.Aliases object.
For type, you have to choose Box2DMapObjectParser.Aliases.body, Box2DMapObjectParser.Aliases.fixture or Box2DMapObjectParser.Aliases.object.
To add Fixtures to a Body, add a Box2DMapObjectParser.Aliases.body property with the same value to each Fixture of a Body.
To create Joints, add any object to the layer and just put everything needed in its properties. Note that you use the editors unit here which will be converted to Box2D meters automatically using Box2DMapObjectParser.Aliases.unitScale.
For more information read the wiki.
  • Field Details

  • Constructor Details

  • Method Details

    • load

      public com.badlogic.gdx.physics.box2d.World load​(com.badlogic.gdx.physics.box2d.World world, com.badlogic.gdx.maps.Map map)
      creates the given Map's MapObjects in the given World
      Parameters:
      world - the World to create the MapObjects of the given Map in
      map - the Map which MapObjects to create in the given World
      Returns:
      the given World with the parsed MapObjects of the given Map created in it
    • load

      public com.badlogic.gdx.physics.box2d.World load​(com.badlogic.gdx.physics.box2d.World world, com.badlogic.gdx.maps.MapLayer layer)
      creates the given MapLayer's MapObjects in the given World
      Parameters:
      world - the World to create the MapObjects of the given MapLayer in
      layer - the MapLayer which MapObjects to create in the given World
      Returns:
      the given World with the parsed MapObjects of the given MapLayer created in it
    • createObject

      public com.badlogic.gdx.physics.box2d.Body createObject​(com.badlogic.gdx.physics.box2d.World world, com.badlogic.gdx.maps.MapObject object)
      Parameters:
      world - the World in which to create the Body and Fixtures
      object - the MapObject to parse
      Returns:
      the created Body
      See Also:
      createBody(World, MapObject), createFixtures(MapObject)
    • createBody

      public com.badlogic.gdx.physics.box2d.Body createBody​(com.badlogic.gdx.physics.box2d.World world, com.badlogic.gdx.maps.MapObject mapObject)
      creates a Body in the given World from the given MapObject
      Parameters:
      world - the World to create the Body in
      mapObject - the MapObject to parse the Body from
      Returns:
      the Body created in the given World from the given MapObject
    • createFixture

      public com.badlogic.gdx.physics.box2d.Fixture createFixture​(com.badlogic.gdx.maps.MapObject mapObject, com.badlogic.gdx.physics.box2d.Body body)
      creates a Fixture from a MapObject
      Parameters:
      mapObject - the MapObject to parse
      body - the Body to create the Fixtures on
      Returns:
      the parsed Fixture
    • createFixtures

      public com.badlogic.gdx.physics.box2d.Fixture[] createFixtures​(com.badlogic.gdx.maps.MapObject mapObject, com.badlogic.gdx.physics.box2d.Body body)
      creates Fixtures from a MapObject
      Parameters:
      mapObject - the MapObject to parse
      body - the Body to create the Fixtures on
      Returns:
      an array of parsed Fixtures
    • createFixture

      public com.badlogic.gdx.physics.box2d.Fixture createFixture​(com.badlogic.gdx.maps.MapObject mapObject)
      creates the fixture from the given MapObject on the associated body in bodies
      See Also:
      createFixture(MapObject, Body)
    • createFixtures

      public com.badlogic.gdx.physics.box2d.Fixture[] createFixtures​(com.badlogic.gdx.maps.MapObject mapObject)
      creates the fixtures from the given MapObject on the associated body in bodies
      See Also:
      createFixtures(MapObject, Body)
    • transform

      public void transform​(com.badlogic.gdx.math.Matrix4 mat, java.lang.String orientation)
      transforms the given matrix according to the given orientation
      Parameters:
      mat - the matrix to transform
      orientation - the orientation
    • createJoint

      public com.badlogic.gdx.physics.box2d.Joint createJoint​(com.badlogic.gdx.maps.MapObject mapObject)
      creates a Joint from a MapObject
      Parameters:
      mapObject - the Joint to parse
      Returns:
      the parsed Joint
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.BodyDef bodyDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns the given properties to the values of the given BodyDef
      Parameters:
      bodyDef - the BodyDef which values to set according to the given MapProperties
      properties - the MapProperties to assign to the given BodyDef
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.FixtureDef fixtureDef, com.badlogic.gdx.maps.MapProperties properties)
      See Also:
      assignProperties(BodyDef, MapProperties)
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.JointDef jointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns the common properties of all JointDefs
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.DistanceJointDef distanceJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.FrictionJointDef frictionJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.GearJointDef gearJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.MouseJointDef mouseJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef prismaticJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.PulleyJointDef pulleyJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef revoluteJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.RopeJointDef ropeJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.WeldJointDef weldJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • assignProperties

      public void assignProperties​(com.badlogic.gdx.physics.box2d.joints.WheelJointDef wheelJointDef, com.badlogic.gdx.maps.MapProperties properties)
      assigns all properties unique to the given joint definition
    • findAvailableName

      public static java.lang.String findAvailableName​(java.lang.String desiredName, com.badlogic.gdx.utils.ObjectMap<java.lang.String,​?> map)
      Returns:
      the desiredName if it was available, otherwise desiredName with a number appended
    • reset

      public void reset()
      resets all fields to their default values
    • getUnitScale

      public float getUnitScale()
      Returns:
      the unitScale
    • setUnitScale

      public void setUnitScale​(float unitScale)
      Parameters:
      unitScale - the unitScale to set
    • isIgnoreMapUnitScale

      public boolean isIgnoreMapUnitScale()
      Returns:
      the ignoreMapUnitScale
    • setIgnoreMapUnitScale

      public void setIgnoreMapUnitScale​(boolean ignoreMapUnitScale)
      Parameters:
      ignoreMapUnitScale - the ignoreMapUnitScale to set
    • isIgnoreLayerUnitScale

      public boolean isIgnoreLayerUnitScale()
      Returns:
      the ignoreLayerUnitScale
    • setIgnoreLayerUnitScale

      public void setIgnoreLayerUnitScale​(boolean ignoreLayerUnitScale)
      Parameters:
      ignoreLayerUnitScale - the ignoreLayerUnitScale to set
    • getTileWidth

      public float getTileWidth()
      Returns:
      the tileWidth
    • setTileWidth

      public void setTileWidth​(float tileWidth)
      Parameters:
      tileWidth - the tileWidth to set
    • getTileHeight

      public float getTileHeight()
      Returns:
      the tileHeight
    • setTileHeight

      public void setTileHeight​(float tileHeight)
      Parameters:
      tileHeight - the tileHeight to set
    • isTriangulate

      public boolean isTriangulate()
      Returns:
      the triangulate
    • setTriangulate

      public void setTriangulate​(boolean triangulate)
      Parameters:
      triangulate - the triangulate to set
    • getAliases

      public Box2DMapObjectParser.Aliases getAliases()
      Returns:
      the Box2DMapObjectParser.Aliases
    • setAliases

      public void setAliases​(Box2DMapObjectParser.Aliases aliases)
      Parameters:
      aliases - the Box2DMapObjectParser.Aliases to set
    • getListener

      public Box2DMapObjectParser.Listener getListener()
      Returns:
      the listener
    • setListener

      public void setListener​(Box2DMapObjectParser.Listener listener)
      Parameters:
      listener - the listener to set
    • getBodies

      public com.badlogic.gdx.utils.ObjectMap<java.lang.String,​com.badlogic.gdx.physics.box2d.Body> getBodies()
      Returns:
      the parsed bodies
    • getFixtures

      public com.badlogic.gdx.utils.ObjectMap<java.lang.String,​com.badlogic.gdx.physics.box2d.Fixture> getFixtures()
      Returns:
      the parsed fixtures
    • getJoints

      public com.badlogic.gdx.utils.ObjectMap<java.lang.String,​com.badlogic.gdx.physics.box2d.Joint> getJoints()
      Returns:
      the parsed joints
    • getHeritage

      public com.badlogic.gdx.maps.MapProperties getHeritage()
      Returns:
      the heritage
    • setHeritage

      public void setHeritage​(com.badlogic.gdx.maps.MapProperties heritage)
      Parameters:
      heritage - the heritage to set