Gravity and collision problem

Hello! I recently began a school project where we are going to make a game by only using Java/JavaFX. Since i am kinda new to java i’ve stumbled across some issues with my gravity and collision methods, mainly collision i think since it’s far from flawless.
The problem i am having is when i implement some gravity to make the sprite fall, i set the falling to false on collision and set it to true again when in the air. Since the collision is pushing the sprite back the sprite thinks it 's in the air again so it just bounces on the tile.

I have asked the TA at my school and he suggested adding some padding, but i ain’t sure how to do that either and i am sure it’s a more efficient way to do it.

Here’s my collision code and the falling is just changing the y value and update it on ticks.
The collision is prob the main issue, but i don’t how to fix it.


for (int i = 0; i < hero.getMaps().getTileList().size(); i++) {
            Tile tile = hero.getMaps().getTileList().get(i);
    if (collision(tile)) {
                iY -= vY;
                falling = false;
                canJump = true;

            }
            else {
                falling = true;
            }

            if (hero.isDown() && collision(tile)) {
                iY -= vY;

            }

           else if (hero.isRight() && collision(tile)) {
                iX -= vX;

            }
           else if (hero.isLeft() && collision(tile)) {
                iX += vX;

            }
            if (hero.isUp() && collision(tile)) {

                iY +=  vY;


            }
        }
}

        public boolean collision(Tile tile) {
        boolean collisionDetect = false;

                if (hero.getiNinja().getSpriteFrame()
                        .getBoundsInParent().intersects(tile.getBoundsInLocal())) {
                        return true;
                    }
            return false;
    }


I appreciate all help!

Just build an extended hitbox from the one the sprite has that toggles gravity.
(that’s the padding your teacher told you about)

Bounds is a class, just grab the data from your sprites Bounds and make a new Bounds object with the bottom extended.

Bounds oldBounds = hero.getiNinja().getSpriteFrame().getBoundsInParent();
double minX = oldBounds.getMinX();
[...]
Bounds extendedBounds = new Bounds(minX, minY, mixZ, width, height + TILEWIDTH*0.25, depth);
checkPaddedBoundsToggleGravityOrWhatever(extendedBounds);

See this: https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Bounds.html

Just increase the height value a bit to extend the Bounds downwards, maybe a quarter tile width or so.

This isn’t perfect though, if your sprite get’s stuck in a ceiling it’ll disable gravity too, you could try different Bounds sizes and shapes.

Where is var “falling” actually used? The “bouncing” should only become apparent if you’re drawing the sprite inbetween your “falling” code and collision code. Wherever your “falling” code is, I’d move it to the start of your loop, so that when the collision moves the player back up, they are back to the same place as before the fall, meaning there is no bouncing effect.

Thanks VaTTeRGeR, it fixed my first problem so that falling and jumping works.

second problem is the sprite is still bouncing. Steve i use fall at the top of my update(gameloop) method and falling is just a boolean value.

Fall method is just thi.

if (falling) {
            iY += gravity;
        }

If you move the falling code to immediately prior to the collision checking code, then theoretically the sprite should move back to where it was previously, so there should be no bouncing. I don’t know your values, but maybe the line:-


if (collision(tile)) {
                iY -= vY;

needs changing to


if (collision(tile)) {
                iY -= (vY + gravity);

to take into account the movement from gravity? I’ve never needed to create a separate collision box for gravity.

Edit: Looking at your code, I’d change the way it works to something like:-

  1. Read player’s inputs
  2. Move player’s sprite
  3. Check for any collisions
  4. If player is colliding with a tile (or anything else relevant), move them back to where they were at the start of the game loop.

At the moment, you’re checking for a tile collision 5 times, but you only need to do this once.

Changing the code to

if (collision(tile)) {
                iY -= (vY + gravity);

just made the bouncing even worse.

Heres my code for the sprite

[spoiler]

 
@Override
    public void update() { // gameplayloop update
        fall();
        setXYLoc();
        checkCollision();
        setBoundaries();
        setImageState();



    }

   
    private void setXYLoc() {
        if (hero.isRight()) {
            iX += vX;

        }
        if (hero.isLeft()) {
            iX -= vX;
        }
        if (hero.isDown()) {
            iY += 2 * vY;
        }
        if (hero.isUp()) {

            if (canJump) {
                jump(100);
                canJump = false;

            }


        }
        spriteFrame.setTranslateX(iX);
        spriteFrame.setTranslateY(iY);
    }

    
    private void checkCollision() {

        for (int i = 0; i < hero.getMaps().getTileList().size(); i++) {
            Tile tile = hero.getMaps().getTileList().get(i);


                if (collision()) {
                    hero.getGamePlayLoop().stop();
                    hero.getGameOver().setVisible(true);
                }


            if ((hero.isDown() || hero.isUp()) && collision()) {
                hero.getGameScreen().getChildren().remove(hero.getiEnemy().getSpriteFrame());

            }

           if (hero.getiNinja().newBounds().intersects(tile.getBoundsInParent())) {
               iY -= (vY);
               canJump = true;

            }

            if (collision(tile)) {
                if (hero.isDown()) {
                    iY -= vY;

                }

                else if (hero.isRight()) {
                    iX -= vX;

                }
                else if (hero.isLeft() ) {
                    iX += vX;

                }
                if (hero.isUp()) {

                    iY += 3 * vY;


                }
            }





        }
}

    private Bounds newBounds() {
    Bounds oldBounds = hero.getiNinja().getSpriteFrame().getBoundsInParent();
    double minX = oldBounds.getMinX();
    double minY = oldBounds.getMinY();
    double mixZ = oldBounds.getMinZ();
    double width = oldBounds.getWidth();
    double height = oldBounds.getHeight();
    double depth = oldBounds.getDepth();

    double TILEWIDTH = 32;
    Bounds extendedBounds = new BoundingBox(minX, minY, mixZ, width, height + TILEWIDTH*0.05, depth);
    return  extendedBounds;
}

        private boolean collision(Tile tile) {

            return hero.getiNinja().getSpriteFrame()
                    .getBoundsInParent().intersects(tile.getBoundsInLocal());
        }

    private boolean collision() {


        return hero.getiNinja().getSpriteFrame()
                .getBoundsInParent().intersects(hero.getiEnemy().getSpriteFrame().getBoundsInParent());
    }

[/spoiler]