@Override public boolean keyDown (int keyCode) { if (keyCode == Input.Keys.COMMA) { if (m_bullet != null) { world.destroyBody(m_bullet); m_bullet = null; } { CircleShape shape = new CircleShape(); shape.setRadius(0.25f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.bullet = true; bd.position.set(-31, 5); m_bullet = world.createBody(bd); m_bullet.createFixture(fd); m_bullet.setLinearVelocity(new Vector2(400, 0)); } } return false; }
/** Creates a fixture and attach it to this body. Use this function if you need to set some fixture parameters, like friction. * Otherwise you can create the fixture directly from a shape. If the density is non-zero, this function automatically updates * the mass of the body. Contacts are not created until the next time step. * @param def the fixture definition. * @warning This function is locked during callbacks. */ public Fixture createFixture (FixtureDef def) { org.jbox2d.dynamics.FixtureDef fd = def.toJBox2d(); org.jbox2d.dynamics.Fixture f = body.createFixture(fd); Fixture fixture = new Fixture(this, f); fixtures.add(fixture); world.fixtures.put(f, fixture); return fixture; }
shape.set(new Vector2(-40.0f, 0), new Vector2(40, 0)); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.3f;
shape.setRadius(1); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f;
shape.set(new Vector2(-20, 0), new Vector2(20, 0)); FixtureDef fd = new FixtureDef(); fd.shape = shape; ground.createFixture(fd); shape.setAsBox(0.5f, 4.0f, new Vector2(4.0f, 0), 0.5f * (float)Math.PI); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.6f; shape.setAsBox(0.75f, 0.75f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.6f;
FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; shape.set(vertices); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; shape.setRadius(0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f;
shape.setAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; shape.setAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f;
shape.set(new Vector2(-40.0f, 0), new Vector2(40, 0)); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.3f; polygon.set(vertices); FixtureDef triangleShapeDef = new FixtureDef(); triangleShapeDef.shape = polygon; triangleShapeDef.density = 1.0f; FixtureDef boxShapeDef = new FixtureDef(); boxShapeDef.shape = polygon; boxShapeDef.density = 1; circle.setRadius(1); FixtureDef circleShapeDef = new FixtureDef(); circleShapeDef.shape = circle; circleShapeDef.density = 1.0f;
shape.setAsBox(0.6f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f;
FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.density = 0; poly1.set(vertices); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.density = 4.0f; poly2.set(vertices); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.density = 2.0f; shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f;
shape.setAsBox(10.0f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.8f;
shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f;
shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; shape.set(vertices); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; shape.setRadius(0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f;
shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; shape.set(vertices); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; shape.setRadius(0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f;
FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = groundPoly; fixtureDef.filter.groupIndex = 0;
public Enemy(Texture texture, float x, float y) { this.texture = texture; enemyBody = createPolygon(BodyType.DynamicBody, x, y, 1f, 0f, 0.8f, ENEMY_WIDTH * 0.5f, ENEMY_HEIGHT * 0.5f, CATEGORY_ENEMY, MASK_ENEMY); //To identify the specific instance when collisions happen enemyBody.setUserData(this); // Sensor shape to detect other bodies getting close to him CircleShape circle = new CircleShape(); circle.setRadius(1f); FixtureDef sensorFD = new FixtureDef(); sensorFD.isSensor = true; sensorFD.shape = circle; sensorFD.filter.categoryBits = CATEGORY_SENSOR; sensorFD.filter.maskBits = MASK_SENSOR; enemyBody.createFixture(sensorFD); }
private void createBall(Vector2 orig, Vector2 force) { Random rand = new Random(); float radius = rand.nextFloat() * 0.02f + 0.02f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.angularDamping = 0.5f; bd.linearDamping = 0.5f; bd.position.set(orig); bd.angle = rand.nextFloat() * MathUtils.PI; Body b = world.createBody(bd); b.applyLinearImpulse(force, orig, true);//TODO probably a bug ballsBodies.add(b); CircleShape shape = new CircleShape(); shape.setRadius(radius); FixtureDef fd = new FixtureDef(); fd.density = 1f; fd.friction = 0.5f; fd.restitution = 1f; fd.shape = shape; b.createFixture(fd); Sprite sp = new Sprite(Assets.inst().get("data/ball.png", Texture.class)); sp.setSize(radius * 2, radius * 2); sp.setOrigin(sp.getWidth() / 2, sp.getHeight() / 2); ballsSprites.add(sp); }
private void createGlass() { // Instantiate the loader with the created JSON data BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/box2D/glass.json")); // Create the glass body definition and place it in within the world BodyDef bd = new BodyDef(); bd.type = BodyType.StaticBody; bd.position.set(7, 1.5f); // Set physics properties FixtureDef fd = new FixtureDef(); fd.density = 1; fd.friction = 0.5f; fd.restitution = 0.05f; // Create the glass body glassBody = world.createBody(bd); // Magic happens here!! Glass fixture is generated automatically by the loader. loader.attachFixture(glassBody, "Glass", fd, GLASS_WIDTH); }
/** * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), * and rotating the box counterclockwise through the given angle, with specified restitution. */ public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) { float cx = (xmin + xmax) / 2; float cy = (ymin + ymax) / 2; float hx = Math.abs((xmax - xmin) / 2); float hy = Math.abs((ymax - ymin) / 2); PolygonShape wallshape = new PolygonShape(); // Don't set the angle here; instead call setTransform on the body below. This allows future // calls to setTransform to adjust the rotation as expected. wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f); FixtureDef fdef = new FixtureDef(); fdef.shape = wallshape; fdef.density = 1.0f; if (restitution>0) fdef.restitution = restitution; BodyDef bd = new BodyDef(); bd.position.set(cx, cy); Body wall = world.createBody(bd); wall.createFixture(fdef); wall.setType(BodyDef.BodyType.StaticBody); wall.setTransform(cx, cy, angle); return wall; }
private void createBalls() { BodyDef ballBodyDef = new BodyDef(); ballBodyDef.type = BodyType.DynamicBody; CircleShape shape = new CircleShape(); shape.setRadius(BALL_RADIUS); FixtureDef fd = new FixtureDef(); fd.density = 1; fd.friction = 0.5f; fd.restitution = 0.5f; fd.shape = shape; ballModels = new Body[MAX_BALLS]; for (int i = 0; i < MAX_BALLS; i++) { ballModels[i] = world.createBody(ballBodyDef); ballModels[i].createFixture(fd); } shape.dispose(); }