private Entity buildPuffin(World world) {
    Entity e = engine.createEntity();
    e.add(new PuffinComponent());

    AnimationComponent a = new AnimationComponent();
    a.animations.put(
        "DEFAULT", new Animation(1f / 16f, Assets.getPuffinArray(), Animation.PlayMode.LOOP));
    a.animations.put(
        "RUNNING", new Animation(1f / 16f, Assets.getPuffinRunArray(), Animation.PlayMode.LOOP));
    e.add(a);
    StateComponent state = new StateComponent();
    state.set("DEFAULT");
    e.add(state);
    TextureComponent tc = new TextureComponent();
    e.add(tc);

    TransformComponent tfc = new TransformComponent();
    tfc.position.set(10f, 10f, 1f);
    tfc.rotation = 15f;
    tfc.scale.set(0.25f, 0.25f);
    e.add(tfc);

    BodyComponent bc = new BodyComponent();
    BodyDef bodyDef = new BodyDef();
    // We set our body to dynamic, for something like ground which doesn't move we would set it to
    // StaticBody
    bodyDef.type = BodyDef.BodyType.DynamicBody;

    // Set our body's starting position in the world
    bodyDef.position.set(10f, 23f);

    // Create our body in the world using our body definition
    bc.body = world.createBody(bodyDef);
    bc.body.applyAngularImpulse(50f, true);

    // Create a circle shape and set its radius to 6
    CircleShape circle = new CircleShape();
    circle.setRadius(2f);

    // Create a fixture definition to apply our shape to
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 20f;
    fixtureDef.friction = 0.4f;
    fixtureDef.restitution = 0.6f; // Make it bounce a little bit

    // Create our fixture and attach it to the body
    bc.body.createFixture(fixtureDef);

    // Remember to dispose of any shapes after you're done with them!
    // BodyDef and FixtureDef don't need disposing, but shapes do.
    circle.dispose();

    e.add(bc);
    return e;
  }
Example #2
0
  private void createCircleAtPos(Body body) {

    FixtureDef fixtureDef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(1f);
    fixtureDef.shape = shape;
    fixtureDef.density = 5f;
    fixtureDef.restitution = 0.8f;

    Gdx.app.log(TAG, "creating body with pos: " + body.getPosition());
    body.createFixture(fixtureDef);
  }
  @Override
  public void onDrawFrame(GL10 unused) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    float[] parScratch = new float[16];
    float[] bogieScratch = new float[16];
    float[] birdieScratch = new float[16];
    float[] shadowScratch1 = new float[16];
    float[] shadowScratch2 = new float[16];

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    float parAngle = -1 * (360 * parPercent);
    float birdieAngle = -1 * (360 * birdiePercent);
    float startingAngle = -90;

    // System.out.println("par angle = " + Float.toString(parAngle));
    // System.out.println("birdie angle = " + Float.toString(birdieAngle));
    // System.out.println("bogie angle = " + Float.toString(-1 * (360 * bogiePercent)));

    // Create a rotation transformation;
    if (angle > -450) {
      // rotates birdie sector to starting angle from angle 0
      if (angle > startingAngle) {
        Matrix.setRotateM(mRotationMatrixBirdie, 0, angle, 0, 0, -1.0f);
      }
      if (angle > startingAngle + birdieAngle) {
        Matrix.setRotateM(mRotationMatrixPar, 0, angle, 0, 0, -1.0f);
      }
      if (angle > startingAngle + birdieAngle + parAngle) {
        Matrix.setRotateM(mRotationMatrixBogie, 0, angle, 0, 0, -1.0f);
      }
      angle -= 2;
    }

    // Combine the rotation matrix with the projection and camera view
    // Note that the mMVPMatrix factor *must be first* in order
    // for the matrix multiplication product to be correct.
    Matrix.multiplyMM(shadowScratch1, 0, mMVPMatrix, 0, mRotationMatrixShadow, 0);
    Matrix.multiplyMM(bogieScratch, 0, mMVPMatrix, 0, mRotationMatrixBogie, 0);
    Matrix.multiplyMM(parScratch, 0, mMVPMatrix, 0, mRotationMatrixPar, 0);
    Matrix.multiplyMM(birdieScratch, 0, mMVPMatrix, 0, mRotationMatrixBirdie, 0);

    shadowPieWheel.draw(mMVPMatrix);
    wheelBG.draw(mMVPMatrix);
    bogiePieWheel.draw(bogieScratch);
    parPieWheel.draw(parScratch);
    birdiePieWheel.draw(birdieScratch);
    wheelHole.draw(mMVPMatrix);
  }
Example #4
0
  private void addBody(World world, float x, float y, boolean dirt) {
    final BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    final Body body = world.createBody(bodyDef);
    body.setTransform(x + 0.5f, y + 0.5f, 0);

    final CircleShape shape = new CircleShape();
    shape.setRadius(0.1f);
    final FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    final Fixture fixture = body.createFixture(fixtureDef);
    shape.dispose();
    if (dirt) {
      dirts.add(fixture);
    }
  }
Example #5
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 render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.position.set(circleBody.getPosition().x, circleBody.getPosition().y, 0);
    camera.update();
    renderer.render(world, camera.combined);
    System.out.println(
        "player.getPosition().x = "
            + player.getPosition().x
            + "\nplayer.getPosition().y = "
            + player.getPosition().y
            + "\ncamera.position = "
            + camera.position);
    Sprite sprite;
    sprite = (Sprite) circleBody.getUserData();
    // set position and width and height and makes sure it is in the center
    sprite.setBounds(
        convertToWorld(circleBody.getPosition().x) - sprite.getWidth() / 2,
        convertToWorld(circleBody.getPosition().y) - sprite.getHeight() / 2,
        convertToWorld(circleShape.getRadius() * 2),
        convertToWorld(circleShape.getRadius() * 2));

    tiledMapRenderer.setView(camera);
    tiledMapRenderer.render();

    System.out.println(
        "Bouncing circle: " + circleBody.getMass() + "\n" + "Player: " + player.getMass());

    // world.step(1/45f, 6, 2);
    world.step(Gdx.graphics.getDeltaTime(), 4, 4);
    player.setAwake(true);
    // camera.project(point.set(player.getPosition().x, player.getPosition().y, 0));

    // logger.log();
    batch.begin();
    // Circle.draw(batch);
    sprite.draw(batch);
    batch.end();
  }
  public PatrolShip() {
    // Creating a box2D entity
    BodyDef circleDef = new BodyDef();
    circleDef.type = BodyDef.BodyType.DynamicBody;

    body = com.astrodestroyer.Game.b2World.createBody(circleDef);

    shape = new CircleShape();
    shape.setRadius(radius);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;
    fixtureDef.filter.groupIndex = 1;
    fixture = body.createFixture(fixtureDef);

    Random rnd = new Random();
    body.setTransform(
        (rnd.nextFloat() - 0.5f) * 2f, (rnd.nextFloat() - 0.5f) * 2f, rnd.nextFloat() * 2f - 1f);
    body.setLinearVelocity(rnd.nextFloat() * 0.5f - 0.25f, rnd.nextFloat() * 0.5f - 0.25f);
  }
  @Override
  public void resize(int width, int height) {
    PPuX = width / 10;
    System.out.println("width = " + width);
    System.out.println("height = " + height);
    PPuY = height / 10;
    world = new World(new Vector2(0, -9.8f), false);
    renderer = new Box2DDebugRenderer();
    camera = new OrthographicCamera(width, height);
    debugMatrix = new Matrix4(camera.combined);

    Circle.setBounds(
        Circle.getX() * PPuX,
        Circle.getY() * PPuY,
        Circle.getWidth() * PPuX,
        Circle.getHeight() * PPuY);
    Circle.setOrigin(Circle.getWidth() / 2, Circle.getHeight() / 2);
    BodyDef circleDef = new BodyDef();
    circleDef.type = BodyType.DynamicBody;
    // To allign the Circle sprite with box 2d
    circleDef.position.set(
        convertToBox(Circle.getX() + Circle.getWidth() / 2),
        convertToBox(Circle.getY() + Circle.getHeight() / 2));
    circleBody = world.createBody(circleDef);
    // box2d builds around 0,0 you can see -X and -Y, this makes sure that you only see X,Y
    debugMatrix.translate(-camera.viewportWidth / 2, -camera.viewportHeight / 2, 0);
    // scale the debug matrix by the scaling so everything looks normal
    debugMatrix.scale(BOX_TO_WORLD, BOX_TO_WORLD, 0);
    circleShape = new CircleShape();
    circleShape.setRadius(convertToBox(Circle.getWidth() / 2));

    FixtureDef circleFixture = new FixtureDef();
    circleFixture.shape = circleShape;
    circleFixture.density = 0.4f;
    circleFixture.friction = 0.2f;
    circleFixture.restitution = 1f;

    circleBody.createFixture(circleFixture);
    circleBody.setUserData(Circle);

    // create ground
    BodyDef groundDef = new BodyDef();
    groundDef.position.set(convertToBox(camera.viewportWidth / 2), 0);

    Body groundBody = world.createBody(groundDef);

    PolygonShape groundBox = new PolygonShape();

    groundBox.setAsBox(convertToBox(camera.viewportWidth / 2), 0);
    groundBody.createFixture(groundBox, 0);

    BodyDef def = new BodyDef();
    def.type = BodyType.DynamicBody;
    def.position.set(0, 0);
    Body box = world.createBody(def);

    PolygonShape poly = new PolygonShape();
    poly.setAsBox(0.1f, 0.2f);
    playerPhysicsFixture = box.createFixture(poly, 1);
    poly.dispose();

    CircleShape circle = new CircleShape();
    circle.setRadius(0.1f);
    circle.setPosition(new Vector2(0, -0.2f));
    playerSensorFixture = box.createFixture(circle, 0);
    circle.dispose();

    box.setBullet(true);

    player = box;
    player.setTransform(1.0f, 2.0f, 0);
    player.setFixedRotation(true);
  }
Example #9
0
  @Override
  public void create() {
    cam = new OrthographicCamera(48, 32);
    cam.position.set(0, 15, 0);
    cam.update();

    Box2D.init();
    world = new World(new Vector2(0, -9.8f), true);
    dDebugRenderer = new Box2DDebugRenderer();

    spawnBoxes = false;
    // GROUND
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(0, 0);

    ground = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(15, 1);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0f;

    ground.createFixture(fixtureDef);

    // CIRCLE
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(0, 10);

    circle = world.createBody(bodyDef);

    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(1);

    fixtureDef.shape = circleShape;
    fixtureDef.density = 10;
    fixtureDef.restitution = 1;

    circle.createFixture(fixtureDef);

    // BOX
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(0, 15);

    box = world.createBody(bodyDef);

    bodyDef.bullet = false;
    shape.setAsBox(1, 1);

    fixtureDef.friction = 100f;
    fixtureDef.restitution = 0.5f;
    fixtureDef.shape = shape;
    fixtureDef.density = 2;

    box.createFixture(fixtureDef);

    // PLATFORM
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.position.set(-10, 6);

    platform = world.createBody(bodyDef);

    shape.setAsBox(3, .5f);

    fixtureDef.shape = shape;
    fixtureDef.density = 0;
    fixtureDef.friction = 7;
    fixtureDef.restitution = 0;

    platform.createFixture(fixtureDef);

    Gdx.input.setInputProcessor(new MyInputProcessor());

    platform.setLinearVelocity(PLATFORM_VELOCITY, 0);
  }