コード例 #1
0
ファイル: Box2DTest.java プロジェクト: WaDelma/libgdx
  @Override
  public boolean touchDown(int x, int y, int pointer, int newParam) {
    // translate the mouse coordinates to world coordinates
    testPoint.set(x, y, 0);
    camera.unproject(testPoint);

    // ask the world which bodies are within the given
    // bounding box around the mouse pointer
    hitBody = null;
    world.QueryAABB(
        callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);

    // if we hit something we create a new mouse joint
    // and attach it to the hit body.
    if (hitBody != null) {
      MouseJointDef def = new MouseJointDef();
      def.bodyA = groundBody;
      def.bodyB = hitBody;
      def.collideConnected = true;
      def.target.set(testPoint.x, testPoint.y);
      def.maxForce = 1000.0f * hitBody.getMass();

      mouseJoint = (MouseJoint) world.createJoint(def);
      hitBody.setAwake(true);
    } else {
      for (Body box : boxes) world.destroyBody(box);
      boxes.clear();
      createBoxes();
    }

    return false;
  }
コード例 #2
0
  @Override
  public void initialize() {
    super.initialize();

    // doStep true = not simulate inactive bodies
    world = new World(new Vector2(0.0f, 10.0f), true);
    world.setContinuousPhysics(true);
    world.setWarmStarting(true);
    world.setAutoClearForces(true);
    ValueMap valueMap = game.getGameState();
    valueMap.setValue((String) null, VAR_PH_WORLD, world);

    velocityIterations = 24;
    positionIterations = 8;

    for (SceneElement e : effect.getElements()) {
      createBody(world, e, valueMap);
    }

    for (SceneElement e : effect.getJoints()) {
      createBody(world, e, valueMap);
    }

    RevoluteJointDef jd = new RevoluteJointDef();
    jd.collideConnected = false;

    for (int i = 0; i < effect.getJoints().size() - 1; i += 2) {
      SceneElement e1 = effect.getJoints().get(i);
      SceneElement e2 = effect.getJoints().get(i + 1);
      Body b1 = (Body) valueMap.getValue(e1.getId(), VAR_PH_BODY, null);
      Body b2 = (Body) valueMap.getValue(e2.getId(), VAR_PH_BODY, null);
      jd.initialize(b2, b1, new Vector2(b1.getPosition().x, b1.getPosition().y));
      world.createJoint(jd);
    }
  }
コード例 #3
0
  public void MakeBody(
      float width,
      float height,
      float radius,
      BodyDef.BodyType bodyType,
      float density,
      float restitution,
      Vector2 pos,
      float angle) {
    World world = BoxObjectManager.GetWorld();
    BodyDef jumperBodyDef = new BodyDef();
    jumperBodyDef.type = bodyType;

    jumperBodyDef.position.set(
        BoxObjectManager.ConvertToBox(pos.x), BoxObjectManager.ConvertToBox(pos.y));

    jumperBodyDef.angle = angle;

    body = world.createBody(jumperBodyDef);
    /** Boxes are defined by their "half width" and "half height", hence the 2 multiplier. */
    if (radius == 0) {
      MakeRectBody(width, height, bodyType, density, restitution, pos, angle);

    } else {
      MakeCircleBody(radius, bodyType, density, restitution, pos, angle);
    }

    /** The character should not ever spin around on impact. */
    bodyWorldPosition.set(
        BoxObjectManager.ConvertToWorld(body.getPosition().x),
        BoxObjectManager.ConvertToWorld(body.getPosition().y));
  }
コード例 #4
0
  public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // tell the camera to update its matrices.
    camera.update(
        car.body.getPosition().x * PIXELS_PER_METER, car.body.getPosition().y * PIXELS_PER_METER);

    spriteBatch.setProjectionMatrix(camera.getCombined());

    if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isTouched()) {
      car.setAccelerate(Car.ACC_ACCELERATE);
    } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
      car.setAccelerate(Car.ACC_BRAKE);
    } else {
      car.setAccelerate(Car.ACC_NONE);
    }

    if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.getAccelerometerY() < -2.5) {
      car.setSteer(Car.STEER_HARD_LEFT);
    } else if (Gdx.input.getAccelerometerY() < -1) {
      car.setSteer(Car.STEER_LEFT);
    } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.getAccelerometerY() > 2.5) {
      car.setSteer(Car.STEER_HARD_RIGHT);
    } else if (Gdx.input.getAccelerometerY() > 1) {
      car.setSteer(Car.STEER_RIGHT);
    } else {
      car.setSteer(Car.STEER_NONE);
    }

    car.update(Gdx.app.getGraphics().getDeltaTime());

    /**
     * Have box2d update the positions and velocities (and etc) of all tracked objects. The second
     * and third argument specify the number of iterations of velocity and position tests to perform
     * -- higher is more accurate but is also slower.
     */
    world.step(Gdx.app.getGraphics().getDeltaTime(), 3, 3);

    world.clearForces();

    // draw the sprites
    spriteBatch.begin();

    playerSprite.setPosition(
        PIXELS_PER_METER * car.body.getPosition().x - playerTexture.getRegionWidth() / 2,
        PIXELS_PER_METER * car.body.getPosition().y - playerTexture.getRegionHeight() / 2);
    playerSprite.setRotation((MathUtils.radiansToDegrees * car.body.getAngle()));

    playerSprite.setFlip(false, true);
    playerSprite.setScale(0.3f);

    playerSprite.draw(spriteBatch);

    spriteBatch.end();

    /** Draw this last, so we can see the collision boundaries on top of the sprites and map. */
    debugRenderer.render(
        world, camera.getCombined().scale(PIXELS_PER_METER, PIXELS_PER_METER, PIXELS_PER_METER));
  }
コード例 #5
0
 public void destroyLevel() {
   for (Obstacle obstacle : obs) {
     gameWorldPhysics.destroyBody(obstacle.getBody());
   }
   obs.clear();
   gameWorldPhysics.destroyBody(goal.getBody());
   goal = null;
 }
コード例 #6
0
    void onGameStart(Nothing ignored) throws InterruptedException {
      System.out.println("Physics: on game start");
      if (world != null) world.dispose();
      world = new World(new Vector2(0, Constants.WORLD_GRAVITY), true);

      world.setContactListener(new HitTester((isOnGround) -> isPlayerOnFloor = isOnGround));

      _createPlayer();

      state = GameState.IN_GAME;
    }
コード例 #7
0
  /*Boundaries game screen*/
  private void createLayout() {
    // down bound
    BodyDef bdef = new BodyDef();
    bdef.position.set(0 / GameScreenManager.PPM_W, referansDown / GameScreenManager.PPM_H);
    bdef.type = BodyDef.BodyType.StaticBody;
    Body body = world.createBody(bdef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(GameScreenManager.WIDTH, 1);
    FixtureDef fdef = new FixtureDef();
    fdef.shape = shape;
    fdef.filter.categoryBits = CollisionVar.BIT_SCREEN;
    fdef.filter.maskBits =
        CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER;

    body.createFixture(fdef).setUserData("down");
    // top bound
    bdef.position.set(0 / GameScreenManager.PPM_W, referansTop / GameScreenManager.PPM_H);
    bdef.type = BodyDef.BodyType.StaticBody;
    body = world.createBody(bdef);

    shape.setAsBox(GameScreenManager.WIDTH, 1);
    fdef.shape = shape;
    fdef.filter.categoryBits = CollisionVar.BIT_SCREEN;
    fdef.filter.maskBits =
        CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER;

    body.createFixture(fdef).setUserData("top");

    // left bound
    bdef.position.set(0 / GameScreenManager.PPM_W, 80 / GameScreenManager.PPM_H);
    bdef.type = BodyDef.BodyType.StaticBody;
    body = world.createBody(bdef);

    shape.setAsBox(0, GameScreenManager.HEIGHT);
    fdef.shape = shape;
    fdef.filter.categoryBits = CollisionVar.BIT_SCREEN;
    fdef.filter.maskBits =
        CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER;
    body.createFixture(fdef).setUserData("left");

    // right bound
    bdef.position.set(GameScreenManager.WIDTH + 1, 0 / GameScreenManager.PPM_H);
    bdef.type = BodyDef.BodyType.StaticBody;
    body = world.createBody(bdef);
    fdef.filter.categoryBits = CollisionVar.BIT_SCREEN;
    fdef.filter.maskBits =
        CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER;

    shape.setAsBox(0, GameScreenManager.HEIGHT);
    fdef.shape = shape;
    body.createFixture(fdef).setUserData("right");
  }
コード例 #8
0
ファイル: IceSupports.java プロジェクト: jbouton/PenPop
  public void createIceBall() {
    // gear base
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.StaticBody;
    bodyDef.position.x = x;
    bodyDef.position.y = y;
    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(1);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.isSensor = true;
    fixtureDef.shape = circleShape;
    gearBody = world.createBody(bodyDef);
    gearBody.createFixture(fixtureDef);

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

    BodyDef boxBodyDef = new BodyDef();
    boxBodyDef.type = BodyType.DynamicBody;
    boxBodyDef.position.x = x;
    boxBodyDef.position.y = y;

    fixtureDef = new FixtureDef();
    fixtureDef.shape = circleShape;
    fixtureDef.friction = 0f;
    fixtureDef.density = 4.10f;
    fixtureDef.restitution = 1.5f;
    fixtureDef.isSensor = true;
    fixtureDef.filter.groupIndex = -1;
    gearBase = world.createBody(boxBodyDef);
    SpriteInfo spriteTemp = new SpriteInfo(assets.getSprite("star"));
    spriteTemp.setName("star");
    gearBase.setUserData(spriteTemp);
    gearBase.createFixture(fixtureDef);
    // iceBody.createFixture(circleShape, .1f);

    RevoluteJointDef wheeljointDef = new RevoluteJointDef();
    wheeljointDef.bodyA = gearBase;
    wheeljointDef.bodyB = gearBody;
    wheeljointDef.localAnchorA.y = 0;
    wheeljointDef.localAnchorB.y = 0;
    wheeljointDef.motorSpeed = .5f;
    wheeljointDef.enableMotor = true;
    wheeljointDef.maxMotorTorque = 100000f;
    // Joint joint =
    // wheeljointDef.bodyA.getWorld().createJoint(wheeljointDef);
    world.createJoint(wheeljointDef);

    circleShape.dispose();
  }
コード例 #9
0
ファイル: Physics.java プロジェクト: TCMOREIRA/PSPACE
  /**
   * * Filtered version of createRectangularBody Category is who I am, Group filters GameObjects of
   * the same class as mine so we dont collide, Mask is who is allowed to collide With Me Default
   * filter values: categoryBits = 0x0001, maskBits = -1, groupIndex = 0 Leaving them at default
   * would be the same as not passing such argument (use if that is the intended result) **
   */
  public static Body createRectangularBody(
      float x,
      float y,
      float halfWidth,
      float halfHeight,
      short category,
      short mask,
      short group,
      GameEntity myGameObjectIndentifier) {
    bodyDef.type = BodyType.DynamicBody;
    // The body is originally instantiated at the Bottom-Left position
    bodyDef.position.set((x + halfWidth) / PPM, (y + halfHeight) / PPM);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(halfWidth / PPM, halfHeight / PPM);

    fixtureDef.shape = shape;
    fixtureDef.filter.categoryBits = category;
    fixtureDef.filter.maskBits = mask;
    fixtureDef.filter.groupIndex = group;

    Body body = WORLD.createBody(bodyDef);
    body.createFixture(fixtureDef).setUserData(myGameObjectIndentifier);
    body.setLinearDamping(1);

    shape.dispose();

    return body;
  }
コード例 #10
0
ファイル: Physics.java プロジェクト: TCMOREIRA/PSPACE
  public static void simulateWorld() {

    // Step to update time and accumulator
    Physics.accumulateTimeStep();

    isLockedLocal = false;
    // step as many times it needs to
    while (accumulator >= Physics.TIMESTEP) {
      isLockedLocal = true;

      ///// WORLD Step////////////////////
      /** */
      WORLD.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
      ///// WORLD Step////////////////////

      cleanRemovableBodies();

      accumulator -= Physics.TIMESTEP;
    }
    isLockedLocal = false;

    // Interpolate the remainder from accumulator
    // Obs.: nice way to also attribute the remainder to alphaTime at the same time
    Physics.interpolateTimeStep((alphaTime = (float) accumulator / Physics.TIMESTEP));
  }
コード例 #11
0
ファイル: Box2DTest.java プロジェクト: WaDelma/libgdx
  private void createBoxes() {
    // next we create 50 boxes at random locations above the ground
    // body. First we create a nice polygon representing a box 2 meters
    // wide and high.
    PolygonShape boxPoly = new PolygonShape();
    boxPoly.setAsBox(1, 1);

    // next we create the 50 box bodies using the PolygonShape we just
    // defined. This process is similar to the one we used for the ground
    // body. Note that we reuse the polygon for each body fixture.
    for (int i = 0; i < 20; i++) {
      // Create the BodyDef, set a random position above the
      // ground and create a new body
      BodyDef boxBodyDef = new BodyDef();
      boxBodyDef.type = BodyType.DynamicBody;
      boxBodyDef.position.x = -24 + (float) (Math.random() * 48);
      boxBodyDef.position.y = 10 + (float) (Math.random() * 100);
      Body boxBody = world.createBody(boxBodyDef);

      boxBody.createFixture(boxPoly, 1);

      // add the box to our list of boxes
      boxes.add(boxBody);
    }

    // we are done, all that's left is disposing the boxPoly
    boxPoly.dispose();
  }
コード例 #12
0
  @Override
  public void render() {
    // Update
    //		Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    world.step(1 / 60f, 10, 10);
    for (int i = 0; i < MAX_BALL_COUNT; i++) {
      Vector2 pos =
          ballModels[i]
              .getPosition()
              .sub(ballSprites[i].getWidth() / 2, ballSprites[i].getHeight() / 2);
      float angleDeg = ballModels[i].getAngle() * MathUtils.radiansToDegrees;

      ballSprites[i].setPosition(pos.x, pos.y);
      ballSprites[i].setRotation(angleDeg);
    }

    // Render
    //		GL10 gl = Gdx.gl10;
    //		gl.glClearColor(1, 1, 1, 1);
    //		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    spriteBatch.setProjectionMatrix(camera.combined);
    spriteBatch.begin();
    vialSprite.draw(spriteBatch);
    for (int i = 0; i < MAX_BALL_COUNT; i++) ballSprites[i].draw(spriteBatch);
    spriteBatch.end();

    spriteBatch
        .getProjectionMatrix()
        .setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    spriteBatch.begin();
    font.draw(spriteBatch, "Touch the screen to restart", 5, 25);
    spriteBatch.end();
  }
コード例 #13
0
 public void dispose() {
   m_BorderTexture.dispose();
   m_BackgroundMusic.stop();
   m_BackgroundMusic.dispose();
   m_RayHandler.dispose();
   m_PhysicsWorld.dispose();
 }
コード例 #14
0
  public static void createBall(Ball ball) {
    int index = 0;

    BodyDef bdef = new BodyDef();
    Vector2 ballPosition =
        new Vector2(ball.position.x + ball.length.x / 2, ball.position.y + ball.length.y / 2);
    bdef.position.set(ball.position);
    bdef.type = BodyDef.BodyType.DynamicBody;
    Body body;
    synchronized (world) {
      body = world.createBody(bdef);
    }
    CircleShape shape = new CircleShape();
    System.out.println("length : " + ball.length.y);
    shape.setRadius(ball.length.y);

    synchronized (PlayState.ballLinkedHashMap) {
      ball.body = body;
      PlayState.ballLinkedHashMap.put(PlayState.ballLinkedHashMap.size(), ball);
      index = PlayState.ballLinkedHashMap.size();

      FixtureDef fdef = new FixtureDef();
      fdef.shape = shape;
      fdef.restitution = (float) 1.1;
      fdef.filter.categoryBits = CollisionVar.BIT_BALL;
      fdef.filter.maskBits =
          CollisionVar.BIT_SCREEN | CollisionVar.BIT_PLAYER | CollisionVar.BIT_WEAPON;

      ball.body.createFixture(fdef).setUserData("ball" + (index - 1));
    }

    float angle = (float) (Math.atan2(ball.direction.y, ball.direction.x));
    ball.body.setLinearVelocity(new Vector2(45 * MathUtils.cos(angle), 45 * MathUtils.sin(angle)));
  }
コード例 #15
0
ファイル: DemoCR.java プロジェクト: rchampa/Prototypes
  @Override
  public void dispose() {

    planetTexture.dispose();
    backgroundTexture.dispose();
    planetCoreTexture.dispose();
    ballTexture.dispose();
    batch.dispose();
    font.dispose();

    // disposing bodies
    world.destroyBody(planetCoreModel);
    world.destroyBody(planetModel);
    for (int i = 0; i < MAX_BALLS; i++) world.destroyBody(ballModels[i]);
    world.dispose();
  }
コード例 #16
0
  public void defineMario() {
    BodyDef bdef = new BodyDef();
    bdef.position.set(1, 1);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    PolygonShape cuerpito = new PolygonShape();

    Vector2[] cuerpo = new Vector2[4];
    cuerpo[0] = new Vector2(-10, 20).scl(1 / MegaCatastrofeNuclear.PPM);
    cuerpo[1] = new Vector2(5, 20).scl(1 / MegaCatastrofeNuclear.PPM);
    cuerpo[2] = new Vector2(-10, -25).scl(1 / MegaCatastrofeNuclear.PPM);
    cuerpo[3] = new Vector2(5, -25).scl(1 / MegaCatastrofeNuclear.PPM);

    cuerpito.set(cuerpo);

    fdef.filter.categoryBits = MegaCatastrofeNuclear.PLAYER_BIT;
    fdef.filter.maskBits =
        MegaCatastrofeNuclear.GROUND_BIT
            | MegaCatastrofeNuclear.COIN_BIT
            | MegaCatastrofeNuclear.BRICK_BIT
            | MegaCatastrofeNuclear.ENEMY_BIT
            | MegaCatastrofeNuclear.OBJECT_BIT
            | MegaCatastrofeNuclear.ENEMYY_BIT
            | MegaCatastrofeNuclear.ITEM_BIT;

    fdef.shape = cuerpito;
    b2body.createFixture(fdef);
  }
コード例 #17
0
  public FinishFlag(World world, float x, float y) {
    this.world = world;

    initPoly(x * 2, y);

    // Rectangle
    // shape.setAsBox(5, 5);
    poly.set(
        new Vector2[] {
          new Vector2(0, 0),
          new Vector2(0, 25),
          new Vector2(2, 0),
          new Vector2(7, 22.5f),
          new Vector2(2, 25)
        });

    // Fixture definition
    fixDef.density = 1f;
    fixDef.friction = 0;
    fixDef.restitution = 0;
    fixDef.isSensor = true;

    body = world.createBody(bodyDef);
    body.setUserData(7);
    body.createFixture(fixDef);
  }
コード例 #18
0
ファイル: RobotNinjas.java プロジェクト: jmintb/ninjaRun
  public void setUp() {

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x + width / 2, y + height / 2);
    bodyDef.type = BodyDef.BodyType.KinematicBody;

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(width / 2, height / 2);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;

    body = world.createBody(bodyDef);
    body.createFixture(fixtureDef);
    body.setUserData(Constants.robotNinjaID);

    body.setLinearVelocity(Constants.standardVelocity);

    sprite = new Sprite(texture);
    sprite.setSize(width, height);
    sprite.setOrigin(width / 2, height / 2);
    sprite.flip(false, flipy);
    sprite.setPosition(x, y);

    dead = false;
  }
コード例 #19
0
ファイル: DemoCR.java プロジェクト: rchampa/Prototypes
  private void updatePhysics() {
    // Update
    // float deltaTime = Gdx.graphics.getRawDeltaTime();
    float deltaTime = 1 / 60f;
    // System.out.println(deltaTime);
    tweenManager.update(deltaTime); // Setting delta time
    world.step(deltaTime, 10, 10);

    Vector2 planetPos = planetModel.getPosition().sub(planetModelOrigin);
    planetSprite.setPosition(planetPos.x, planetPos.y);
    planetSprite.setOrigin(planetModelOrigin.x, planetModelOrigin.y);
    planetSprite.setRotation(planetModel.getAngle() * MathUtils.radiansToDegrees);

    Vector2 planetCorePos = planetCoreModel.getPosition().sub(planetCoreModelOrigin);
    planetCoreSprite.setPosition(planetCorePos.x, planetCorePos.y);
    planetCoreSprite.setOrigin(planetCoreModelOrigin.x, planetCoreModelOrigin.y);
    planetCoreSprite.setRotation(planetCoreModel.getAngle() * MathUtils.radiansToDegrees);

    for (int i = 0; i < MAX_BALLS; i++) {
      Vector2 ballPos = ballModels[i].getPosition();
      ballSprites[i].setPosition(
          ballPos.x - ballSprites[i].getWidth() / 2, ballPos.y - ballSprites[i].getHeight() / 2);
      ballSprites[i].setRotation(ballModels[i].getAngle() * MathUtils.radiansToDegrees);
    }
  }
コード例 #20
0
ファイル: SSGame.java プロジェクト: AdamAwan/SpaceShooter
  @Override
  public void create() {
    tweenManager = new TweenManager();
    Tween.registerAccessor(Sprite.class, new SpriteAccessor());
    // Box2d
    world = new World(new Vector2(0, 0), true); // No gravity, space dah.
    world.step(1 / 60f, 6, 2);
    rayHandler = new RayHandler(world);
    rayHandler.setAmbientLight(0, 0, 0, 1f);
    // rayHandler.setAmbientLight(1f, 1f, 1f, 1f);
    rayHandler.setCulling(true);
    rayHandler.setBlur(true);
    rayHandler.setBlurNum(3);
    rayHandler.setShadows(true);
    RayHandler.isDiffuse = true;
    RayHandler.setGammaCorrection(true);

    controls = new GameControls(this);
    Gdx.input.setCursorCatched(true);
    camera = new OrthographicCamera(1080, 1920);
    spriteBatch = new SpriteBatch();
    player = new PlayerShip(rayHandler, tweenManager);

    background = new Texture("backgroundbw.png");

    enemies = new EnemyHandler(rayHandler, tweenManager);
  }
コード例 #21
0
ファイル: PezBasic.java プロジェクト: vandalo/AquariumDeluxe
  public void initBody(World world, int playerNum) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    // bodyDef.fixedRotation = true;
    bodyDef.linearDamping = 0.0f;
    bodyDef.position.set(getX() + getWidth() / 2, getY() + getHeight() / 2);
    body = world.createBody(bodyDef);
    body.setUserData(this);
    // ((Sprite)body.getUserData()).setPosition(body.getPosition().x,body.getPosition().y);

    // PolygonShape shape = new PolygonShape();
    CircleShape shape = new CircleShape();
    shape.setRadius(getWidth() / 2);
    // shape.setAsBox(player.getWidth()/2 / 1, player.getHeight()/2 / 1);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1.1f;
    fixtureDef.restitution = 0.6f;
    fixtureDef.friction = 0.0f;
    if (playerNum == 0) {
      fixtureDef.filter.categoryBits = PECES;
      fixtureDef.filter.maskBits = ENEMIGO;
    } else {
      fixtureDef.filter.categoryBits = ENEMIGO;
      fixtureDef.filter.maskBits = PECES;
    }
    // fixtureDef.isSensor = true; --> use it on towers not to react but detect collision
    body.createFixture(fixtureDef);
    shape.dispose();
  }
コード例 #22
0
ファイル: physics.java プロジェクト: weihanli101/FlickyBall
  public void render() { // the artistic drawing function that draws everything out (such talent)
    // make the frame ticker thing that actually makes pretty pictures move
    // in this case you are updating the world you just made
    // apparently you aren't suppose to update this in the render loop but w/e
    // also have no idea what 6 & 2 mean so again w/e (sensai plssss)
    world.step(Gdx.graphics.getDeltaTime(), 6, 2);

    // move the sprite with the body!
    sprite.setPosition(body.getPosition().x, body.getPosition().y);

    // just a limit for when it falls off the screen since there is no ground LOL
    System.out.println(body.getPosition().y);
    if (body.getPosition().y < (10)) {
      body.setAwake(false);
    } else {
      body.setAwake(true);
    }

    // Again not box2dStuff just cool things I added
    HandleTouch();

    // the Background color
    // This doesn't look like it does anything?    Gdx.gl.glClearColor(1, 1, 1, 1);

    // clears the background after each fram update
    // Same with this?    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // this is where the sprite img is rendered and updated etc.
    batch.begin();
    batch.draw(sprite, sprite.getX(), sprite.getY());
    batch.end();
  }
コード例 #23
0
  private void createCoins() {
    coins = new Array<Coin>();

    MapLayer layer = tileMap.getLayers().get("Coins");
    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    for (MapObject mo : layer.getObjects()) {
      bdef.type = BodyType.StaticBody;
      float x = mo.getProperties().get("x", Float.class) / PPM;
      float y = mo.getProperties().get("y", Float.class) / PPM;

      bdef.position.set(x, y);
      CircleShape cShape = new CircleShape();
      cShape.setRadius(8 / PPM);
      fdef.shape = cShape;
      fdef.isSensor = true;
      fdef.filter.categoryBits = B2DVars.BIT_COINS;
      fdef.filter.maskBits = B2DVars.BIT_PLAYER;
      Body body = world.createBody(bdef);
      body.createFixture(fdef).setUserData("coin");
      Coin c = new Coin(body);
      coins.add(c);

      body.setUserData(c);
    }
  }
コード例 #24
0
ファイル: Mario.java プロジェクト: GeorgebigG/Mario_Bros
  public void defineMario() {
    BodyDef bDef = new BodyDef();
    bDef.position.set(32 / MarioBros.PPM, 32 / MarioBros.PPM);
    bDef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bDef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(6 / MarioBros.PPM);
    fdef.filter.categoryBits = MarioBros.MARIO_BIT;
    fdef.filter.maskBits =
        MarioBros.GROUND_BIT
            | MarioBros.COIN_BIT
            | MarioBros.BRICK_BIT
            | MarioBros.ENEMY_BIT
            | MarioBros.OBJECT_BIT
            | MarioBros.ENEMY_HEAD_BIT
            | MarioBros.ITEM_BIT;

    fdef.shape = shape;
    b2body.createFixture(fdef);

    EdgeShape head = new EdgeShape();
    head.set(
        new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM),
        new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM));
    fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT;
    fdef.filter.maskBits = MarioBros.BRICK_BIT | MarioBros.COIN_BIT;
    fdef.shape = head;
    fdef.isSensor = true;

    b2body.createFixture(fdef).setUserData(this);
  }
コード例 #25
0
ファイル: HW3.java プロジェクト: KnightPickles/CS328
  @Override
  public void render() {
    // Update
    getInput();

    b2dWorld.step(1f / 60f, 6, 2);

    camera.update();
    cameraBak.update();
    manager.update();
    background.update();

    // Draw
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    // Background
    batch.setProjectionMatrix(cameraBak.combined);
    batch.begin();
    background.draw(batch);
    batch.end();

    batch.setProjectionMatrix(camera.combined);
    debugMatrix = batch.getProjectionMatrix().cpy().scale(PPM, PPM, 0);
    // debugRenderer.render(b2dWorld, debugMatrix);
    batch.begin();
    manager.draw(batch);
    batch.end();

    // GUI
    batch.setProjectionMatrix(cameraBak.combined);
    batch.begin();
    drawGUI(batch);
    batch.end();
  }
コード例 #26
0
ファイル: PhysicsWorld.java プロジェクト: Juginabi/ProjectGDX
 public void createCheckpointSensor(float x, float y, Vector2 heading) {
   // First we create a body definition
   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.StaticBody;
   // Set our body's starting position in the world
   bodyDef.position.set(x, y);
   // Create our body in the world using our body definition
   Body body = world_.createBody(bodyDef);
   // Create a circle shape and set its radius to 6
   CircleShape circle = new CircleShape();
   circle.setRadius(0.25f);
   // Create a fixture definition to apply our shape to
   FixtureDef fixtureDef = new FixtureDef();
   fixtureDef.shape = circle;
   fixtureDef.isSensor = true;
   fixtureDef.filter.maskBits = ENTITY_ENEMY;
   fixtureDef.filter.categoryBits = SENSOR_NAVIGATION;
   // Create our fixture and attach it to the body
   Fixture fix = body.createFixture(fixtureDef);
   fix.setUserData(heading);
   // Remember to dispose of any shapes after you're done with them!
   // BodyDef and FixtureDef don't need disposing, but shapes do.
   circle.dispose();
 }
コード例 #27
0
  @Override
  public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camera.update();
    mainGame.spriteBatch.setProjectionMatrix(camera.combined);

    world.step(1 / 45f, 6, 2);

    // Renderable.update
    for (Renderable r : renderables) {
      r.update();
    }

    if (Gdx.input.isTouched()) {
      Gdx.app.debug(TAG, "Fire");
    }

    mainGame.spriteBatch.begin();

    // Renderable.render
    for (Renderable r : renderables) {
      r.render(mainGame.spriteBatch);
    }

    mainGame.spriteBatch.end();
  }
コード例 #28
0
 public void dispose() {
   renderer.dispose();
   world.dispose();
   renderer = null;
   world = null;
   hitBody = null;
 }
コード例 #29
0
ファイル: Main.java プロジェクト: MurderousFrog/NikosWorld
  private void update(float deltaTime) {
    world.step(1 / 60f, 6, 2);

    inputUpdate(deltaTime);
    cameraUpdate(deltaTime);
    batch.setProjectionMatrix(camera.combined);
  }
コード例 #30
0
  public void render(OrthographicCamera camera) {
    // update the world with a fixed time step
    world.step(Gdx.app.getGraphics().getDeltaTime() * 10, 8, 3);
    if (mouseJoint != null) mouseJoint.setTarget(bob.getBody().getWorldCenter());

    // Destroy mouseJoint? (drop item)
    if (destroyMousejoint == true) {
      if (!world.isLocked()) {
        world.destroyJoint(mouseJoint);
        mouseJoint = null;
        destroyMousejoint = false;
      }
    }

    // Delete any bodies up for deletion
    if (!bodiesToDelete.isEmpty()) {
      // Make sure it is safe to delete!!
      if (!world.isLocked()) {
        for (Body body : bodiesToDelete) {
          world.destroyBody(body);
          body.setUserData(null);
          body = null;
        }
        bodiesToDelete.clear(); // Don't forget to clear the null bodies!
      }
    }

    // Create any bodies up for creation
    if (!bodiesToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (BodyDef body : bodiesToCreate) {
          world.createBody(body);
        }
        bodiesToCreate.clear(); // Don't forget to clear!
      }
    }

    // Create any joints up for creation
    if (!jointsToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (JointDef body : jointsToCreate) {
          mouseJoint = (MouseJoint) world.createJoint(body);
        }
        jointsToCreate.clear(); // Don't forget to clear!
      }
    }
    // render the world using the debug renderer
    renderer.render(world, camera.combined);
  }