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

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

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

Body.applyLinearImpulse介绍

[英]Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body.
[中]在某一点上施加冲动。这会立即修改速度。如果应用点不在质心,它也会修改角速度。这唤醒了身体。

代码示例

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

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);
  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 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: MovingBlocks/box2d-editor

private void createBall(Vector2 orig, Vector2 force) {
  Random rand = new Random();
  float radius = rand.nextFloat() * 0.02f + 0.02f;
  BodyDef bd = new BodyDef();
  bd.type = BodyType.DynamicBody;
  bd.angularDamping = 0.5f;
  bd.linearDamping = 0.5f;
  bd.position.set(orig);
  bd.angle = rand.nextFloat() * MathUtils.PI;
  Body b = world.createBody(bd);
  b.applyLinearImpulse(force, orig, true);//TODO probably a bug
  ballsBodies.add(b);
  CircleShape shape = new CircleShape();
  shape.setRadius(radius);
  FixtureDef fd = new FixtureDef();
  fd.density = 1f;
  fd.friction = 0.5f;
  fd.restitution = 1f;
  fd.shape = shape;
  b.createFixture(fd);
  Sprite sp = new Sprite(Assets.inst().get("data/ball.png", Texture.class));
  sp.setSize(radius * 2, radius * 2);
  sp.setOrigin(sp.getWidth() / 2, sp.getHeight() / 2);
  ballsSprites.add(sp);
}

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

@Override public boolean touchUp(int x, int y, int arg2, int arg3) {
  Vector3 coordinates = new Vector3(x,y,0);
  camera.unproject(coordinates);
  if(lastTouchPolygon != null){
    Vector2 impulse = new Vector2(coordinates.x, coordinates.y).
        sub(lastTouchCoordinates).scl(lastTouchPolygon.getMass());
    Log.log("LiveMode.touchUp", "applying impulse: " + 
        impulse + " on body: " + lastTouchPolygon.getPosition());
    lastTouchPolygon.applyLinearImpulse(impulse, 
        lastTouchPolygonLocalCoordinates.add(lastTouchPolygon.getPosition()),true);
    lastTouchCoordinates = null;
    lastTouchPolygon = null;
    lastTouchPolygonLocalCoordinates = null;
  }
  return false;
}

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

相关文章