com.badlogic.gdx.physics.box2d.Body.getWorldCenter()方法的使用及代码示例

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

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

Body.getWorldCenter介绍

[英]Get the world position of the center of mass. Note that the same Vector2 instance is returned each time this method is called.
[中]获取质心的世界位置。请注意,每次调用此方法时都返回相同的Vector2实例。

代码示例

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

private void renderBox (Body body, float halfWidth, float halfHeight) {
  // get the bodies center and angle in world coordinates
  Vector2 pos = body.getWorldCenter();
  float angle = body.getAngle();
  // set the translation and rotation matrix
  transform.setToTranslation(pos.x, pos.y, 0);
  transform.rotate(0, 0, 1, (float)Math.toDegrees(angle));
  // render the box
  renderer.begin(ShapeType.Line);
  renderer.setTransformMatrix(transform);
  renderer.setColor(1, 1, 1, 1);
  renderer.rect(-halfWidth, -halfHeight, halfWidth * 2, halfHeight * 2);
  renderer.end();
}

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

public void applyLinearImpulse(Vector2 impulse) {
  body.applyLinearImpulse(impulse, body.getWorldCenter(), true);
}

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

public void jump(){
  if ( currentState != State.JUMPING ) {
    b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true);
    currentState = State.JUMPING;
  }
}

代码示例来源:origin: danialgoodwin/dev

public void jump() {
  if (!(jumping || dodging || hit)) {
    body.applyLinearImpulse(getUserData().getJumpingLinearImpulse(), body.getWorldCenter(), true);
    jumping = true;
  }
}

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

public void handleInput(float dt){
  //control our player using immediate impulses
  if(player.currentState != Mario.State.DEAD) {
    if (Gdx.input.isKeyJustPressed(Input.Keys.UP))
      player.jump();
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2)
      player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
      player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
    if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE))
      player.fire();
  }
}

代码示例来源:origin: narfman0/GDXWorld

@Override public CompletionEnum tick(float dt){
  PrismaticJoint joint = (PrismaticJoint)executor.getPhysicsJoint(this.joint);
  if(joint.isMotorEnabled() && towardA && executor.getPhysicsObject(name).getWorldCenter().dst(pointA) < TURN_DISTANCE){
    towardA = false;
    joint.setMaxMotorForce(maxMotorForceB);
    joint.setMotorSpeed(motorSpeedB);
    joint.enableMotor(false);
    executor.getPhysicsObject(name).setType(BodyType.StaticBody);
    timeChangeDirection = System.currentTimeMillis() + waitDuration;
  }else if(joint.isMotorEnabled() && !towardA && executor.getPhysicsObject(name).getWorldCenter().dst(pointB) < TURN_DISTANCE){
    towardA = true;
    joint.setMaxMotorForce(maxMotorForceA);
    joint.setMotorSpeed(motorSpeedA);
    joint.enableMotor(false);
    executor.getPhysicsObject(name).setType(BodyType.StaticBody);
    timeChangeDirection = System.currentTimeMillis() + waitDuration;
  }else if(!joint.isMotorEnabled() && System.currentTimeMillis() > timeChangeDirection){
    joint.enableMotor(true);
    executor.getPhysicsObject(name).setType(BodyType.DynamicBody);
  }
  return CompletionEnum.EXECUTING;
}

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

public void die() {
  if (!isDead()) {
    MarioBros.manager.get("audio/music/mario_music.ogg", Music.class).stop();
    MarioBros.manager.get("audio/sounds/mariodie.wav", Sound.class).play();
    marioIsDead = true;
    Filter filter = new Filter();
    filter.maskBits = MarioBros.NOTHING_BIT;
    for (Fixture fixture : b2body.getFixtureList()) {
      fixture.setFilterData(filter);
    }
    b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true);
  }
}

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

state.setCurrentState("walking_left");
if (body.getLinearVelocity().x > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(-enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_right");
if (body.getLinearVelocity().x < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_up");
if (body.getLinearVelocity().y < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
state.setCurrentState("walking_down");
if (body.getLinearVelocity().y > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, -enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);

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

state.setCurrentState("walking_left");
if (body.getLinearVelocity().x > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(-enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_right");
if (body.getLinearVelocity().x < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_up");
if (body.getLinearVelocity().y < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
state.setCurrentState("walking_down");
if (body.getLinearVelocity().y > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, -enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);

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

if (player.invincible || !hitBombVertical(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y + 0.5f))) {
  if (Math.abs(linearVelocity.y) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(0, player.acceleration * body.getMass()), body.getWorldCenter(), true);
if (player.invincible || !hitBombVertical(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y - 0.5f))) {
  if (Math.abs(linearVelocity.y) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(0, -player.acceleration * body.getMass()), body.getWorldCenter(), true);
if (player.invincible || !hitBombHorizontal(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x - 0.5f, body.getPosition().y))) {
  if (Math.abs(linearVelocity.x) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(-player.acceleration * body.getMass(), 0), body.getWorldCenter(), true);
if (player.invincible || !hitBombHorizontal(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x + 0.5f, body.getPosition().y))) {
  if (Math.abs(linearVelocity.x) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(player.acceleration * body.getMass(), 0), body.getWorldCenter(), true);

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

body.applyLinearImpulse(toVector.scl(enemy.getSpeed()), body.getWorldCenter(), true);

相关文章