Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (797)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
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  
  LibGDX box2D Moving Platfromer and Player  (Read 1447 times)
0 Members and 1 Guest are viewing this topic.
Offline ingrid_dev

Junior Newbie





« Posted 2019-05-01 11:45:18 »

Hi.

I want player to stay on the moving platform without sliding and if i want to move i want player to move freely on platform even when platform moves.
So what i did is i add moving platform's velocity to player's body but player still sliding. I look up the problem on google and here but can't find a solution. Some people add platform's speed to player body's setTransform method but it is just not working or i am doing something wrong.

I am adding executable jar file:
https://drive.google.com/file/d/1hvn6S7phjv051h8OA1UBNrfolJwEteTf

and source code:
https://github.com/ingriddev/Libgdx-Box2d-Platformer

playerUpdate method in Player class is where i add platform's velocity to player.

To control player use arrow keys and press 'x' to show box2d debug renderer.
I hope i explain it good.
Thanks in advance. Smiley
Offline SugarBlood
« Reply #1 - Posted 2019-05-04 14:49:20 »

Hi, have you tried to add some more friction to the moving platform? What happens then, is it makes movement on the platform difficult? To think about it as a simulation, there have to be more downforce for the player, I guess. Also there is kinda wacky solution - to add an invisible helper object that will attach player to the platform until player jump Tongue
Offline VaTTeRGeR
« Reply #2 - Posted 2019-05-04 20:03:12 »

I found the bug that causes your issue, you dampen the linear velocity after you apply the velocity of the moving platform:
https://github.com/ingriddev/Libgdx-Box2d-Platformer/blob/master/core/src/com/ingriddev/game/entities/Player.java#L90
1  
2  
3  
4  
5  
6  
7  
        // This also runs while on a moving platform duh
        if(!isPressingAnyButton()) {
            float lx = body.getLinearVelocity().x *= 0.78f;  //TODO dampling value
            //float ly = body.getLinearVelocity().y *= 0.9f;
            body.setLinearVelocity(lx, body.getLinearVelocity().y);

        }

You're disabling this when no button is pressed but it's still enabled when the player is on a platform, that's what is slowing down the player while on a platform.

You can't simply fix this by disabling the dampening while on a platform though, as this causes another bug! The player won't slow down again if you move while standing on a platform. Instead you need to separate the platform velocity and player velocity by keeping track of the player velocity separately and then adding the platform velocity to that if on a platform.

As another option, have tried working with the friction variable yet as SugarBlood suggested? Maybe you could go by without applying platform velocity altogether if you use friction, read this topic: https://www.badlogicgames.com/forum/viewtopic.php?f=11&t=15894
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ingrid_dev

Junior Newbie





« Reply #3 - Posted 2019-05-05 09:03:04 »

have you tried to add some more friction to the moving platform?

I tried and yes it makes movement on platform really difficult if i add more friction to platform player doesn't move at all. Sliding problem solved my removing the dampening value.
For movement on platforms i am thinking something like this: When platform moves to left faster and player wants to move left, instead of adding just platform's velocity to player i will add extra values to make it move normally just like on the ground and if platform moves to the right i will extract some values to stop the player moving so fast to left and vice versa.

Thank you for the answer. Wink

Offline ingrid_dev

Junior Newbie





« Reply #4 - Posted 2019-05-05 09:04:48 »

I found the bug that causes your issue, you dampen the linear velocity after you apply the velocity of the moving platform:

Thank you. Now player stay on the platform without sliding. Cheesy I remove that dampening value completely and i will work with friction values now. Movement on the ground feels weird now. Player stuck at walls because of the friction but i can add sensors to left and right side of the player and disable the movement when sensors touched walls. Or i can add wheels under the player. I update the code on github.
Offline VaTTeRGeR
« Reply #5 - Posted 2019-05-05 09:37:10 »

Quote
I remove that dampening value completely and i will work with friction values now. Movement on the ground feels weird now.

Well... What did you expect? Read this again:

Quote
Instead you need to separate the platform velocity and player velocity by keeping track of the player velocity separately and then adding the platform velocity to that if on a platform.

You can and should keep your dampening code, just make sure you only apply the dampening on the player initiated portion of the velocity by tracking the player speed separately in some class level variable and you're golden.
Offline ingrid_dev

Junior Newbie





« Reply #6 - Posted 2019-05-06 08:21:40 »

Quote
Instead you need to separate the platform velocity and player velocity by keeping track of the player velocity separately and then adding the platform velocity to that if on a platform.

I tried something. What do you think? I removed the dampening code because this one works better i think.
Jar file:
https://drive.google.com/open?id=1LOxWiRm2z9_dmfZTzYjDqUaBX-Ojkixy

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  
    private float velocity = 0;
    private float run_velocity = 12;
    private float jump_velocity = 12;
    private float increase_value = 2f;
    private float decrease_value = 1f;

    private void playerUpdate(){

        if(rightButton && !leftButton){
            velocity += increase_value;
            playerState = State.RUN;
            if(velocity >= run_velocity){
                velocity = run_velocity;
            }
        }

        if(!rightButton && leftButton){
            velocity -= increase_value;
            playerState = State.RUN;
            if(velocity <= -run_velocity){
                velocity = -run_velocity;
            }
        }

        if(!rightButton && !leftButton){ //&& !isTouchedPlatform){
            if(velocity > 0){
                velocity -= decrease_value;
                if(velocity <= 0){
                    velocity = 0;
                }
            }

            if(velocity < 0){
                velocity += decrease_value;
                if(velocity >= 0){
                    velocity = 0;
                }
            }

            if(velocity == 0){
                velocity = 0;
            }
        }

        body.setLinearVelocity(velocity, body.getLinearVelocity().y);

        if (upButton && ground > 0) {
            body.applyLinearImpulse(new Vector2(0, jump_velocity), body.getWorldCenter(), true);
        }

        if(isTouchedPlatform){

            float mpx = movingPlatform.getLinearVelocity().x;
            float mpy = movingPlatform.getLinearVelocity().y;

            if(!rightButton && !leftButton && !upButton) { // if we stand on platform.
                velocity = 0;
                body.setLinearVelocity(mpx, mpy);

            }else{  // if we try to move on platform.

                if (!upButton) {

                    if(mpx > 0 && velocity > 0){
                        body.setLinearVelocity((velocity /2) + mpx, mpy);
                    }

                    if(mpx < 0 && velocity <  0){
                        body.setLinearVelocity((velocity /2) + mpx, mpy);
                    }

                    if((mpx > 0 && velocity < 0) || (mpx < 0 && velocity > 0)){
                        body.setLinearVelocity(velocity + mpx, mpy);
                    }

                }
            }
        }
    }

Offline VaTTeRGeR
« Reply #7 - Posted 2019-05-06 09:06:03 »

This works well with slow platforms and is absolute garbage with fast ones (player does not retain platform speed after jumping) because you do not actually layer the velocities.

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  
    private float velocityX = 0;
    private float run_velocity = 12;
    private float jump_velocity = 12;
    private float increase_value = 2f;
    private float decrease_value = 1f;

    private void playerUpdate(){

        if(rightButton && !leftButton){
            velocityX += increase_value;
            playerState = State.RUN;
            if(velocityX >= run_velocity){
                velocityX = run_velocity;
            }
        }

        if(!rightButton && leftButton){
            velocityX -= increase_value;
            playerState = State.RUN;
            if(velocityX <= -run_velocity){
                velocityX = -run_velocity;
            }
        }

        if(!rightButton && !leftButton){ //&& !isTouchedPlatform){
            if(velocityX > 0){
                velocityX -= decrease_value;
                if(velocityX <= 0){
                    velocityX = 0;
                }
            }

            if(velocityX < 0){
                velocityX += decrease_value;
                if(velocityX >= 0){
                    velocityX = 0;
                }
            }

            if(velocityX == 0){
                velocityX = 0;
            }
        }

        // Layer the platform velocity on top of the bodies velocity if on a platform
        if(isTouchedPlatform){

            float mpx = movingPlatform.getLinearVelocity().x;
            float mpy = movingPlatform.getLinearVelocity().y;
           
            // Vertical movement can't be added up since you do not have a constant reference like with velocityX
            // adding body.getLinearVelocity().y and mpy would make the player spazz around on up/down movement.
            // Idk if this makes the player stick to the platform when trying to jump though
            body.setLinearVelocity(velocityX + mpx, mpy);

        } else {
            body.setLinearVelocity(velocityX, body.getLinearVelocity().y);
        }

        if (upButton && ground > 0) {
            body.applyLinearImpulse(new Vector2(0, jump_velocity), body.getWorldCenter(), true);
        }
    }
Offline ingrid_dev

Junior Newbie





« Reply #8 - Posted 2019-05-06 10:29:33 »

I fixed the jumping problem like this.(probably not a good solution to be honest) It looks okay i guess. Also changed float velocityX value to Vector2 class. Like you said i need to vertical reference like horizontal one. I am working on it.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
if(isTouchedPlatform) {

    float mpx = movingPlatform.getLinearVelocity().x;
    float mpy = movingPlatform.getLinearVelocity().y;

    if (!upButton) {
        body.setLinearVelocity(velocity.x + mpx, mpy);
    }else{
        body.setLinearVelocity(velocity.x + mpx, body.getLinearVelocity().y);
    }
}else{ //.....
Offline fenaryh
« Reply #9 - Posted 2019-06-23 23:02:40 »

If I may, I would recommend use State Pattern for your player states, otherwise you gonna have a bunch of flags around your inputs and that's really error prone.

something like:
1  
2  
3  
JumpingState
onGroundState
onPlatformState


The State class can be a nested class, for example.

Then you just use inputs for each state and don't need to worry about flags all around the house.

Good lecture: http://gameprogrammingpatterns.com/state.html

Regards!
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (3667 views)
2018-11-16 13:46:03

hadezbladez (1310 views)
2018-11-16 13:41:33

hadezbladez (3674 views)
2018-11-16 13:35:35

hadezbladez (707 views)
2018-11-16 13:32:03

EgonOlsen (4018 views)
2018-06-10 19:43:48

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

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

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

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

nelsongames (4750 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

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