/** * Box's Constructor. * Creates a Box in the given with world, from the given object. * * @param world Physics world where the box will be in. * @param object Object used to create the box. */ public BoxModel(World world, RectangleMapObject object) { super(world, object.getRectangle().getCenter(new Vector2()).scl(PIXEL_TO_METER), ModelType.BOX, ANGULAR_DAMP, LINEAR_DAMP); // Fetch density from properties Float property = object.getProperties().get("density", float.class); if (property != null) density = property; // Create Fixture's Shape Shape shape = createPolygonShape(new float[]{ 0, 0, 64, 0, 64, 64, 0, 64 }, new Vector2(64, 64)); createFixture(new FixtureProperties(shape, density, FRICTION, RESTITUTION, GROUND_BIT, (short) (BALL_BIT | GROUND_BIT | FLUID_BIT))); }
public Body createTempBody(float x, float y, FixtureDef fixtureDef) { // Dynamic Body BodyDef bodyDef = new BodyDef(); if (box2dDebug) bodyDef.type = BodyType.StaticBody; else bodyDef.type = BodyType.DynamicBody; // transform into box2d x = x * WORLD_TO_BOX; y = y * WORLD_TO_BOX; bodyDef.position.set(x, y); Body body = world.createBody(bodyDef); Shape shape = new CircleShape(); ((CircleShape) shape).setRadius(1 * WORLD_TO_BOX); if (fixtureDef == null) throw new GdxRuntimeException("fixtureDef cannot be null!"); fixtureDef.shape = shape; body.createFixture(fixtureDef); return body; }
/** * Erlaubt es den Unterklassen möglichst einfach einen beliebigen Box2D Körper zu erstellen. * * @param position die Startposition des Body * @param shape die Form, die für dne Body verwendet werden soll * @param type der Typ des Körpers * @return ein Box2D Körper */ protected Body createEntityBody(Vector2 position, Shape shape, BodyDef.BodyType type) { position.scl(Physics.MPP); BodyDef bodyDef = new BodyDef(); bodyDef.type = type; bodyDef.position.set(position); bodyDef.fixedRotation = true; Body body = worldObjectManager.getPhysicalWorld().createBody(bodyDef); body.setUserData(this); FixtureDef fixture = new FixtureDef(); fixture.shape = shape; fixture.filter.categoryBits = Physics.CATEGORY_ENTITIES; fixture.filter.maskBits = Physics.MASK_ENTITIES; body.createFixture(fixture).setUserData(this); shape.dispose(); return body; }
/** * Erzeugt automatisch aus einem MapObject einen statischen Trigger. * * @see InteractionHandler#createInteractionFixture(Body, Shape) * * @param mapObject das MapObject */ public void createStaticInteractionBody(MapObject mapObject) { Shape shape = PhysicsTileMapBuilder.createShape(mapObject, PhysicsTileMapBuilder.TYPE_TRIGGER); if (shape == null) { Gdx.app.log("WARNING", "Unkown trigger type! The interaction object " + mapObject.getName() + " will be ignored!"); return; } BodyDef triggerDef = new BodyDef(); triggerDef.type = BodyDef.BodyType.StaticBody; Body trigger = manager.getPhysicalWorld().createBody(triggerDef); createInteractionFixture(trigger, shape); shape.dispose(); }
/** * Erzeugt automatisch aus einem MapObject einen statischen Trigger. * * @see AttackHandler#createAttackFixture(Body, Shape) * * @param mapObject das MapObject */ public void createStaticAttackBody(MapObject mapObject) { Shape shape = PhysicsTileMapBuilder.createShape(mapObject, PhysicsTileMapBuilder.TYPE_TRIGGER); if (shape == null) { Gdx.app.log("WARNING", "Unkown trigger type! The attack object " + mapObject.getName() + " will be ignored!"); return; } BodyDef triggerDef = new BodyDef(); triggerDef.type = BodyDef.BodyType.StaticBody; Body trigger = manager.getPhysicalWorld().createBody(triggerDef); createAttackFixture(trigger, shape); shape.dispose(); }
/** * Erzeugt eine Form aus einem MapObject. * * @param mapObject das MapObject * @param mask Bitmaske, um Shape Typen auszuschließen * @return die entsprechende Form */ public static Shape createShape(MapObject mapObject, short mask) { Shape shape = null; if (mapObject instanceof RectangleMapObject && (mask & TYPE_RECTANGLE) != 0) { shape = PhysicsTileMapBuilder.createRectangle((RectangleMapObject) mapObject); } else if (mapObject instanceof PolygonMapObject && (mask & TYPE_POLYGON) != 0) { shape = PhysicsTileMapBuilder.createPolygon((PolygonMapObject) mapObject); } else if (mapObject instanceof EllipseMapObject && (mask & TYPE_CIRCLE) != 0) { shape = PhysicsTileMapBuilder.createCircle((EllipseMapObject) mapObject); } else if (mapObject instanceof PolylineMapObject && (mask & TYPE_POLYLINE) != 0) { shape = createPolyline((PolylineMapObject) mapObject); } return shape; }
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 static Shape getPolyline(PolylineMapObject object) { float[] vertices = object.getPolyline().getTransformedVertices(); Vector2[] worldVertices = new Vector2[vertices.length / 2]; for(int i = 0; i < vertices.length / 2; ++i){ Vector2 vector = new Vector2(); vector.x = vertices[i * 2]; vector.y = vertices[i * 2 + 1]; worldVertices[i] = Pixels.toMeters(new Vector2(vector)); } ChainShape shape = new ChainShape(); shape.createChain(worldVertices); return shape; }
@Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { /* Checks whether the max amount of balls were spawned */ if (spawnedBalls < MAX_SPAWNED_BALLS) { spawnedBalls++; /* Translate camera point to world point */ Vector3 unprojectedVector = new Vector3(); camera.unproject(unprojectedVector.set(screenX, screenY, 0)); /* Create a new ball */ Shape shape = Box2DFactory.createCircleShape(1); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(unprojectedVector.x, unprojectedVector.y)); } return true; }
public static Body createWalls(World world, float viewportWidth, float viewportHeight, float offset) { float halfWidth = viewportWidth / 2 - offset; float halfHeight = viewportHeight / 2 - offset; Vector2[] vertices = new Vector2[] { new Vector2(-halfWidth, -halfHeight), new Vector2(halfWidth, -halfHeight), new Vector2(halfWidth, halfHeight), new Vector2(-halfWidth, halfHeight), new Vector2(-halfWidth, -halfHeight) }; Shape shape = createChainShape(vertices); FixtureDef fixtureDef = createFixture(shape, 1, 0.5f, 0, false); return createBody(world, BodyType.StaticBody, fixtureDef, new Vector2( 0, 0)); }
/** * @param shape the {@link Shape} which position should be returned * @param rotation the rotation of the {@link Body Body's} {@link Transform} this shape is attached to * @return the position of the given {@link Shape} relative to its {@link Body} */ public static Vector2 getPositionRelative(Shape shape, float rotation) { Vector2 position = new Vector2(); // get the position without rotation Vector2[] shapeVertices = getVertices(shape); position.set(GeometryUtils.getMaxX(shapeVertices) - GeometryUtils.getWidth(shapeVertices) / 2, GeometryUtils.getMaxY(shapeVertices) - GeometryUtils.getHeight(shapeVertices) / 2); // transform position according to rotation // http://stackoverflow.com/questions/1469149/calculating-vertices-of-a-rotated-rectangle float xx = position.x, xy = position.y, yx = position.x, yy = position.y; xx = xx * (float) Math.cos(rotation) - xy * (float) Math.sin(rotation); yy = yx * (float) Math.sin(rotation) + yy * (float) Math.cos(rotation); position.set(xx, yy); return position; }
public Body createDynamicBody(float posX, float posY, Shape shape, float density, float friction, float restitution) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(posX, posY); Body body = world.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.friction = friction; fixtureDef.density = density; fixtureDef.restitution = restitution; Fixture fixture = body.createFixture(fixtureDef); shape.dispose(); return body; }
/** * Constructs a Body without using BodyDef or FixtureDef. * * @param world * The EntityWorld. * @param e * The entity that contains this Body. * @param bodyType * The type of this Body. * @param shape * The body's shape. * @param position * The body's initial position. */ public Body(EntityWorld world, Entity e, BodyType bodyType, Shape shape, Vector2 position) { entityWorld = world; BodyDef bodyDef = new BodyDef(); bodyDef.type = bodyType; bodyDef.position.set(position); body = world.getBox2DWorld().createBody(bodyDef); body.setUserData(e); LogManager.debug("Physics", "Body Created"); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; body.createFixture(fixtureDef); }
public static GameObject createGameObjectWithBox2DBody(Vector2 gameObjectPos, Texture texture, Vector3 densityFrictionRestitution, BodyDef.BodyType bodyType, Shape.Type shapeType, World world) { GameObject gameObject = new GameObject(); gameObject.setPosition(gameObjectPos.x, gameObjectPos.y); gameObject.setTexture(texture); gameObject.setSprite(new Sprite(texture)); gameObject.getSprite().setOriginCenter(); gameObject.setWorld(world); BodyCreator bodyCreator = new BodyCreator(); gameObject.setBody(bodyCreator.createBody(new Vector2(gameObject.getX(), gameObject.getY()), texture, bodyType, world, shapeType, densityFrictionRestitution)); bodyCreator = null; return gameObject; }
public Body createBody(Vector2 position, Texture texture, BodyDef.BodyType bodyType, World world, Shape.Type shape, Vector3 densityRestitutionFriction) { this.texture = texture; bodyDef = createBodyDef(bodyType, position); body = world.createBody(bodyDef); FixtureCreator.createFixture(body, createShape(shape), densityRestitutionFriction); return body; }
private static FixtureDef createFixtureDef(Shape shape, float density, float restitution, float friction) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = density; fixtureDef.restitution = restitution; fixtureDef.friction = friction; return fixtureDef; }
/** * Ball Model's Constructor. * Creates a ball in a given starting position, in a given world. * * @param world Physics world where the ball will be used in. * @param pos Ball's initial Position. */ public BallModel(World world, Vector2 pos) { super(world, pos, ModelType.BALL, ANGULAR_DAMP, LINEAR_DAMP); // Create Fixture's Shape Shape circle = new CircleShape(); circle.setRadius(RADIUS); createFixture(new FixtureProperties(circle, density, friction, restitution, BALL_BIT, (short) (BALL_BIT | GROUND_BIT | FLUID_BIT | HITTABLE_BIT))); }
/** * Generic Power Up's constructor. * Creates a power up from the given object, with the given type, into the given world. * * @param world The world the power up will be in. * @param object The object to create the power up with. * @param type Type of Power Up. */ public PowerUp(World world, RectangleMapObject object, ModelType type) { super(world, object.getRectangle().getCenter(new Vector2()).scl(PIXEL_TO_METER), type); getBody().setType(BodyDef.BodyType.StaticBody); Shape shape = createPolygonShape(new float[]{ 14f, 1f, 18f, 1f, 25f, 7f, 25f, 23f, 18f, 31f, 14f, 31f, 7f, 23f, 7f, 7f }, new Vector2(32, 32)); FixtureProperties fixtureProperties = new FixtureProperties(shape, HITTABLE_BIT, BALL_BIT); fixtureProperties.setSensor(); createFixture(fixtureProperties); }
/** * Create a fixture, based float density, float friction,float restitution, * shape * * @return */ public FixtureDef createFixtureDef(float density, float friction, float restitution, Shape shape) { FixtureDef output = new FixtureDef(); output.density = density; output.friction = friction; output.restitution = restitution; output.shape = shape; return output; }
/** * Create a fixture, based float density, float friction,float restitution, * shape * * @return */ protected FixtureDef createFixtureDef(float density, float friction, float restitution, Shape shape) { FixtureDef output = new FixtureDef(); output.density = density; output.friction = friction; output.restitution = restitution; output.shape = shape; return output; }
/** * Erstellt einen Box2D Körper und macht ihm zum Sensor. */ @Override public void onInit() { World world = worldObjectManager.getPhysicalWorld(); Shape shape = PhysicsTileMapBuilder.createShape(rawObject, PhysicsTileMapBuilder.TYPE_TRIGGER); if (shape == null) { Gdx.app.log("WARNING", "Unkown trigger type! The trigger " + objectId + " will be ignored!"); return; } BodyDef triggerDef = new BodyDef(); triggerDef.type = BodyDef.BodyType.StaticBody; trigger = world.createBody(triggerDef); FixtureDef fixture = new FixtureDef(); fixture.shape = shape; fixture.isSensor = true; fixture.filter.categoryBits = Physics.CATEGORY_TRIGGER; fixture.filter.maskBits = Physics.MASK_TRIGGER; trigger.createFixture(fixture).setUserData(this); shape.dispose(); }
/** * Erzeugt aus einer Form ein Fixture und fügt dieses einem Körper hinzu. * Das Fixture wird als Trigger für Interaktionen benutzt. * * @param body der Körper * @param shape die Form */ public void createInteractionFixture(Body body, Shape shape) { FixtureDef fixture = new FixtureDef(); fixture.shape = shape; fixture.isSensor = true; fixture.filter.categoryBits = Physics.CATEGORY_TRIGGER; fixture.filter.maskBits = Physics.MASK_TRIGGER; body.createFixture(fixture).setUserData(this); }
/** * Erzeugt aus einer Form ein Fixture und fügt dieses einem Körper hinzu. * Das Fixture wird als Trigger für Angriffe benutzt. * * @param body der Körper * @param shape die Form */ public void createAttackFixture(Body body, Shape shape) { FixtureDef fixture = new FixtureDef(); fixture.shape = shape; fixture.isSensor = true; fixture.filter.categoryBits = Physics.CATEGORY_TRIGGER; fixture.filter.maskBits = Physics.MASK_TRIGGER; body.createFixture(fixture).setUserData(this); }
/** * Erzeugt aus einem ObjectLayer Box2D Körper. * * @param layer die Ebene * @param world die Box2D Welt */ public static void buildBodiesFromLayer(MapLayer layer, World world) { for(MapObject tileObject : layer.getObjects()) { if ( tileObject instanceof TextureMapObject || tileObject instanceof CircleMapObject || tileObject instanceof EllipseMapObject) continue; Shape shape = createShape(tileObject, TYPE_COLLISION); if (shape == null) continue; BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; Body body = world.createBody(bodyDef); FixtureDef fixture = new FixtureDef(); fixture.shape = shape; fixture.filter.categoryBits = Physics.CATEGORY_WORLD; fixture.filter.maskBits = Physics.MASK_WORLD; body.createFixture(fixture); shape.dispose(); } }
@Override public boolean claim(WorldRoom parent) { if (this.parent == null) { this.parent = parent; String[] defShape = this.mapEntrypoint.split(":"); this.bodyData = this.parent.getMap().getDefinedShape(defShape[0], defShape[1]); if (this.spawnPoint == null) { String rawPoint = this.parent.getMap().getEntrypointSpawn(this.mapEntrypoint); if (rawPoint != null && !rawPoint.trim().isEmpty()) { String[] defPoint = rawPoint.split(":"); this.spawnPoint = this.parent.getMap().getDefinedPoint(defPoint[0], defPoint[1]); } } if (this.bodyData != null) { float ptm = OverworldController.PIXELS_TO_METERS; BodyDef def = new BodyDef(); def.type = BodyType.StaticBody; def.position.set(bodyData.getPosition()); def.position.x = def.position.x * ptm; def.position.y = def.position.y * ptm; Shape shape = bodyData.generateShape(); this.body = parent.getOverworld().getCollisionHandler().getWorld().createBody(def); this.body.createFixture(shape, 0F); this.body.setUserData(this); shape.dispose(); } return true; } return false; }
private static Shape getTextureMapShape(MapObject object) { TextureMapObject textureMapObject = (TextureMapObject)object; float width = (Float)textureMapObject.getProperties().get("width"); float height = (Float)textureMapObject.getProperties().get("height"); PolygonShape shape = new PolygonShape(); float scale = MainCamera.getInstance().getTileMapScale(); shape.setAsBox((width / 2) * scale, (height / 2) * scale); return shape; }
private static Shape getRectangleShape(MapObject object) { Rectangle rectangle = ((RectangleMapObject)object).getRectangle(); PolygonShape shape = new PolygonShape(); float scale = MainCamera.getInstance().getTileMapScale(); shape.setAsBox((rectangle.width / 2) * scale, (rectangle.height / 2) * scale); return shape; }
private static Shape getCircleShape(MapObject object) { Circle circle = ((CircleMapObject)object).getCircle(); CircleShape shape = new CircleShape(); shape.setRadius(circle.radius * MainCamera.getInstance().getTileMapScale()); return shape; }
private static Shape getEllipseShape(MapObject object) { Ellipse circle = ((EllipseMapObject)object).getEllipse(); CircleShape shape = new CircleShape(); shape.setRadius(circle.width / 2 * MainCamera.getInstance().getTileMapScale()); return shape; }
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 static Shape getPolylineShape(MapObject object) { Polyline polyline = ((PolylineMapObject)object).getPolyline(); float[] vertices = polyline.getTransformedVertices(); for (int i = 0; i < vertices.length; i++) { vertices[i] *= MainCamera.getInstance().getTileMapScale(); } ChainShape shape = new ChainShape(); shape.createChain(vertices); return shape; }
private static FixtureDef getFixtureDef(final Shape shape) { final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.filter.categoryBits = Box2DUtil.CAT_PLAYERS; fixtureDef.filter.maskBits = Box2DUtil.MASK_PLAYER; fixtureDef.density = 0.4f; fixtureDef.restitution = 0.3f; return fixtureDef; }
private static final Fixture createFixture(Body body, Shape shape){ FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 1f; Fixture fixture = body.createFixture(fixtureDef); shape.dispose(); return fixture; }
private Shape loadShape(JsonValue root) { Shape shape = null; JsonValue shapeValue = root.get("shape"); if (shapeValue == null) { return shape; } String type = shapeValue.getString("type"); float x = shapeValue.getFloat("centerX", 0.0f); float y = shapeValue.getFloat("centerY", 0.0f); if (type.equals("circle")) { logger.info("loading cicle shape"); CircleShape circle = new CircleShape(); circle.setPosition(new Vector2(x, y)); circle.setRadius(shapeValue.getFloat("radius", 1.0f)); shape = circle; } else if (type.equals("polygon")) { logger.info("loading polygon shape"); PolygonShape polygon = new PolygonShape(); polygon.setAsBox(shapeValue.getFloat("width", 1.0f), shapeValue.getFloat("height", 1.0f), new Vector2(x, y), 0.0f); shape = polygon; } else { logger.error("shape unknown " + type); } return shape; }
@Override public void drawBorder(Border border, Batch batch, Color parentColor) { Shape shape = border.getShape(); if (shape instanceof ChainShape) { Graphics.drawChain(border.getVertexes(), new Color(0.1f, 1, 0.5f, 0.75f), 0.2f); } }
public static FixtureDef fixtureDef(float density,float friction,float restitution, Shape shape, boolean isSensor){ FixtureDef fd = new FixtureDef(); fd.density = density; fd.friction = friction; fd.restitution = restitution; fd.shape = shape; fd.isSensor = isSensor; return fd; }
public ShapeModel(Shape s){ shapeType = s.getType(); radius = s.getRadius(); switch (shapeType){ case Circle: CircleShape circle = (CircleShape)s; position.set(circle.getPosition()); break; case Polygon: PolygonShape poly = (PolygonShape)s; for (int i=0; i<poly.getVertexCount(); i++){ Vector2 v = new Vector2(); poly.getVertex(i, v); vertices.add(v); } break; case Edge: assert false : "Edge shapes not supported in PhysicsComponent.ShapeModel."; break; case Chain: ChainShape chain = (ChainShape)s; isLooped = chain.isLooped(); for (int i=0; i<chain.getVertexCount(); i++){ Vector2 v = new Vector2(); chain.getVertex(i, v); vertices.add(v); } break; } }
public Shape toShape(){ Shape s = null; switch (shapeType){ case Circle: CircleShape circle = new CircleShape(); circle.setPosition(position); circle.setRadius(radius); s = circle; break; case Polygon: PolygonShape poly = new PolygonShape(); Vector2[] verts = vertices.toArray(Vector2.class); poly.set(verts); s = poly; break; case Edge: EdgeShape edge = new EdgeShape(); s = edge; assert false : "Edge shapes not supported in PhysicsComponent.ShapeModel."; break; case Chain: ChainShape chain = new ChainShape(); if (chain.isLooped()) chain.createLoop(vertices.toArray()); else chain.createChain(vertices.toArray()); s = chain; break; } return s; }