Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Best way to handle wall jumping?  (Read 1238 times)
0 Members and 1 Guest are viewing this topic.
Offline Nanor

Senior Newbie


Exp: 2 years



« Posted 2014-05-14 11:28:31 »

I'm creating a game where the player ascends a tower by wall jumping his way to the top. When the player has collided with the right wall they can only jump left and vice versa. Here is part of my current implementation:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
    if(wallCollision() == "left"){
        player.setPosX(0);
        player.setVelX(0);
        ignoreCollisions = true;
        player.setCanJump(true);
        player.setFacingLeft(false);
    } else if (wallCollision() == "right"){
        player.setPosX(screenWidth-playerWidth*2);
        player.setVelX(0);
        ignoreCollisions = true;
        player.setCanJump(true);
        player.setFacingLeft(true);
    } else{
        player.setVelY(player.getVelY() + gravity);
    }

and
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
private String wallCollision(){
    if(player.getPosX() < playerWidth && !ignoreCollisions)
        return "left";
    else if(player.getPosX() > screenWidth - playerWidth*2 && !ignoreCollisions)
        return "right";
    else{      
        timeToJump += Gdx.graphics.getDeltaTime();
        if(timeToJump > 0.50f){
            timeToJump = 0;
            ignoreCollisions = false;
        }
        return "jumping";
    }
}



If the player is colliding with the left wall it will switch between the states left and jumping repeatedly due to the variable ignoreCollisions being switched repeatedly in collision checks. This will give a chance to either jump as intended or simply ascend vertically instead of diagonally.

I can't figure out an implementation that will reliably make sure the player jumps as intended. Does anyone have any pointers?
Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #1 - Posted 2014-05-15 08:42:29 »

If no-one can help me with this particular example of code can they provide me an example of a wall jump that works?
Offline T1663R

Junior Member


Medals: 2



« Reply #2 - Posted 2014-05-15 12:24:36 »

I can't figure out an implementation that will reliably make sure the player jumps as intended. Does anyone have any pointers?
i dont really understand what your trying to do. So lets asume the player hits the left wall.
What do you want to happen ?
Quote
they can only jump left and vice versa

does that mean automaticly ? or will you wait for a player input to start the jump ? and what is supposed to happen while you wait for the player input ?
Or do you want the player to fall down until the player gives the input to jump ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #3 - Posted 2014-05-15 13:34:49 »

I've taken a video to show how my game currently works here. It works sort of how I want except if you notice at 8 seconds on the left wall when I click he moves up the wall. I know the issue is because of the logic in my code.

Referring to the snippet I provided imagine you're currently flying towards the left wall. The boolean ignoreCollisions will bet set to false because you want to collide with the wall. When you collide with the wall the wallCollision() method returns "left" which stops movement and sets ignoreCollisions to true because you want to ignore the fact you're now in collision boundaries with the wall. By ignoring the collision you can jump to the opposite wall. The problem then arises that whenever wallCollision() is called it will switch between returning "left" and "jumping" because ignoreCollision will keep getting switched between true and false. This is what causes the vertical jump at 8 seconds.

I know this is very hard to understand and my code is inherently flawed so I need another way to go about it.
Offline T1663R

Junior Member


Medals: 2



« Reply #4 - Posted 2014-05-15 13:45:05 »

now i hope i understand you correctly.
i would do it like this in your loop:

first - Check for collisions - if the player collides with a wall you set a boolean to true that states this fact.
  for example "collisionLeft = true" , or "collisionRight = true", also stop the xVel if you collide.
second - apply the gravity and playermovement:

and simply perform checks there, so for example if theres a "collisionLeft", only allow the player to jump right and vice versa.
Also only allow the player to jump (basicly setting a xVel), if he is colliding with a wall, but in the opposite direction.

ive tryed to do some code here for your case:
1  
2  
3  
4  
5  
public void movePlayer(){
yVel = xyz;
if (collisionLeft && !collisionRight && Gdx.input.isKeyPressed(Keys.D)) { xVel= 15; }  // for example ^^
if (!collisionLeft && collisionRight && Gdx.input.isKeyPressed(Keys.A)) { xVel= -15; }// for example ^^
}

ofc in order for that to work, you will need to set the collision booleans like ive wrote before and set the xVel to 0 as soon as you collide with a wall.
btw ive wrote that on my phone, so im sure there are some bugs in there, but i think you will understand what i mean. Wink
Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #5 - Posted 2014-05-15 14:49:54 »

I believe I have something similar to what you have wrote. Whenever the user clicks then this piece of code is executed:

1  
2  
3  
4  
5  
6  
7  
if(player.canJump(screenWidth, playerWidth)){
            if(player.isFacingLeft())
               player.setVelX(-36.0f);
            else
               player.setVelX(36.0f);
            player.setVelY(25.0f);
         }


the canJump() method looks like this

1  
2  
3  
4  
5  
6  
   public boolean canJump(float screenWidth, int playerWidth) {
      if(posX <= playerWidth || posX >= screenWidth - playerWidth*2 || posY < 0)
         return true;
      else
         return false;
   }



I believe it does something similar to what you said?
Offline T1663R

Junior Member


Medals: 2



« Reply #6 - Posted 2014-05-15 14:52:18 »

yeah looks that way. does it work ? Cheesy

EDIT
ofc it would make sense to check for collision before doing this stuff.
and only setting xVel to 0 if you hit a wall.
Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #7 - Posted 2014-05-15 14:54:24 »

No, that's how I had it originally. The problem is to do with the ignoreCollisions variable that keeps getting switched when wallCollision() is called. Here is my full code. Maybe this will help better than my explanations!
Offline jonjava
« Reply #8 - Posted 2014-05-15 15:02:16 »

What is the significance of ignoreCollisions? Remove it.

Offline T1663R

Junior Member


Medals: 2



« Reply #9 - Posted 2014-05-15 15:13:43 »

try the following :
create 2 Instance Variables:
1  
2  
boolean collisionLeft = false;
boolean collisionRight = false;


change your code here like this :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
      if(wallCollision() == "left" && !collisionLeft){
         player.setPosX(0);
         player.setVelX(0);
         player.setFacingLeft(false);
         collisionLeft = true;
         collisionRight = false;
      } else if (wallCollision() == "right" && !collisionRight){
         player.setPosX(screenWidth-playerWidth*2);
         player.setVelX(0);
         player.setFacingLeft(true);
         collisionRight = true;
         collisionLeft = false;
      } else{
         player.setVelY(player.getVelY() + gravity);
      }

      

now in my opinion the biggest problem is that you do more than you would have to do. And since your "updateRunning()" gets called really often, you set your xVel back to 0 before youve even moved out of the wall. If you would remove all the unused Stuff you could solve this really easily. But for your case, these changes i did here COULD fix that. But im not sure since i havent looked at your whole code. But you can try it. Cheesy

EDIT
and also
What is the significance of ignoreCollisions? Remove it.
do that Grin
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #10 - Posted 2014-05-15 15:37:26 »

Thank you so much! It now works as desired!
Online Longarmx
« Reply #11 - Posted 2014-05-15 16:54:06 »

Remember to compare strings using
.equals()
and not
==
  Smiley

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (25 views)
2014-09-21 02:42:18

BurntPizza (16 views)
2014-09-21 01:30:30

moogie (19 views)
2014-09-21 00:26:15

UprightPath (26 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (44 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (100 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!