Solved LibGDX Trouble moving with acceleration and velocity vectors

I’m trying to move an actor in my world and have`` the camera follow it and keep it centered.

The problem is when I accelerate in any direction, the sprite image moves in that direction a bit as well, while it should just stay centered in the middle of the screen. The amount it moves seems to be depend on how much I set as the max Velocity.

Constructor:

    public AbstractSpaceShipActor(int posX, int posY) {
        v2Acc = new Vector2();
        v2Vel = new Vector2();

        setPosition(posX, posY);
        spaceShipActor = new SpaceShipActor("small_spaceship");

        setWidth(spaceShipActor.getSpaceShipTexture().getWidth());
        setHeight(spaceShipActor.getSpaceShipTexture().getHeight());
        setOrigin(spaceShipActor.getSpaceShipTexture().getWidth() / 2, spaceShipActor.getSpaceShipTexture().getHeight() / 2);

        addActor(spaceShipActor);

        accelerationRate = 10;
        maxSpeed = 20;   //<-- This changes how much the sprite moves in the direction its accelerating in
    }

Movement Code:

@Override
    public void act(float delta) {
        super.act(delta);

        if(isAccelerating) {
            //Calculate acceleration based on rotation. I.e. if Rotation = 0, only accelerate on +Y
            v2Acc.set((float) -Math.sin(Math.toRadians(getRotation())) * accelerationRate, (float) Math.cos(Math.toRadians(getRotation())) * accelerationRate);

            //Add Acceleration multiplied by delta to Velocity
            v2Vel.mulAdd(v2Acc, delta);

            //Limit Speed on X-Axis
            if (v2Vel.x > maxSpeed) {
                v2Vel.x = maxSpeed;
            } else if (v2Vel.x < -maxSpeed) {
                v2Vel.x = -maxSpeed;
            }

            //Limit Speed on Y-Axis
            if (v2Vel.y > maxSpeed) {
                v2Vel.y = maxSpeed;
            } else if (v2Vel.y < -maxSpeed) {
                v2Vel.y = -maxSpeed;
            }
        }else if(isStopping){
            //If velX is really small, just stop movement completely
            if(v2Vel.x < 0.15 && v2Vel.x > -0.15){
                v2Vel.x = 0;
            }else if(v2Vel.x > 0){
                v2Vel.x = v2Vel.x + (-accelerationRate * delta);
            }else if(v2Vel.x < 0){
                v2Vel.x = v2Vel.x + (accelerationRate * delta);
            }

            //If velY is really small, just stop movement completely
            if(v2Vel.y < 0.15 && v2Vel.y > -0.15){
                v2Vel.y = 0;
            }else if(v2Vel.y > 0){
                v2Vel.y = v2Vel.y + (-accelerationRate * delta);
            }else if(v2Vel.y < 0){
                v2Vel.y = v2Vel.y + (accelerationRate * delta);
            }
        }else{
            v2Acc.set(0, 0);
        }

        //Set Position of Group
        setX(getX() + v2Vel.x);
        setY(getY() + v2Vel.y);
    }

I’m not that familiar with LibGDX, so if this response is completely off-base and irrelevant, my apologies. That said, I’ll just mention that the behavior you describe is a typical result of updating the camera before updating the target actor rather than after, so you might double-check and make sure that the camera update code executes after the code you posted, and not before.

This has solved my problem, thank you! :slight_smile: