Java Code Examples for com.badlogic.gdx.physics.box2d.Body#setLinearVelocity()
The following examples show how to use
com.badlogic.gdx.physics.box2d.Body#setLinearVelocity() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnimatePositionSystem.java From Entitas-Java with MIT License | 6 votes |
public void moveFocused(Vector2 center, Body body) { MoveData moveData = new MoveData(); body.setLinearVelocity(new Vector2()); moveData.start = body.getWorldCenter().cpy(); moveData.direction = new Vector2(center.x - moveData.start.x, center.y - moveData.start.y); moveData.current = 1; moveData.total = 10; if (moveData.current == moveData.total + 1) { body.setLinearVelocity(new Vector2()); return; } int t = easeInOut(moveData.current, 0, 1, moveData.total); Vector2 sEnd = moveData.direction.cpy(); sEnd.scl(t); sEnd.add(moveData.start.cpy()); Vector2 sStart = body.getWorldCenter(); sEnd.sub(sStart); sEnd.scl(60); moveData.current++; body.setLinearVelocity(sEnd); }
Example 2
Source File: CarrotPhysicsProcessor.java From ninja-rabbit with GNU General Public License v2.0 | 6 votes |
@Override public void update(final Entity character) { Body body = character.getBody(); if (((Collectible) character).isCollected()) { body.getWorld().destroyBody(body); } else { Vector2 position = character.getBody().getPosition(); if (!character.isInState(CarrotState.UP)) { origin = position.y; body.setLinearVelocity(0.0f, VERTICAL_VELOCITY); character.changeState(CarrotState.UP); } else if (position.y - origin > MAX_DISTANCE) { body.setLinearVelocity(0.0f, -VERTICAL_VELOCITY); } else if (position.y <= origin) { character.changeState(CarrotState.DOWN); } } }
Example 3
Source File: PlayerSystem.java From Pacman_libGdx with MIT License | 5 votes |
@Override protected void processEntity(Entity entity, float deltaTime) { PlayerComponent player = playerM.get(entity); StateComponent state = stateM.get(entity); MovementComponent movement = movementM.get(entity); Body body = movement.body; if (player.hp > 0) { if ((Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.RIGHT)) && checkMovable(body, MoveDir.RIGHT)) { body.applyLinearImpulse(tmpV1.set(movement.speed, 0).scl(body.getMass()), body.getWorldCenter(), true); } else if ((Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.LEFT)) && checkMovable(body, MoveDir.LEFT)) { body.applyLinearImpulse(tmpV1.set(-movement.speed, 0).scl(body.getMass()), body.getWorldCenter(), true); } else if ((Gdx.input.isKeyPressed(Input.Keys.W) || Gdx.input.isKeyPressed(Input.Keys.UP)) && checkMovable(body, MoveDir.UP)) { body.applyLinearImpulse(tmpV1.set(0, movement.speed).scl(body.getMass()), body.getWorldCenter(), true); } else if ((Gdx.input.isKeyPressed(Input.Keys.S) || Gdx.input.isKeyPressed(Input.Keys.DOWN))&& checkMovable(body, MoveDir.DOWN)) { body.applyLinearImpulse(tmpV1.set(0, -movement.speed).scl(body.getMass()), body.getWorldCenter(), true); } if (body.getLinearVelocity().len2() > movement.speed * movement.speed) { body.setLinearVelocity(body.getLinearVelocity().scl(movement.speed / body.getLinearVelocity().len())); } } // player is invincible for 3 seconds when spawning if (GameManager.instance.playerIsInvincible) { player.invincibleTimer += deltaTime; if (player.invincibleTimer >= 3.0f) { GameManager.instance.playerIsInvincible = false; player.invincibleTimer = 0; } } player.playerAgent.update(deltaTime); state.setState(player.currentState); }
Example 4
Source File: Box2DSynchronizerPre.java From riiablo with Apache License 2.0 | 5 votes |
@Override protected void process(int entityId) { Body body = mBox2DBody.get(entityId).body; Vector2 velocity = mVelocity.get(entityId).velocity; body.setLinearVelocity(velocity); // FIXME: This is a temp fix to prevent shoving NPCs -- need to explore Contact Filters if (velocity.isZero()) { body.setType(BodyDef.BodyType.StaticBody); } else { body.setType(BodyDef.BodyType.DynamicBody); } }
Example 5
Source File: NinjaRabbitBodyProcessor.java From ninja-rabbit with GNU General Public License v2.0 | 5 votes |
/** * Sets a new body onto the {@link NinjaRabbit} instance, after destroying the current one. Sets * the current direction to the given {@link Direction}. * * @param character * The {@link NinjaRabbit} being updated. * @param direction * The direction the {@link NinjaRabbit} is facing. */ private void changeDirection(final Entity character) { Vector2 position = character.getBody().getPosition(); Vector2 velocity = character.getBody().getLinearVelocity(); float angle = character.getBody().getAngle(); world.destroyBody(character.getBody()); Body newBody = bodyFactory.create(world, character.getDirection()); newBody.setTransform(position, angle); newBody.setLinearVelocity(velocity); character.setBody(newBody); newBody.setUserData(character); lastKnownDirection = character.getDirection(); }
Example 6
Source File: ComponentMovement.java From RuinsOfRevenge with MIT License | 5 votes |
@Override public void apply(Entity entity) { Body body = entity.getBody(); moving = false; // If trying to move (pressing buttons on Keyboard, steering with Gamepad) if (xsteer != 0f || ysteer != 0f) { moving = true; if (Math.abs(xsteer) > Math.abs(ysteer)) { if (xsteer < 0) { direction = Dir.LEFT; } else { direction = Dir.RIGHT; } } else { if (ysteer < 0) { direction = Dir.DOWN; } else { direction = Dir.UP; } } } Vector2 linVel = body.getLinearVelocity(); if (linVel.len() > maxspeed) { body.setLinearVelocity(linVel.cpy().nor().scl(maxspeed)); } body.applyForceToCenter(strength * xsteer, strength * ysteer); if (friction > 1f && !moving) { body.setLinearVelocity(linVel.div(friction)); } }
Example 7
Source File: BombSystem.java From Bomberman_libGdx with MIT License | 4 votes |
@Override protected void process(int entityId) { Bomb bomb = mBomb.get(entityId); State state = mState.get(entityId); RigidBody rigidBody = mRigidBody.get(entityId); Body body = rigidBody.body; bomb.countDown -= world.getDelta(); if (bomb.countDown <= 0) { // explode bomb.state = Bomb.State.EXPLODING; } switch (bomb.state) { case EXPLODING: state.setCurrentState("exploding"); GameManager.getInstance().playSound("Explosion.ogg", 1.0f, MathUtils.random(0.6f, 0.8f), 0); // create explosion ActorBuilder actorBuilder = ActorBuilder.init(body.getWorld(), world); actorBuilder.createExplosion(body.getPosition().x, body.getPosition().y, bomb.power); // destroy itself World b2dWorld = body.getWorld(); b2dWorld.destroyBody(body); world.delete(entityId); break; case MOVING_UP: if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y + 0.55f))) { body.setLinearVelocity(0, bomb.speed); } else { body.setLinearVelocity(0, 0); body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0); bomb.state = Bomb.State.NORMAL; } break; case MOVING_DOWN: if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y - 0.55f))) { body.setLinearVelocity(0, -bomb.speed); } else { body.setLinearVelocity(0, 0); body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0); bomb.state = Bomb.State.NORMAL; } break; case MOVING_LEFT: if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x - 0.55f, body.getPosition().y))) { body.setLinearVelocity(-bomb.speed, 0); } else { body.setLinearVelocity(0, 0); body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0); bomb.state = Bomb.State.NORMAL; } break; case MOVING_RIGHT: if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x + 0.55f, body.getPosition().y))) { body.setLinearVelocity(bomb.speed, 0); } else { body.setLinearVelocity(0, 0); body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0); bomb.state = Bomb.State.NORMAL; } break; case NORMAL: default: state.setCurrentState("normal"); break; } }