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

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

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

Body.getLinearVelocity介绍

[英]Get the linear velocity of the center of mass. Note that the same Vector2 instance is returned each time this method is called.
[中]得到质心的线速度。请注意,每次调用此方法时都返回相同的Vector2实例。

代码示例

代码示例来源: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

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

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, 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);
    System.out.println("jump, " + player.getLinearVelocity());

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

public Vector2 getLinearVelocity() {
  return body.getLinearVelocity();
}

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

public State getState(){
  //Test to Box2D for velocity on the X and Y-Axis
  //if mario is going positive in Y-Axis he is jumping... or if he just jumped and is falling remain in jump state
  if(marioIsDead)
    return State.DEAD;
  else if(runGrowAnimation)
    return State.GROWING;
  else if((b2body.getLinearVelocity().y > 0 && currentState == State.JUMPING) || (b2body.getLinearVelocity().y < 0 && previousState == State.JUMPING))
    return State.JUMPING;
  //if negative in Y-Axis mario is falling
  else if(b2body.getLinearVelocity().y < 0)
    return State.FALLING;
  //if mario is positive or negative in the X axis he is running
  else if(b2body.getLinearVelocity().x != 0)
    return State.RUNNING;
  //if none of these return then he must be standing
  else
    return State.STANDING;
}

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

private void handleImpactFeedback () {
    // process impact feedback
    while (impacts > 0) {
      impacts--;

      // feed back the result to the car simulator
      carDesc.velocity_wc.set(body.getLinearVelocity());
      carDesc.angularvelocity = -body.getAngularVelocity();
    }
  }
}

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

public void update(float dt){
  stateTime += dt;
  setRegion(fireAnimation.getKeyFrame(stateTime, true));
  setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
  if((stateTime > 3 || setToDestroy) && !destroyed) {
    world.destroyBody(b2body);
    destroyed = true;
  }
  if(b2body.getLinearVelocity().y > 2f)
    b2body.setLinearVelocity(b2body.getLinearVelocity().x, 2f);
  if((fireRight && b2body.getLinearVelocity().x < 0) || (!fireRight && b2body.getLinearVelocity().x > 0))
    setToDestroy();
}

代码示例来源: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: 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: BrentAureli/SuperMario

if((b2body.getLinearVelocity().x < 0 || !runningRight) && !region.isFlipX()){
  region.flip(true, false);
  runningRight = false;
else if((b2body.getLinearVelocity().x > 0 || runningRight) && region.isFlipX()){
  region.flip(true, false);
  runningRight = true;

代码示例来源:origin: com.badlogicgames.gdx/gdx-box2d

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: yichen0831/Bomberman_libGdx

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);
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);
case WALKING_UP:
  state.setCurrentState("walking_up");
  if (body.getLinearVelocity().y < enemy.getSpeed()) {
    body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
default:
  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

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);
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);
case WALKING_UP:
  state.setCurrentState("walking_up");
  if (body.getLinearVelocity().y < enemy.getSpeed()) {
    body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
default:
  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

Vector2 linearVelocity = body.getLinearVelocity();

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

toVector.nor();
if (body.getLinearVelocity().len2() < enemy.getSpeed() * enemy.getSpeed()) {
  body.applyLinearImpulse(toVector.scl(enemy.getSpeed()), body.getWorldCenter(), true);

相关文章