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

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

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

Body.setLinearVelocity介绍

[英]Set the linear velocity of the center of mass.
[中]设置质心的线速度。

代码示例

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

public void update (float deltaTime) {
    dist += dir.len() * deltaTime;
    if (dist > maxDist) {
      dir.scl(-1);
      dist = 0;
    }
    platform.setLinearVelocity(dir);
  }
}

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

private void launch () {
  m_body.setTransform(new Vector2(0, 20), 0);
  m_angularVelocity = (float)Math.random() * 100 - 50;
  m_body.setLinearVelocity(new Vector2(0, -100));
  m_body.setAngularVelocity(m_angularVelocity);
}

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

@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
public boolean keyDown (int keyCode) {
  if (keyCode == Input.Keys.COMMA) {
    if (m_bullet != null) {
      world.destroyBody(m_bullet);
      m_bullet = null;
    }
    {
      CircleShape shape = new CircleShape();
      shape.setRadius(0.25f);
      FixtureDef fd = new FixtureDef();
      fd.shape = shape;
      fd.density = 20.0f;
      fd.restitution = 0.05f;
      BodyDef bd = new BodyDef();
      bd.type = BodyType.DynamicBody;
      bd.bullet = true;
      bd.position.set(-31, 5);
      m_bullet = world.createBody(bd);
      m_bullet.createFixture(fd);
      m_bullet.setLinearVelocity(new Vector2(400, 0));
    }
  }
  return false;
}

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

player.setLinearVelocity(vel.x, vel.y);
  player.setLinearVelocity(vel.x * 0.9f, vel.y);
} else {
  stillTime = 0;
  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);

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

@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();
  }
}

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

public void resetPhysics () {
  body.setAngularVelocity(0);
  body.setLinearVelocity(0, 0);
  impacts = 0;
}

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

@Override
public void act(float delta) {
  super.act(delta);
  body.setLinearVelocity(getUserData().getLinearVelocity());
}

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

/**
 * Launches a new ball. The position and velocity of the ball are controlled by the parameters
 * in the field layout JSON.
 */
public Ball launchBall() {
  List<Float> position = layout.getLaunchPosition();
  List<Float> velocity = layout.getLaunchVelocity();
  float radius = layout.getBallRadius();
  Ball ball = Ball.create(world, position.get(0), position.get(1), radius,
      layout.getBallColor(), layout.getSecondaryBallColor());
  ball.getBody().setLinearVelocity(new Vector2(velocity.get(0), velocity.get(1)));
  this.balls.add(ball);
  audioPlayer.playBall();
  return ball;
}

代码示例来源: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: manuelbua/uracer-kotd

@Override
public void onBeforePhysicsSubstep () {
  super.onBeforePhysicsSubstep();
  // let's subclasses behave as needed, ask them to fill carForces with new data
  onComputeCarForces(carForces);
  // trigger event, new forces have been computed
  if (triggerEvents) {
    GameEvents.playerCar.data.setForces(carForces);
    GameEvents.playerCar.trigger(this, CarEvent.Type.onPhysicsForcesReady);
  }
  // put newly computed forces into the system
  body.setLinearVelocity(carForces.velocity_x, carForces.velocity_y);
  body.setAngularVelocity(-carForces.angularVelocity);
}

代码示例来源:origin: MovingBlocks/box2d-editor

private void restart() {
  bottleModel.setTransform(0, 3, 0.2f);
  bottleModel.setLinearVelocity(0, 0);
  bottleModel.setAngularVelocity(0);
    ballModels[i].setLinearVelocity(vec.set(0, 0));
    ballModels[i].setAngularVelocity(0);
    ballModels[i].setTransform(vec.set(tx, ty), angle);

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

public void update(float dt){
  stateTime += dt;
  if(setToDestroy && !destroyed){
    world.destroyBody(b2body);
    destroyed = true;
    setRegion(new TextureRegion(screen.getAtlas().findRegion("goomba"), 32, 0, 16, 16));
    stateTime = 0;
  }
  else if(!destroyed) {
    b2body.setLinearVelocity(velocity);
    setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
    setRegion(walkAnimation.getKeyFrame(stateTime, true));
  }
}

代码示例来源: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 defineFireBall(){
  BodyDef bdef = new BodyDef();
  bdef.position.set(fireRight ? getX() + 12 /MarioBros.PPM : getX() - 12 /MarioBros.PPM, getY());
  bdef.type = BodyDef.BodyType.DynamicBody;
  if(!world.isLocked())
  b2body = world.createBody(bdef);
  FixtureDef fdef = new FixtureDef();
  CircleShape shape = new CircleShape();
  shape.setRadius(3 / MarioBros.PPM);
  fdef.filter.categoryBits = MarioBros.FIREBALL_BIT;
  fdef.filter.maskBits = MarioBros.GROUND_BIT |
      MarioBros.COIN_BIT |
      MarioBros.BRICK_BIT |
      MarioBros.ENEMY_BIT |
      MarioBros.OBJECT_BIT;
  fdef.shape = shape;
  fdef.restitution = 1;
  fdef.friction = 0;
  b2body.createFixture(fdef).setUserData(this);
  b2body.setLinearVelocity(new Vector2(fireRight ? 2 : -2, 2.5f));
}

代码示例来源:origin: MovingBlocks/DestinationSol

@Test
public void getSpeed() {
  Body body = BodyUtilities.createDummyBody();
  body.setLinearVelocity(1f, 2f);
  final Shard shard = new Shard(body, drawables);
  assertTrue(shard.getVelocity().epsilonEquals(1f, 2f, 0.01f));
  assertTrue(SHARD_CONSTANT.getVelocity().epsilonEquals(0f, 0f, 0.01f));
}

代码示例来源:origin: oakes/libgdx-examples

ballBody.setLinearVelocity(10, 10);

相关文章