Constant movement of an actor in libgdx


I am developing a game in libgdx .

The game consists of an actor, which the camera constantly follows with that actor centered.

My problem is that the movement of the actor is not resolved, because I would like it to behave like , the snake always moves and you only add / change the direction.

At the moment I do not move the actor because I wanted to find out how to make the movement that I asked. Anyway, the movement you would use is a simple movement:

public static final float SPEED = 100;
public void render(float delta){
   actor.setPosition(actor.getX() + (SPEED * delta * dirX), actor.getY() + (SPEED * delta * dirY));

dirX and dirY are the coordinates of the joystic to get the address.

It is a 2D game with overhead view. I would like it to always move forward. Forward I mean if the actor turns, keep going forward, keeping the direction the actor has taken. I get this address with the joystick.

translate does not work for me, but I do not see the option. The same is because I do not use sprites, I use textures and actors. Also, I'm using Scene2D and box2D, which I had not mentioned before.

I need to create a continuous actor movement, that without the user putting any buttons, moves, but when the user moves the joystick that direction is applied to the actor, the actor can move freely around the screen

asked by OlivaDevelop 27.02.2017 в 10:34

2 answers


After many laps I have found the solution. It is not exact if the joystick is used but meets the requirements. Thank you all for your help.

The movement code, inside the render () method

private void move(float delta) {
    if ((dirX == (float) 0.000015258789 && dirY == (float) -0.007827878)) {
        dirX = 0.0f;
        dirY = 0.0f;
    if (!(dirX == 0.0 && dirY == 0.0)) {
        lastDirX = dirX;
        lastDirY = dirY;
        assigned = false;
    } else {
        if (!assigned) {
            dirX = lastDirX;
            dirY = lastDirY;
            assigned = true;
            (getX() * METERS_IN_PIXELS) + (METERS_IN_PIXELS * dirX * (PLAYER_SPEED + speedExtra)),
            (getY() * METERS_IN_PIXELS) + (METERS_IN_PIXELS * dirY * (PLAYER_SPEED + speedExtra)),

    getScreen().getStage().getCamera().position.x = (getX() + (getWidth() / 2))
            + (METERS_IN_PIXELS * dirX * (PLAYER_SPEED + speedExtra));
    getScreen().getStage().getCamera().position.y = (getY() - (getHeight() * METERS_IN_PIXELS) / 2)
            + (METERS_IN_PIXELS * dirY * (PLAYER_SPEED + speedExtra));
answered by 01.03.2017 / 01:54

A solution could be to save the penultimate position in addition to the last, these two data will give us the address (negative or positive) in which it is moving.

if(penultimoX != null && penultimoX != null){
actor.setPosition(actor.getX()+(SPEED*delta*dirX), actor.getY()+(SPEED delta*dirY));

can you comment, the listener that receives the data that the user presses?

answered by 27.02.2017 в 22:03