com.badlogic.gdx.physics.box2d.Body类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(146)

本文整理了Java中com.badlogic.gdx.physics.box2d.Body类的一些代码示例,展示了Body类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Body类的具体详情如下:
包路径:com.badlogic.gdx.physics.box2d.Body
类名称:Body

Body介绍

[英]A rigid body. These are created via World.CreateBody.
[中]刚体。这些都是通过World创建的。CreateBody。

代码示例

代码示例来源:origin: libgdx/libgdx

/** Initialize the bodies and offsets using the current transforms. */
public void initialize (Body body1, Body body2) {
  this.bodyA = body1;
  this.bodyB = body2;
  this.linearOffset.set(bodyA.getLocalPoint(bodyB.getPosition()));
  this.angularOffset = bodyB.getAngle() - bodyA.getAngle();
}

代码示例来源:origin: libgdx/libgdx

public void create () {
  cam = new OrthographicCamera(48, 32);
  cam.position.set(0, 15, 0);
  renderer = new Box2DDebugRenderer();
  world = new World(new Vector2(0, -10), true);
  Body body = world.createBody(new BodyDef());
  CircleShape shape = new CircleShape();
  shape.setRadius(1f);
  MassData mass = new MassData();
  mass.mass = 1f;
  body.setMassData(mass);
  body.setFixedRotation(true);
  body.setType(BodyType.KinematicBody);
  body.createFixture(shape, 1);
  body.setBullet(true);
  body.setTransform(new Vector2(0, 0), body.getAngle());
  body.setLinearVelocity(new Vector2(50f, 0));
}

代码示例来源:origin: libgdx/libgdx

protected void renderBody (Body body) {
  Transform transform = body.getTransform();
  for (Fixture fixture : body.getFixtureList()) {
    if (drawBodies) {
      drawShape(fixture, transform, getColorByBody(body));
      if (drawVelocities) {
        Vector2 position = body.getPosition();
        drawSegment(position, body.getLinearVelocity().add(position), VELOCITY_COLOR);
      }
    }
    if (drawAABBs) {
      drawAABB(fixture, transform);
    }
  }
}

代码示例来源:origin: libgdx/libgdx

@Override
public boolean keyDown (int keyCode) {
  if (keyCode == Keys.D) m_platform.setType(BodyType.DynamicBody);
  if (keyCode == Keys.S) m_platform.setType(BodyType.StaticBody);
  if (keyCode == Keys.K) {
    m_platform.setType(BodyType.KinematicBody);
    m_platform.setLinearVelocity(tmp.set(-m_speed, 0));
    m_platform.setAngularVelocity(0);
  }
  return false;
}

代码示例来源:origin: libgdx/libgdx

/** Initialize the bodies, anchors, and reference angle using a world anchor point. */
public void initialize (Body bodyA, Body bodyB, Vector2 anchor) {
  this.bodyA = bodyA;
  this.bodyB = bodyB;
  localAnchorA.set(bodyA.getLocalPoint(anchor));
  localAnchorB.set(bodyB.getLocalPoint(anchor));
  referenceAngle = bodyB.getAngle() - bodyA.getAngle();
}

代码示例来源:origin: yichen0831/Bomberman_libGdx

case DYING:
  state.setCurrentState("dying");
  Filter filter = body.getFixtureList().get(0).getFilterData();
  filter.maskBits = GameManager.NOTHING_BIT;
  body.getFixtureList().get(0).setFilterData(filter);
      ActorBuilder actorBuilder = ActorBuilder.init(body.getWorld(), world);
      actorBuilder.createPortal();
      GameManager.getInstance().playSound("PortalAppears.ogg");
      ActorBuilder actorBuilder = ActorBuilder.init(body.getWorld(), world);
      actorBuilder.createPowerUp(body.getPosition().x, body.getPosition().y);
    body.getWorld().destroyBody(body);
    mRigidBody.set(entityId, false);
    mEnemy.set(entityId, false);
case WALKING_LEFT:
  state.setCurrentState("walking_left");
  if (body.getLinearVelocity().x > -enemy.getSpeed()) {
    body.applyLinearImpulse(new Vector2(-enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
  if (hitSomethingHorizontal(body, fromVector.set(body.getPosition()), toVector.set(body.getPosition().x - 0.5f, body.getPosition().y))) {
    changeWalkingState(enemy);
case WALKING_RIGHT:
  state.setCurrentState("walking_right");
  if (body.getLinearVelocity().x < enemy.getSpeed()) {
    body.applyLinearImpulse(new Vector2(enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);

代码示例来源:origin: libgdx/libgdx

@Override
public void render () {
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  cam.position.set(player.getPosition().x, player.getPosition().y, 0);
  cam.update();
  renderer.render(world, cam.combined);
  Vector2 vel = player.getLinearVelocity();
  Vector2 pos = player.getPosition();
  boolean grounded = isPlayerGrounded(Gdx.graphics.getDeltaTime());
  if (grounded) {
    player.setLinearVelocity(vel.x, vel.y);
    player.setLinearVelocity(vel.x * 0.9f, vel.y);
  } else {
    stillTime = 0;
      player.applyLinearImpulse(0, -24, pos.x, pos.y, true);
    player.applyLinearImpulse(-2f, 0, pos.x, pos.y, true);
    player.applyLinearImpulse(2f, 0, pos.x, pos.y, true);
    jump = false;
    if (grounded) {
      player.setLinearVelocity(vel.x, 0);
      System.out.println("jump before: " + player.getLinearVelocity());
      player.setTransform(pos.x, pos.y + 0.01f, 0);
      player.applyLinearImpulse(0, 40, pos.x, pos.y, true);

代码示例来源:origin: libgdx/libgdx

for (int i = 0; i < boxes.size(); i++) {
  Body box = boxes.get(i);
  Vector2 position = box.getPosition(); // that's the box's center position
  float angle = MathUtils.radiansToDegrees * box.getAngle(); // the rotation angle around the center
  batch.draw(textureRegion, position.x - 1, position.y - 1, // the bottom left corner of the box, unrotated
    1f, 1f, // the rotation center relative to the bottom left corner of the box

代码示例来源:origin: libgdx/libgdx

ground.createFixture(shape, 0.0f);
ground.createFixture(shape, 0);
shape.dispose();
  bd.position.set(xs[j] + x, 0.752f + 1.54f * i);
  Body body = world.createBody(bd);
  body.setUserData(n);
  body.createFixture(fd);

代码示例来源:origin: libgdx/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();
}

代码示例来源:origin: BrentAureli/SuperMario

@Override
  public void update(float dt) {
    super.update(dt);
    setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);
    velocity.y = body.getLinearVelocity().y;
    body.setLinearVelocity(velocity);
  }
}

代码示例来源:origin: libgdx/libgdx

public CirclePlatform (int x, int y, float radius, float da) {
  platform = createCircle(BodyType.KinematicBody, radius, 1);
  platform.setTransform(x, y, 0);
  platform.getFixtureList().get(0).setUserData("p");
  platform.setAngularVelocity(da);
  platform.setUserData(this);
}

代码示例来源:origin: manuelbua/uracer-kotd

protected void toNormalRelativeAngle () {
    // normalize body angle since it can grows unbounded
    float angle = AMath.normalRelativeAngle(body.getAngle());
    body.setTransform(body.getPosition(), angle);
  }
}

代码示例来源:origin: dozingcat/Vector-Pinball

public void draw(IFieldRenderer renderer) {
  CircleShape shape = (CircleShape)body.getFixtureList().get(0).getShape();
  Vector2 center = body.getPosition();
  float radius = shape.getRadius();
  renderer.fillCircle(center.x, center.y, radius, primaryColor);
  // Draw a smaller circle to show the ball's rotation.
  float angle = body.getAngle();
  float smallCenterX = center.x + (radius / 2) * MathUtils.cos(angle);
  float smallCenterY = center.y + (radius / 2) * MathUtils.sin(angle);
  renderer.fillCircle(smallCenterX, smallCenterY, radius / 4, secondaryColor);
}

代码示例来源:origin: libgdx/libgdx

/** Initialize the bodies, anchors, axis, and reference angle using the world anchor and world axis. */
public void initialize (Body bodyA, Body bodyB, Vector2 anchor, Vector2 axis) {
  this.bodyA = bodyA;
  this.bodyB = bodyB;
  localAnchorA.set(bodyA.getLocalPoint(anchor));
  localAnchorB.set(bodyB.getLocalPoint(anchor));
  localAxisA.set(bodyA.getLocalVector(axis));
  referenceAngle = bodyB.getAngle() - bodyA.getAngle();
}

代码示例来源:origin: BrentAureli/SuperMario

@Override
public void update(float dt) {
  setRegion(getFrame(dt));
  if(currentState == State.STANDING_SHELL && stateTime > 5){
    currentState = State.WALKING;
    velocity.x = 1;
    System.out.println("WAKE UP SHELL");
  }
  setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - 8 /MarioBros.PPM);
  b2body.setLinearVelocity(velocity);
}

代码示例来源:origin: dsaltares/libgdx-cookbook

@Override
public boolean mouseMoved(int screenX, int screenY) {
  viewport.getCamera().unproject(point.set(screenX, screenY, 0));
  pointerBody.setTransform(point.x, point.y, pointerBody.getAngle() + 5 * MathUtils.degreesToRadians);
  return true;
}

代码示例来源:origin: libgdx/libgdx

protected void renderBody (Body body) {
  Transform transform = body.getTransform();
  int len = body.getFixtureList().size;
  Array<Fixture> fixtures = body.getFixtureList();
  for (int i = 0; i < len; i++) {
    Fixture fixture = fixtures.get(i);
    if (drawBodies) {
      if (body.isActive() == false && drawInactiveBodies)
        drawShape(fixture, transform, SHAPE_NOT_ACTIVE);
      else if (body.getType() == BodyType.StaticBody)
        drawShape(fixture, transform, SHAPE_STATIC);
      else if (body.getType() == BodyType.KinematicBody)
        drawShape(fixture, transform, SHAPE_KINEMATIC);
      else if (body.isAwake() == false)
        drawShape(fixture, transform, SHAPE_NOT_AWAKE);
      else
        drawShape(fixture, transform, SHAPE_AWAKE);
    }
    if (drawAABBs) {
      drawAABB(fixture, transform);
    }
  }
}

代码示例来源:origin: libgdx/libgdx

@Override
  public void render () {
    if (m_platform.getType() == BodyType.KinematicBody) {
      Vector2 p = m_platform.getTransform().getPosition();
      Vector2 v = m_platform.getLinearVelocity();

      if ((p.x < -10 && v.x < 0) || (p.x > 10 && v.x > 0)) {
        v.x = -v.x;
        m_platform.setLinearVelocity(v);
      }
    }

    super.render();

    // if (renderer.batch != null) {
    // renderer.batch.begin();
    // // renderer.batch.drawText(renderer.font, "Keys: (d) dynamic, (s) static, (k) kinematic", 0, Gdx.app.getGraphics()
    // // .getHeight(), Color.WHITE);
    // renderer.batch.end();
    // }
  }
}

代码示例来源:origin: libgdx/libgdx

@Override
protected void createWorld (World world) {
  {
    BodyDef bd = new BodyDef();
    bd.position.set(0, 0);
    Body body = world.createBody(bd);
    EdgeShape shape = new EdgeShape();
    shape.set(new Vector2(-10, 0), new Vector2(10, 0));
    body.createFixture(shape, 0);
    shape.dispose();
    PolygonShape poly = new PolygonShape();
    poly.setAsBox(0.2f, 1.0f, new Vector2(0.5f, 1.0f), 0);
    body.createFixture(poly, 0);
    poly.dispose();
  }
  {
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DynamicBody;
    bd.position.set(0, 20);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(2, 0.1f);
    m_body = world.createBody(bd);
    m_body.createFixture(shape, 1);
    m_angularVelocity = 33.468121f;
    m_body.setLinearVelocity(new Vector2(0, -100));
    m_body.setAngularVelocity(m_angularVelocity);
    shape.dispose();
  }
}

相关文章