0

I am making my first game in LibGdx and it's going well. Atleast I think it is. But I want to implement a better collision system... My current collision looks like this:

       List<Rectangle> bounds= new ArrayList<Rectangle>();

       for (int i = 0; i < 250; i++) 
       {
            for (int j = 0; j < 250; j++) 
            {
                TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers().get(1);
                Cell cell3 = new Cell();

                if (cur.getCell(i, j) != null) 
                {
                    cell3 = cur.getCell(i, j);
                    System.out.println("Found a chest detector at: " + i + ", " + j
                            + ", " + cell3.getTile().getId());
                    bounds.add(new Rectangle(i * 64, j * 64, 64, 64));
                }
            }
        }


// CHECKING FOR TREES
        for (int i = 0; i < bounds.size(); i++) 
        {
            if (bounds.get(i).overlaps(player.getBounds())) 
            {
                int x = (int) bounds.get(i).x / 64;
                int y = (int) bounds.get(i).y / 64;

                TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers()
                        .get(1);
                Cell cell = cur.getCell(x, y);

                if (cell.getTile().getProperties().containsKey("Green")) 
                {
                    if (Gdx.input.isKeyPressed(Keys.SPACE)) 
                    {
                        System.out.println("You've cut down a tree!");
                    }
                }
                if (cell.getTile().getProperties().containsKey("Gray")) 
                {
                    player.reAdjust();
                }
            }
        }

As you can see when the player OVERLAPS with a tree that has a property of "Gray" a method called reAdjust is called; That method is in the player class:

First I define a String movement and set it to "";

String movement = "";

After that I add the movement code:

        if (Gdx.input.isKeyPressed(Keys.W)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.y += 2.5f;
                    movement = "up";
                    curEnergy--;

                    currentFrame = animation.getKeyFrame(12 + stateTime);
                }
            }
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.y += 2f;
            movement = "up";
            currentFrame = animation.getKeyFrame(12 + stateTime);
        }

        if (Gdx.input.isKeyPressed(Keys.A)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.x -= 2.5f;
                    movement = "left";
                    curEnergy--;
                    currentFrame = animation.getKeyFrame(4 + stateTime);
                }
            } 
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.x -= 2f;
            movement = "left";
            currentFrame = animation.getKeyFrame(4 + stateTime);
        }

        if (Gdx.input.isKeyPressed(Keys.D)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.x += 2.5f;
                    movement = "right";
                    curEnergy--;
                    currentFrame = animation.getKeyFrame(8 + stateTime);
                }
            } 
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.x += 2f;
            movement = "right";
            currentFrame = animation.getKeyFrame(8 + stateTime);
        }

        if (Gdx.input.isKeyPressed(Keys.S)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.y -= 2.5f;
                    movement = "down";
                    curEnergy--;
                    currentFrame = animation.getKeyFrame(0 + stateTime);
                }
            } 
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.y -= 2f;
            movement = "down";
            currentFrame = animation.getKeyFrame(0 + stateTime);
        }
    }

Please ignore the lines with curEnergy, that's just something I am working on... And finally, I make the reAdjust method:

public void reAdjust() 
{
    if (movement == "up") {
        position.y -= 2f;
    }
    if (movement == "down") {
        position.y += 2f;
    }
    if (movement == "right") {
        position.x -= 2f;
    }
    if (movement == "left") {
        position.x += 2f;
    }
}

Now, my question is, is there a better way for the collision? I don't really like this one... If you need any other part of the code or if I just forgot something please tell me. Thank you very much!

EDIT: Here is a picture when I moved into the wall while pressing 2 move buttons: http://i1252.photobucket.com/albums/hh576/mateo72354/bugExample_zps72892da0.png

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Grim Reaper
  • 561
  • 4
  • 6
  • 22
  • Why don't you just use Box2D, which is a part of libGDX? – Kevin Workman Jul 22 '14 at 12:42
  • Hey, I might try and learn it, so will it be better for a collision? If so, can I please get an example :) – Grim Reaper Jul 22 '14 at 12:46
  • I guess it depends on how you're defining "better". You haven't really told us what you don't like about the code you've already posted. Box2D is a full 2D physics engine. – Kevin Workman Jul 22 '14 at 12:51
  • I know what Box2d is :) But what I don't like about this is that well.. I need to get into the block and it pushes me back, BUT if I hold two moving keys at once for example W and D I can glitch into the wall – Grim Reaper Jul 22 '14 at 12:53
  • If you know what Box2D is, then you should know whether it will be better for your purposes. Anyway, the first thing I noticed is that you're using == to compare Strings. This is a VERY bad idea. Use the equals() method instead. As for your actual problem, it's going to be pretty hard to test without an [MCVE](http://stackoverflow.com/help/mcve). – Kevin Workman Jul 22 '14 at 12:56
  • Thanks, didn't really remember to use .equals() or .equalsIgnoreCase(). I will try it out, and I know what Box2d is but I just haven't used it yet :) – Grim Reaper Jul 22 '14 at 12:59
  • `movement == "up"` should be `"up".equals(movement)` and the same applies for the next 3 although technically it just should be an `enum`. – EpicPandaForce Jul 22 '14 at 13:25
  • Check out http://stackoverflow.com/questions/24651403/populating-a-boolean-array-in-java/24651741#24651741 on handling directions. – EpicPandaForce Jul 22 '14 at 13:26
  • Thanks everyone, I will try and check out the handling directions topic – Grim Reaper Jul 22 '14 at 13:30

0 Answers0