Search code examples
memory-leakslibgdxcollision-detectionaide

Progressive lag with each collision iteration libGDX


Hello I am developing a game on my spare time with AIDE and libGDX. Though AIDE has some missing methods of the libGDX API and I had to create some workarounds to compensate for the missing methods. So my problem is that with every instance of a collision the app becomes more and more laggy. No new textures are being drawn and non go away. Just a poor implementation that is meant to push you out of said collision tile. It runs fine until you have a few collisions. My thought is that it creates a new variable with every collision and stores it into memory causing a leak. But I can't really tell if that is the case. Oh I'd like to add that I don't have access to a computer, just my phone. Here is my movement class

move.java

public class move
{
    float posX;
    float posY;
    float touchedOnScreenX;
    float touchedOnScreenY;
    float centerOfScreenX;
    float centerOfScreenY;
    float posXSetter;
    float posYSetter;
    float Speed = 150;
    float mapBoundsWidth;
    float mapBoundsHeight;
    boolean upperBounds;
    boolean lowerBounds;
    boolean rightBounds;
    boolean leftBounds;
    boolean tileCollition;



    public move() {

    }

    public void renderMove(float delta) {


        if(Gdx.input.isTouched()) {
            screenAndTouchInfo();
            checkBoundsBoolean();
            checkForCollision();
        }
    }

    //slows game down
    private void checkForCollision()
    {
        if (upperBounds == false)
        {
            if (lowerBounds == false)
            {
                if (rightBounds == false)
                {
                    if (leftBounds == false)
                    {
                        if (tileCollition == false)
                        {
                            movement();
                        }
                        else
                        {
                            collitionSide();
                        }
                    }
                    else
                    {
                        posX++;
                    }
                }
                else
                {
                    posX--;
                }
            }   
            else
            {
                posY++;
            }
        }
        else
        {
            posY --;
        }
    }

    private void movement()
    {
        posYSetter = posY;
        posXSetter = posX;
        if (touchedOnScreenX < centerOfScreenX)
        {
            posX -= Gdx.graphics.getDeltaTime() * Speed;

        }
        else
        {
            posX += Gdx.graphics.getDeltaTime() * Speed;

        }
        if (touchedOnScreenY < centerOfScreenY)
        {
            posY -= Gdx.graphics.getDeltaTime() * Speed;

        }
        else
        {
            posY += Gdx.graphics.getDeltaTime() * Speed;

        }
        if (touchedOnScreenY < centerOfScreenY + 64 && touchedOnScreenY > centerOfScreenY - 64)
        {
            posY = posYSetter;

        }
        if (touchedOnScreenX < centerOfScreenX + 64 && touchedOnScreenX > centerOfScreenX - 64)
        {
            posX = posXSetter;

        }
    }

    //buggy and slows game down. Can push you into tile if input is the opposite of the side
    public void collitionSide() {
        if (tileCollition == true){
            if (touchedOnScreenX < centerOfScreenX)
            {
                posX = posX +10;
            }
            else
            {
                posX = posX - 10;

            }
            if (touchedOnScreenY < centerOfScreenY)
            {
                posY = posY +10;
            }
            else
            {
                posY = posY -10;
            }
        }
    }

    private void screenAndTouchInfo()
    {
        touchedOnScreenX = Gdx.input.getX();
        touchedOnScreenY = (Gdx.graphics.getHeight() - Gdx.input.getY());
        centerOfScreenX = Gdx.graphics.getWidth() / 2;
        centerOfScreenY = Gdx.graphics.getHeight() / 2;
    }
    //slows game down
    private void checkBoundsBoolean()
    {
        if (posX > mapBoundsWidth)
        {
            rightBounds = true;
        }
        else {
            rightBounds = false;
        }
        if (posY > mapBoundsHeight)
        {
            upperBounds = true;
        }
        else {
            upperBounds = false;
        }
        if (posX < mapBoundsWidth - mapBoundsWidth)
        {
            leftBounds = true;
        }
        else {
            leftBounds = false;
        }
        if (posY < mapBoundsHeight - mapBoundsHeight)
        {
            lowerBounds = true;
        }
        else {
            lowerBounds = false;
        }
    }

    public void setTileCollision(boolean tileCollision) {
        this.tileCollition = tileCollision;
    }

    public float getPosX() {
        return posX;
    }

    public float getPosY() {
        return posY;
    }

    public float getTouchedOnScreenX() {
        return touchedOnScreenX;
    }

    public float getTouchedOnScreenY() {
        return touchedOnScreenY;
    }

    public float getCenterOfScreenX() {
        return centerOfScreenX;
    }

    public float getCenterOfScreenY() {
        return centerOfScreenY;
    }

    public void setMapboundsWidth(float width) {
        this.mapBoundsWidth = width;
    }

    public void setMapboundsHeight(float height) {
        this.mapBoundsHeight = height;
    }
}

I know that is a lot of code to comb through and I am sorry if it isn't always clear what is going on. I refactored it to where it would be a little easier to understand. Oh if anyone could tell me why AIDE is missing some methods of libGDX that would be nice too.The most notable one would be Cell.setTile(). That I know is in libGDX's API and can be found in their documentation. But when I implement it it throws a unknown method of class Cell error. I have researched on how to use the method as well with no avail. Had to create int[][] map and a for loop to draw map with another Texture[]. It works. Lol. Thank you to whomever even took the time to look at my long winded double question. Hopefully someone can tell me why this lag is created from said collisions.


Solution

  • I recommend moving your collision code into a separate thread. This should improve performance significantly:

    Executor executor = Executors.newSingleThreadExecutor();
      executor.execute(new Runnable() {
         @Override
         public void run() {
            // Physics loop goes here
         }
    });
    

    Make sure to shutdown the executor when disposing your screen.