Пример #1
0
  private void definePlayer() {
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(32 / Gyaya.PPM, 32 / Gyaya.PPM);
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(8f / Gyaya.PPM);
    fixtureDef.filter.categoryBits = Gyaya.PLAYER_BIT;
    fixtureDef.filter.maskBits =
        Gyaya.GROUND_BIT
            | Gyaya.BRICK_BIT
            | Gyaya.COIN_BIT
            | Gyaya.OBJECT_BIT
            | Gyaya.ENEMY_BIT
            | Gyaya.ENEMY_HEAD_BIT;

    fixtureDef.shape = shape;

    b2body.createFixture(fixtureDef);
    shape.setPosition(new Vector2(0, 4 / Gyaya.PPM));
    shape.setRadius(6f / Gyaya.PPM);
    b2body.createFixture(fixtureDef);

    EdgeShape head = new EdgeShape();
    head.set(
        new Vector2(-3 / Gyaya.PPM, 10f / Gyaya.PPM), new Vector2(3 / Gyaya.PPM, 10f / Gyaya.PPM));
    fixtureDef.shape = head;
    fixtureDef.isSensor = true;
    b2body.createFixture(fixtureDef).setUserData("head");

    EdgeShape feet = new EdgeShape();
    feet.set(
        new Vector2(-6 / Gyaya.PPM, -9f / Gyaya.PPM), new Vector2(6 / Gyaya.PPM, -9f / Gyaya.PPM));
    fixtureDef.shape = feet;
    fixtureDef.isSensor = true;
    b2body.createFixture(fixtureDef).setUserData("feet");
  }
  @Override
  public void create() {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");

    // Create two identical sprites slightly offset from each other vertically
    sprite = new Sprite(img);
    sprite.setPosition(-sprite.getWidth() / 2, -sprite.getHeight() / 2 + 200);
    sprite2 = new Sprite(img);
    sprite2.setPosition(-sprite.getWidth() / 2 + 20, -sprite.getHeight() / 2 + 400);

    world = new World(new Vector2(0, -1f), true);

    // Sprite1's Physics body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(
        (sprite.getX() + sprite.getWidth() / 2) / PIXELS_TO_METERS,
        (sprite.getY() + sprite.getHeight() / 2) / PIXELS_TO_METERS);

    body = world.createBody(bodyDef);

    // Sprite2's physics body
    BodyDef bodyDef2 = new BodyDef();
    bodyDef2.type = BodyDef.BodyType.DynamicBody;
    bodyDef2.position.set(
        (sprite2.getX() + sprite2.getWidth() / 2) / PIXELS_TO_METERS,
        (sprite2.getY() + sprite2.getHeight() / 2) / PIXELS_TO_METERS);

    body2 = world.createBody(bodyDef2);

    // Both bodies have identical shape
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(
        sprite.getWidth() / 2 / PIXELS_TO_METERS, sprite.getHeight() / 2 / PIXELS_TO_METERS);

    // Sprite1
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0.1f;
    fixtureDef.restitution = 0.5f;

    // Sprite2
    FixtureDef fixtureDef2 = new FixtureDef();
    fixtureDef2.shape = shape;
    fixtureDef2.density = 0.1f;
    fixtureDef2.restitution = 0.5f;

    body.createFixture(fixtureDef);
    body2.createFixture(fixtureDef2);

    shape.dispose();

    // Now the physics body of the bottom edge of the screen
    BodyDef bodyDef3 = new BodyDef();
    bodyDef3.type = BodyDef.BodyType.StaticBody;
    float w = Gdx.graphics.getWidth() / PIXELS_TO_METERS;
    float h = Gdx.graphics.getHeight() / PIXELS_TO_METERS;

    bodyDef3.position.set(0, 0);
    FixtureDef fixtureDef3 = new FixtureDef();

    EdgeShape edgeShape = new EdgeShape();
    edgeShape.set(-w / 2, -h / 2, w / 2, -h / 2);
    fixtureDef3.shape = edgeShape;

    bodyEdgeScreen = world.createBody(bodyDef3);
    bodyEdgeScreen.createFixture(fixtureDef3);
    edgeShape.dispose();

    camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    /*world.setContactListener(new ContactListener() {
        @Override
        public void beginContact(Contact contact) {
            // Check to see if the collision is between the second sprite and the bottom of the screen
            // If so apply a random amount of upward force to both objects... just because
            if((contact.getFixtureA().getBody() == bodyEdgeScreen &&
                    contact.getFixtureB().getBody() == body2)
                    ||
                    (contact.getFixtureA().getBody() == body2 &&
                            contact.getFixtureB().getBody() == bodyEdgeScreen)) {

                body.applyForceToCenter(0,MathUtils.random(20,50),true);
                body2.applyForceToCenter(0, MathUtils.random(20,50), true);
            }
        }

        @Override
        public void endContact(Contact contact) {
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
        }
    });*/
  }