Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Gravity and collision problem  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
Offline Lostmc

Junior Newbie


Exp: 1-3 months



« Posted 2018-04-11 00:12:58 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
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!
Offline VaTTeRGeR
« Reply #1 - Posted 2018-04-11 07:05:17 »

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.

1  
2  
3  
4  
5  
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.
Offline SteveSmith
« Reply #2 - Posted 2018-04-11 12:09:51 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Lostmc

Junior Newbie


Exp: 1-3 months



« Reply #3 - Posted 2018-04-11 22:39:02 »

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.
1  
2  
3  
if (falling) {
            iY += gravity;
        }
Offline SteveSmith
« Reply #4 - Posted 2018-04-12 06:35:16 »

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:-

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


needs changing to

1  
2  
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
3. 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.

Offline Lostmc

Junior Newbie


Exp: 1-3 months



« Reply #5 - Posted 2018-04-12 16:28:07 »

Changing the code to

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


just made the bouncing even worse.

Heres my code for the sprite

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
 
@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());
    }
Pages: [1]
  ignore  |  Print  
 
 

 
ivj94 (586 views)
2018-03-24 14:47:39

ivj94 (49 views)
2018-03-24 14:46:31

ivj94 (383 views)
2018-03-24 14:43:53

Solater (63 views)
2018-03-17 05:04:08

nelsongames (110 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (704 views)
2018-02-28 16:59:18

buddyBro (92 views)
2018-02-28 16:45:17

xxMrPHDxx (494 views)
2017-12-31 17:17:51

xxMrPHDxx (734 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!