/** * Creates polygon ground from the given object, at the given world. * * @param world The world were the ground will be. * @param object The object used to initialize the ground. * @return The body of the created ground. */ static Body makePolygonGround(World world, PolygonMapObject object) { Polygon polygon = object.getPolygon(); // Body and Fixture variables BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set(PIXEL_TO_METER * polygon.getX(), PIXEL_TO_METER * polygon.getY()); Body body = world.createBody(bdef); float[] new_vertices = polygon.getVertices().clone(); for (int i = 0; i < new_vertices.length; i++) new_vertices[i] *= PIXEL_TO_METER; shape.set(new_vertices); fdef.shape = shape; fdef.filter.categoryBits = GROUND_BIT; body.createFixture(fdef); return body; }
private void processFreeBodies(Array<MapObject> bodies) { for (MapObject object : bodies) { FixtureBodyDefinition fixtureBodyDefinition; if (object instanceof RectangleMapObject) { fixtureBodyDefinition = TiledUtils.createRectangleFixtureBodyDef((RectangleMapObject)object); } else if (object instanceof CircleMapObject) { fixtureBodyDefinition = TiledUtils.createCircleFixtureBodyDef((CircleMapObject)object); } else if (object instanceof EllipseMapObject) { fixtureBodyDefinition = TiledUtils.createEllipseFixtureBodyDef((EllipseMapObject)object); } else if (object instanceof PolylineMapObject || object instanceof PolygonMapObject) { fixtureBodyDefinition = TiledUtils.createPolyFixtureBodyDef(object); } else { throw new InvalidConfigException(filename, "Unknown MapObject type"); } freeBodyDefinitions.add(fixtureBodyDefinition); } }
public static FixtureDef getFixtureDefFromBodySkeleton(MapObject object) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = 1; fixtureDef.friction = 0; fixtureDef.restitution = 0; Shape shape = null; if (object instanceof TextureMapObject) { shape = getTextureMapShape(object); } else if (object instanceof RectangleMapObject) { shape = getRectangleShape(object); } else if (object instanceof CircleMapObject) { shape = getCircleShape(object); } else if (object instanceof EllipseMapObject) { shape = getEllipseShape(object); } else if (object instanceof PolygonMapObject) { shape = getPolygonShape(object); } else if (object instanceof PolylineMapObject) { shape = getPolylineShape(object); } fixtureDef.shape = shape; return fixtureDef; }
private Vector2 getPositionFromMapObject(MapObject mapObject) { if (mapObject instanceof PolygonMapObject) { Polygon polygon = ((PolygonMapObject) mapObject).getPolygon(); return new Vector2(polygon.getX(), polygon.getY()); } else if (mapObject instanceof RectangleMapObject) { Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle(); return new Vector2(rectangle.getX(), rectangle.getY()); } else if (mapObject instanceof EllipseMapObject) { Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse(); return new Vector2(ellipse.x, ellipse.y); } else if (mapObject instanceof CircleMapObject) { Circle circle = ((CircleMapObject) mapObject).getCircle(); return new Vector2(circle.x, circle.y); } throw new GdxRuntimeException("Only Polygons, Rectangles, Ellipses and Circles are supported!"); }
private static Shape getPolygonShape(MapObject object) { Polygon polygon = ((PolygonMapObject)object).getPolygon(); float[] vertices = polygon.getTransformedVertices(); for (int i = 0; i < vertices.length; i++) { vertices[i] *= MainCamera.getInstance().getTileMapScale(); } PolygonShape shape = new PolygonShape(); shape.set(vertices); return shape; }
private Polygon getPolygonFromMapObject(MapObject mapObject) { if (mapObject instanceof PolygonMapObject) { return ((PolygonMapObject) mapObject).getPolygon(); } else if (mapObject instanceof RectangleMapObject) { return MathUtil.polygonFromRectangle(((RectangleMapObject) mapObject).getRectangle()); } throw new GdxRuntimeException("Only Polygons and Rectangles are supported!"); }
private static PolygonShape getPolygon(PolygonMapObject object) { PolygonShape shape = new PolygonShape(); float[] vertices = object.getPolygon().getTransformedVertices(); float[] worldVertices = new float[vertices.length]; for(int i = 0; i < vertices.length; ++i){ worldVertices[i] = Pixels.toMeters(vertices[i]); } shape.set(worldVertices); return shape; }
/** * * @param world * @param map */ public void createBodies(ArrayList<Entity> entities, World world, TiledMap map){ MapObjects objects; objects = map.getLayers().get("ground").getObjects(); for(MapObject object : objects){ if(object instanceof RectangleMapObject){ createRectangle(world, (RectangleMapObject) object, 0.5f, 0.4f, 0.6f); } else if(object instanceof PolygonMapObject){ createPolygon(world, (PolygonMapObject) object, 0.5f, 0.4f, 0.6f); } else if(object instanceof PolylineMapObject){ createPolyline(world, (PolylineMapObject) object, 0.5f, 0.4f, 0.6f); } else if(object instanceof EllipseMapObject){ createEllipse(world, (EllipseMapObject) object, 0.5f, 0.4f, 0.6f); } else{ Gdx.app.error("Error", "Invalid map object"); } } /* objects = map.getLayers().get("dynamic").getObjects(); for(MapObject object : objects){ RectangleMapObject entity = (RectangleMapObject) object; Rectangle position = entity.getRectangle(); entities.add(new Box(world, new Vector2(position.x / SupaBox.PPM, position.y / SupaBox.PPM), new Vector2(0f, 0f), 1f, 1f)); } */ }
/** * * @param world * @param polygonObject * @param density * @param friction * @param restitution */ private void createPolygon(World world, PolygonMapObject polygonObject, float density, float friction, float restitution){ Polygon polygon = polygonObject.getPolygon(); PolygonShape shape = new PolygonShape(); float[] vertices = polygon.getTransformedVertices(); float[] worldVertices = new float[vertices.length]; for(int i = 0; i < vertices.length; i++){ worldVertices[i] = vertices[i] / SupaBox.PPM; } shape.set(worldVertices); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; Body body = world.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.restitution = restitution; body.createFixture(fixtureDef); shape.dispose(); }
private Shape getPolygon(PolygonMapObject polygonObject) { PolygonShape polygon = new PolygonShape(); float[] vertices = polygonObject.getPolygon().getTransformedVertices(); float[] worldVertices = new float[vertices.length]; for (int i = 0; i < vertices.length; ++i) { worldVertices[i] = vertices[i] / GameWorld.units; } polygon.set(worldVertices); return polygon; }
private Shape getPolygon(PolygonMapObject polygonObject) { PolygonShape polygon = new PolygonShape(); float[] vertices = polygonObject.getPolygon().getTransformedVertices(); float[] worldVertices = new float[vertices.length]; for (int i = 0; i < vertices.length; ++i) { worldVertices[i] = vertices[i] / units; } polygon.set(worldVertices); return polygon; }
public static void createGroundObjects(Map map, MapLayer layer, Box2DWorld world) { Matrix4 transformMat4 = getTransformationMatrix(map); for(MapObject object : layer.getObjects()) { if(object instanceof PolygonMapObject) { polygonGround(object, map, world, transformMat4); } } }
private static void polygonGround(MapObject object, Map map, Box2DWorld world, Matrix4 transformMat4) { PolygonMapObject polyObject = (PolygonMapObject)object; Polygon polygon = new Polygon(polyObject.getPolygon().getTransformedVertices()); polygon.setScale(world.WORLD_TO_BOX, world.WORLD_TO_BOX); Vector3 tempVec3 = new Vector3(); // Transform each vertex by transformation matrix for(int ix = 0, iy = 1; iy < polygon.getVertices().length; ix += 2, iy += 2) { tempVec3.set(polygon.getVertices()[ix], polygon.getVertices()[iy], 0); tempVec3.mul(transformMat4); polygon.getVertices()[ix] = tempVec3.x;// - body.getPosition().x; polygon.getVertices()[iy] = tempVec3.y;// - body.getPosition().y; } Polygon[] convexPolygons = GeometryUtils.decompose(polygon); BodyDef bd = new BodyDef(); bd.type = BodyDef.BodyType.StaticBody; bd.fixedRotation = true; Body body = world.getWorld().createBody(bd); for(Polygon convexPolygon : convexPolygons) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = 1f; fixtureDef.friction = 0.0f; fixtureDef.restitution = 0.0f; PolygonShape shape = new PolygonShape(); shape.set(convexPolygon.getTransformedVertices()); fixtureDef.shape = shape; body.createFixture(fixtureDef); } body.setUserData(map); }
/** * creates {@link Fixture Fixtures} from a {@link MapObject} * * @param mapObject the {@link MapObject} to parse * @return an array of parsed {@link Fixture Fixtures} */ public Fixture[] createFixtures(MapObject mapObject) { Polygon polygon; if (!(mapObject instanceof PolygonMapObject) || isConvex(polygon = ((PolygonMapObject) mapObject).getPolygon())) return new Fixture[]{createFixture(mapObject)}; Polygon[] convexPolygons; if (triangulate) { if (areVerticesClockwise(polygon)) { // ensure the vertices are in counterclockwise order (not really necessary according to EarClippingTriangulator's javadoc, but sometimes better) Array<Vector2> vertices = new Array<Vector2>(toVector2Array(polygon.getVertices())); Vector2 first = vertices.removeIndex(0); vertices.reverse(); vertices.insert(0, first); polygon.setVertices(toFloatArray(vertices.items)); } convexPolygons = toPolygonArray(toVector2Array(new EarClippingTriangulator().computeTriangles(polygon.getTransformedVertices()).toArray()), 3); } else { Array<Array<Vector2>> convexPolys = BayazitDecomposer.convexPartition(new Array<Vector2>(toVector2Array(polygon.getTransformedVertices()))); convexPolygons = new Polygon[convexPolys.size]; for (int i = 0; i < convexPolygons.length; i++) convexPolygons[i] = new Polygon(toFloatArray((Vector2[]) convexPolys.get(i).toArray(Vector2.class))); } // create the fixtures using the convex polygons Fixture[] fixtures = new Fixture[convexPolygons.length]; for (int i = 0; i < fixtures.length; i++) { PolygonMapObject convexObject = new PolygonMapObject(convexPolygons[i]); convexObject.setColor(mapObject.getColor()); convexObject.setName(mapObject.getName()); convexObject.setOpacity(mapObject.getOpacity()); convexObject.setVisible(mapObject.isVisible()); convexObject.getProperties().putAll(mapObject.getProperties()); fixtures[i] = createFixture(convexObject); } return fixtures; }
private Shape getPolygon(PolygonMapObject polygonObject) { PolygonShape polygon = new PolygonShape(); float[] vertices = polygonObject.getPolygon().getVertices(); float[] worldVertices = new float[vertices.length]; for (int i = 0; i < vertices.length; ++i) { worldVertices[i] = vertices[i] * m_units; } polygon.set(worldVertices); return polygon; }
/** * Initialisiert den NPC. * Erstellt eine Liste aller Wegpunkte. */ @Override public void onInit() { super.onInit(); float[] vertices; if (rawObject instanceof PolylineMapObject) { PolylineMapObject polyline = (PolylineMapObject) rawObject; loop = false; vertices = polyline.getPolyline().getTransformedVertices(); } else if (rawObject instanceof PolygonMapObject) { PolygonMapObject polygon = (PolygonMapObject) rawObject; loop = true; vertices = polygon.getPolygon().getTransformedVertices(); } else { Gdx.app.log("WARNING", objectId + ": MapObject must be Polyline or Polygon."); worldObjectManager.removeObject(this); return; } waypoints = new Vector2[vertices.length / 2]; for (int i = 0; i < vertices.length / 2; i++) { waypoints[i] = new Vector2(); waypoints[i].x = vertices[i * 2]; waypoints[i].y = vertices[i * 2 + 1]; } if (waypoints.length < 2) { Gdx.app.log("WARNING", objectId + ": Must have at least 2 Waypoints."); worldObjectManager.removeObject(this); return; } body = createCircleBody(waypoints[0].cpy(), 10); targetIndex = 1; }
/** * Initialisierung */ @Override public void onInit() { super.onInit(); npcAtlas = new TextureAtlas(Gdx.files.internal("data/graphics/packed/dragon.atlas")); npcFrontWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("dragon_front"), Animation.PlayMode.LOOP); npcSideWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("dragon_side"), Animation.PlayMode.LOOP); npcBackWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("dragon_back"), Animation.PlayMode.LOOP); float[] vertices; if (rawObject instanceof PolygonMapObject) { PolygonMapObject polygon = (PolygonMapObject) rawObject; loop = true; vertices = polygon.getPolygon().getTransformedVertices(); } else { Gdx.app.log("WARNING", objectId + ": MapObject must be a Polygon."); worldObjectManager.removeObject(this); return; } waypoints = new Vector2[vertices.length / 2]; for (int i = 0; i < vertices.length / 2; i++) { waypoints[i] = new Vector2(); waypoints[i].x = vertices[i * 2]; waypoints[i].y = vertices[i * 2 + 1]; } if (waypoints.length < 2) { Gdx.app.log("WARNING", objectId + ": Must have at least 2 Waypoints."); worldObjectManager.removeObject(this); return; } body = createCircleBody(waypoints[0].cpy(), 10); targetIndex = 1; handler = new AttackHandler(worldObjectManager); handler.setAttackedCallback(new AttackHandler.AttackedCallback() { @Override public boolean run(Player player, int damage) { health -= damage; if (health <= 0) { if (deathCallback != null) deathCallback.run(); worldObjectManager.removeObject(Dragon.this); } else { hitTimer += 0.6f; } return true; } }); handler.createAttackFixture(body, Physics.createRectangle(64, 64, new Vector2(0, 0))); }
/** * Initialisierung */ @Override public void onInit() { super.onInit(); npcAtlas = new TextureAtlas(Gdx.files.internal("data/graphics/packed/mutated_warrior.atlas")); npcFrontWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("warrior_front"), Animation.PlayMode.LOOP); npcSideWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("warrior_side"), Animation.PlayMode.LOOP); npcBackWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("warrior_back"), Animation.PlayMode.LOOP); float[] vertices; if (rawObject instanceof PolygonMapObject) { PolygonMapObject polygon = (PolygonMapObject) rawObject; loop = true; vertices = polygon.getPolygon().getTransformedVertices(); } else { Gdx.app.log("WARNING", objectId + ": MapObject must be a Polygon."); worldObjectManager.removeObject(this); return; } waypoints = new Vector2[vertices.length / 2]; for (int i = 0; i < vertices.length / 2; i++) { waypoints[i] = new Vector2(); waypoints[i].x = vertices[i * 2]; waypoints[i].y = vertices[i * 2 + 1]; } if (waypoints.length < 2) { Gdx.app.log("WARNING", objectId + ": Must have at least 2 Waypoints."); worldObjectManager.removeObject(this); return; } body = createCircleBody(waypoints[0].cpy(), 10); targetIndex = 1; handler = new AttackHandler(worldObjectManager); handler.setAttackedCallback(new AttackHandler.AttackedCallback() { @Override public boolean run(Player player, int damage) { health -= damage; if (health <= 0) { if (deathCallback != null) deathCallback.run(); worldObjectManager.removeObject(Warrior.this); } else { hitTimer += 0.6f; } return true; } }); handler.createAttackFixture(body, Physics.createRectangle(64, 64, new Vector2(0, 0))); }
/** * Initialisierung */ @Override public void onInit() { super.onInit(); npcAtlas = new TextureAtlas(Gdx.files.internal("data/graphics/packed/skeleton.atlas")); npcFrontWalk = new Animation<TextureRegion>(1/7f, npcAtlas.findRegions("skeleton_front"), Animation.PlayMode.LOOP); npcSideWalk = new Animation<TextureRegion>(1/8f, npcAtlas.findRegions("skeleton_side"), Animation.PlayMode.LOOP); npcBackWalk = new Animation<TextureRegion>(1/7f, npcAtlas.findRegions("skeleton_back"), Animation.PlayMode.LOOP); float[] vertices; if (rawObject instanceof PolygonMapObject) { PolygonMapObject polygon = (PolygonMapObject) rawObject; loop = true; vertices = polygon.getPolygon().getTransformedVertices(); } else { Gdx.app.log("WARNING", objectId + ": MapObject must be Polygon."); worldObjectManager.removeObject(this); return; } waypoints = new Vector2[vertices.length / 2]; for (int i = 0; i < vertices.length / 2; i++) { waypoints[i] = new Vector2(); waypoints[i].x = vertices[i * 2]; waypoints[i].y = vertices[i * 2 + 1]; } if (waypoints.length < 2) { Gdx.app.log("WARNING", objectId + ": Must have at least 2 Waypoints."); worldObjectManager.removeObject(this); return; } body = createCircleBody(waypoints[0].cpy(), 10); targetIndex = 1; handler = new AttackHandler(worldObjectManager); handler.setAttackedCallback(new AttackHandler.AttackedCallback() { @Override public boolean run(Player player, int damage) { health -= damage; if (health <= 0) { if (deathCallback != null) deathCallback.run(); worldObjectManager.removeObject(Skeleton.this); } else { hitTimer += 0.6f; } return true; } }); handler.createAttackFixture(body, Physics.createRectangle(64, 64, new Vector2(0, 0))); }
/** * Initialisierung */ @Override public void onInit() { super.onInit(); npcAtlas = new TextureAtlas(Gdx.files.internal("data/graphics/packed/rabbit.atlas")); rabbit = npcAtlas.findRegion("rabbit_mean"); float[] vertices; if (rawObject instanceof PolygonMapObject) { PolygonMapObject polygon = (PolygonMapObject) rawObject; loop = true; vertices = polygon.getPolygon().getTransformedVertices(); } else { Gdx.app.log("WARNING", objectId + ": MapObject must be Polygon."); worldObjectManager.removeObject(this); return; } waypoints = new Vector2[vertices.length / 2]; for (int i = 0; i < vertices.length / 2; i++) { waypoints[i] = new Vector2(); waypoints[i].x = vertices[i * 2]; waypoints[i].y = vertices[i * 2 + 1]; } if (waypoints.length < 2) { Gdx.app.log("WARNING", objectId + ": Must have at least 2 Waypoints."); worldObjectManager.removeObject(this); return; } body = createCircleBody(waypoints[0].cpy(), 10); targetIndex = 1; handler = new AttackHandler(worldObjectManager); handler.setAttackedCallback(new AttackHandler.AttackedCallback() { @Override public boolean run(Player player, int damage) { health -= damage; if (health <= 0) { if (deathCallback != null) deathCallback.run(); worldObjectManager.removeObject(Rabbit.this); } else { hitTimer += 0.6f; } return true; } }); handler.createAttackFixture(body, Physics.createRectangle(64, 64, new Vector2(0, 0))); }
/** * Initialisierung */ @Override public void onInit() { super.onInit(); npcAtlas = new TextureAtlas(Gdx.files.internal("data/graphics/packed/wolf.atlas")); npcFrontWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("wolf_front"), Animation.PlayMode.LOOP); npcSideWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("wolf_side"), Animation.PlayMode.LOOP); npcBackWalk = new Animation<TextureRegion>(1/5f, npcAtlas.findRegions("wolf_back"), Animation.PlayMode.LOOP); float[] vertices; if (rawObject instanceof PolygonMapObject) { PolygonMapObject polygon = (PolygonMapObject) rawObject; loop = true; vertices = polygon.getPolygon().getTransformedVertices(); } else { Gdx.app.log("WARNING", objectId + ": MapObject must be Polygon."); worldObjectManager.removeObject(this); return; } waypoints = new Vector2[vertices.length / 2]; for (int i = 0; i < vertices.length / 2; i++) { waypoints[i] = new Vector2(); waypoints[i].x = vertices[i * 2]; waypoints[i].y = vertices[i * 2 + 1]; } if (waypoints.length < 2) { Gdx.app.log("WARNING", objectId + ": Must have at least 2 Waypoints."); worldObjectManager.removeObject(this); return; } body = createCircleBody(waypoints[0].cpy(), 10); targetIndex = 1; handler = new AttackHandler(worldObjectManager); handler.setAttackedCallback(new AttackHandler.AttackedCallback() { @Override public boolean run(Player player, int damage) { health -= damage; if (health <= 0) { if (deathCallback != null) deathCallback.run(); worldObjectManager.removeObject(Wolf.this); } else { hitTimer += 0.6f; } return true; } }); handler.createAttackFixture(body, Physics.createRectangle(64, 64, new Vector2(0, 0))); }
private void crearColisiones() { Array<Body> slopes = new Array<Body>(); FixtureDef fixDef = new FixtureDef(); MapObjects objects = cls.getObjects(); Iterator<MapObject> objectIt = objects.iterator(); while(objectIt.hasNext()) { MapObject object = objectIt.next(); if (object instanceof TextureMapObject){ continue; } Shape shape; BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; if (object instanceof RectangleMapObject) { RectangleMapObject rectangle = (RectangleMapObject)object; shape = getRectangle(rectangle); } else if (object instanceof PolygonMapObject) { shape = getPolygon((PolygonMapObject)object); } else if (object instanceof PolylineMapObject) { shape = getPolyline((PolylineMapObject)object); } else if (object instanceof EllipseMapObject) { shape = getEllipse((EllipseMapObject)object); } else if (object instanceof CircleMapObject) { shape = getCircle((CircleMapObject)object); } else { continue; } fixDef.shape = shape; fixDef.filter.categoryBits = GameWorld.BIT_PARED; fixDef.filter.maskBits = GameWorld.BIT_JUGADOR | GameWorld.BIT_ENEMIGOS | GameWorld.BIT_SENSOR; Body suelo = GameWorld.mundo.createBody(bodyDef); suelo.createFixture(fixDef).setUserData("cls"); slopes.add(suelo); shape.dispose(); } }
/** * @param map map to be used to create the static bodies. * @param layerName name of the layer that contains the shapes. */ public void createPhysics(Map map, String layerName) { MapLayer layer = map.getLayers().get(layerName); if (layer == null) { logger.error("layer " + layerName + " does not exist"); return; } MapObjects objects = layer.getObjects(); Iterator<MapObject> objectIt = objects.iterator(); while(objectIt.hasNext()) { MapObject object = objectIt.next(); if (object instanceof TextureMapObject){ continue; } Shape shape; BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; if (object instanceof RectangleMapObject) { RectangleMapObject rectangle = (RectangleMapObject)object; shape = getRectangle(rectangle); } else if (object instanceof PolygonMapObject) { shape = getPolygon((PolygonMapObject)object); } else if (object instanceof PolylineMapObject) { shape = getPolyline((PolylineMapObject)object); } else if (object instanceof CircleMapObject) { shape = getCircle((CircleMapObject)object); } else { logger.error("non suported shape " + object); continue; } MapProperties properties = object.getProperties(); String material = properties.get("material", "default", String.class); FixtureDef fixtureDef = materials.get(material); if (fixtureDef == null) { logger.error("material does not exist " + material + " using default"); fixtureDef = materials.get("default"); } fixtureDef.shape = shape; fixtureDef.filter.categoryBits = Env.game.getCategoryBitsManager().getCategoryBits("level"); Body body = world.createBody(bodyDef); body.createFixture(fixtureDef); bodies.add(body); fixtureDef.shape = null; shape.dispose(); } }