I have been working on this game NinjaThief and when I apply linear impulse to player body it just keeps on moving. Some of the functions are still unfinished so it looks a bit messy.

Here is my code:

NinjaThief class(main game class)

public class NinjaThief extends Game {

  public static final int V_WIDTH = 400;
  public static final int V_HEIGHT = 800;
  public SpriteBatch batch;


  @Override
  public void create () {
    batch = new SpriteBatch();

    setScreen(new PlayScreen(this));
  }

  @Override
  public void render () {
    super.render();
  }


  @Override
  public void dispose () {
    batch.dispose();
  }
}

PlayScreen:

public class PlayScreen implements Screen {

 public static int PPM = 100;
 Player p;
 World world;
 TiledMap map;
 OrthographicCamera gamecam;
 TiledMapRenderer maprenderer;
 NinjaThief game;
 Box2DDebugRenderer b2dr;
 public PlayScreen(NinjaThief game) {
  varinit();
  this.game = game;
 }

 @Override
 public void show() {}

 public void varinit() {
  float width = Gdx.graphics.getWidth();
  float height = Gdx.graphics.getHeight();
  gamecam = new OrthographicCamera();
  gamecam.setToOrtho(false, width, height);

  gamecam.update();
  map = new TmxMapLoader().load("mapDebug.tmx");
  maprenderer = new OrthogonalTiledMapRenderer(map);

  world = new World(new Vector2(0, 0), true);
  b2dr = new Box2DDebugRenderer();
  p = new Player();
  p.init(world);
 }

 public void viewsetup() {
  maprenderer.setView(gamecam);
 }


 @Override
 public void render(float delta) {

  update();

  ClearScreen.clear();
  gamecam.update();
  viewsetup();
  maprenderer.render();
  game.batch.begin();
  p.render(game.batch);
  game.batch.end();
  world.step(1 / 60 f, 6, 2);
  b2dr.render(world, gamecam.combined);
 }

 public void update() {
  p.update();
 }

 @Override
 public void resize(int width, int height) {
 }

 @Override
 public void pause() {
 }

 @Override
 public void resume() {
 }

 @Override
 public void hide() {
 }

 @Override
 public void dispose() {
  map.dispose();
  p.dispose();
 }
}

Player:

public class Player {

 boolean isKeyPressed = false;

 Texture texture;
 Sprite sprite;
 BodyDef bdef;

 int MaxVel;
 Vector2 vel;
 Vector2 pos;

 PolygonShape PlayerB;

 public static Body PlayerBody;
 Vector2 beginPos;
 Vector2 currentPos;

 @SuppressWarnings("static-access")
 public void init(World world) {
  MaxVel = 10;

  texture = new Texture("Player.png");
  sprite = new Sprite(texture);

  bdef = new BodyDef();
  bdef.type = bdef.type.DynamicBody;
  bdef.position.set(new Vector2(sprite.getX(), sprite.getY()));

  bdef.position.x = 30;
  bdef.position.y = 40;

  PlayerBody = world.createBody(bdef);

  Vector2[] vertices = new Vector2[4];

  vertices[0] = new Vector2(sprite.getX(), sprite.getY());
  vertices[1] = new Vector2(sprite.getX() + sprite.getWidth(), sprite.getY());
  vertices[2] = new Vector2(sprite.getX(), sprite.getY() + sprite.getHeight());
  vertices[3] = new Vector2(sprite.getX() + sprite.getWidth(), sprite.getY() + sprite.getHeight());

  sprite.setPosition(0, 0);

  FixtureDef fixDef = new FixtureDef();
  PlayerB = new PolygonShape();
  PlayerB.set(vertices);
  fixDef.shape = PlayerB;
  Fixture fix = PlayerBody.createFixture(fixDef);

  vel = PlayerBody.getLinearVelocity();
  pos = PlayerBody.getPosition();
 }

 public void setTexture(String dir) {
  texture = new Texture(dir);
 }

 public void render(Batch batch) {
  sprite.draw(batch);
 }


 public void update() {
  if (Gdx.input.isKeyJustPressed(Keys.W) && isKeyPressed == false) {
   isKeyPressed = true;
   PlayerBody.applyLinearImpulse(new Vector2(0, 1), PlayerBody.getPosition(), true);
  }

  handleBdefPos();
  handleGroundCollision();
  handleTextureMovement();
  handlePlayerB();
 }

 public void handleTextureMovement() {
  sprite.setPosition(bdef.position.x, bdef.position.y);
 }

 public void handleBdefPos() {
  bdef.position.x = PlayerBody.getPosition().x;
  bdef.position.y = PlayerBody.getPosition().y;
 }

 public void handleInput() {
  beginPos = PlayerBody.getPosition();
  currentPos = PlayerBody.getPosition();
  if (Gdx.input.isKeyJustPressed(Keys.W)) {
   while ((beginPos.y - currentPos.y) < 300) {
    currentPos = PlayerBody.getPosition();
    PlayerBody.setLinearVelocity(new Vector2(0, 4));
   }
   PlayerBody.setLinearVelocity(new Vector2(0, 0));
  }
 }

 public void handleGroundCollision() {
 }

 public void handlePlayerB() {
 }

 public void dispose() {
  texture.dispose();
 }
}
  • It's not immediately obvious from reading your question where the problem is. Can you include a description (or even better, an image) of what your scene looks like, and tell us what you're trying to do with the impulse, what you expect should happen, and what's happening instead? If users have to read all your code to figure out the premise of your question, they're less likely to take the time to offer an answer. – DMGregory Nov 14 '16 at 23:11
  • You have no gravity. It's the vector in the World constructor – Niall Quinlan Jan 22 at 14:03

What friction does the player have? Is it sliding against another body that also has friction?

If not, applying a impulse will make it move until you apply a force in the opposite direction.

Set the density of the fixture:

fixDef.density = 1;

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.