Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Gravity and collision problem on: 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());
    }
2  Game Development / Newbie & Debugging Questions / Re: Gravity and collision problem on: 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;
        }
3  Game Development / Newbie & Debugging Questions / Gravity and collision problem on: 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!
Pages: [1]
 
EgonOlsen (1672 views)
2018-06-10 19:43:48

EgonOlsen (1720 views)
2018-06-10 19:43:44

EgonOlsen (1161 views)
2018-06-10 19:43:20

DesertCoockie (1587 views)
2018-05-13 18:23:11

nelsongames (1187 views)
2018-04-24 18:15:36

nelsongames (1716 views)
2018-04-24 18:14:32

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

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

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

Solater (972 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!