Moving Bodies

Now we will create a Player body to demonstrate how to move bodies by adding velocity to them.

First create a new package called ‘Sprites’ to store all your different Sprites objects, which includes all your NPCs and items.

Create a new package
Call it ‘Sprites’

Then create a new class called ‘Player’ in the ‘Sprites’ package and have it extend the Sprite class.

Make a new Java class and call it ‘Player’
Have it extend the Sprite class

The Player should have a World object within which it exists and a Body object to define its hitbox and other properties mentioned in the previous lesson.

The constructor should take in a World object and set the World object of the player like so.

Then create a method to define the Body and Fixture of the Player sprite. For now, I will set the position of the sprite to (50, 50), you can choose a different coordinate position. The Player BodyType will also be different, now it will be a DynamicBody because we later want to move our Player sprite by giving the Player body velocity through arrow key inputs. The size of the polygon can be your decision, but the numbers you input are half of the width and height. If you want your fixture to be a 16×16 pixel tile, then you have to put the parameters of ‘.setAsBox()’ as (8, 8) instead of (16, 16).

Method for making the Body and Fixture of a Player

After creating this method, call the method in your Player constructor.

Call the Method

Now that you have created your Player class, it’s time to create the Player in your game’s Screen. Create the Player variable and initialize it in the game’s Screen constructor. You should also set the gravity of your world to -10 in the ‘y’ direction if you want your world to have gravity (which is for platformers). Otherwise, you can leave it at (0, 0).

Make a Player variable
Initialize the player and set the World gravity to -10 in the y direction

Now that you have created your Player, you should be able to see the Player’s fixture outline on your game’s screen.

First you might want to apply gravity to this world. Some of you might not! In the screen’s constructor we can set the gravity to -10 for the world.

world = new World( new Vector2(0,-10), true);

This will help us create an effect of jumping if you have a platformer.

Now on to making the player move. We need to apply some force. We will have to change handleInput() to apply force to the player. First create a method that returns the player’s body in the Player class:

public Body getBox2Body()
    {
        return box2Body;
    }

Next you will want to update handleInput() in the Screen class:

    private void handleInput(float dt)
    {
        //if right button pressed and player's velocity is less than (a number)
        if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT) && player.box2Body.getLinearVelocity().x <= 10f)
        {
            Vector2 force = new Vector2(10f, 0); //1. create force
            player.getBox2Body().applyLinearImpulse(force, player.getBox2Body().getWorldCenter(), true); //apply force
        }

The reason you want to check if a player’s velocity is less than a specific number, is because after the button is pressed, a force will be continually applied to the player and cause it to accelerate. In this case, it will never stop moving to the right. To fix this issue, we simply check if the velocity is higher than a specific velocity so we know when to stop applying force to the player.

Finally, add this to update() so that the camera will now follow the player:

camera.position.x = player.getBox2Body().getPosition().x;

Video Lesson