Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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] Player Movement lagging  (Read 3592 times)
0 Members and 1 Guest are viewing this topic.
Offline Zeta

Junior Devvie


Medals: 8
Exp: 1 year



« Posted 2014-05-04 20:37:36 »

So I'm trying to make the player move using Box2D. The player moves left and right using 'A' and 'B' keys.
When I press 'A' and 'D' very fast the movement lags or should i say twitches. Here is the code.

The Player class extends InputAdapter and I Override 2 methods, KeyDown and KeyUp.
The movement is Vector2 variable.
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  
@Override
   public boolean keyUp(int keycode) {
      if(Keys.A == keycode || Keys.D == keycode && movement.x > 0) {
         body.setAngularVelocity(0);
         movement.x = 0f;
         body.setLinearVelocity(movement);
      }else
         return false;
     
      return true;
   }
   
   @Override
   public boolean keyDown(int keycode) {
      switch(keycode) {
      case Keys.A:
         movement.x = -velocity;
         break;
      case Keys.D:
         movement.x = velocity;
         break;
      default:
         return false;
      }
      return true;
   }


The Player class has also update method where I apply the movement
1  
2  
3  
public void update() {
         body.applyLinearImpulse(movement, new Vector2(), true);
   }


And then I've just used the players update method in the render method like so player.update();
of course the i've added listenenrs and so on. Is there something that I've forgotten?

Here is the .jar file.
http://www.mediafire.com/download/x5aogpoqahcb87h/test.jar
Is it normal that the file is 18Mb?? Cheesy

Thank you very much.

A clever person solves a problem. A wise person avoids it. -Albert Einstein
Offline T1663R

Junior Devvie


Medals: 2



« Reply #1 - Posted 2014-05-04 22:06:44 »

hey,

im also working with box2d for some time.
can you explain your lagging ? what is lagging ? and how ?
also, try applying force instead of an impulse.
And youve really gotta watch out that u use the right values for your player or entities box2d defs.

Edit
just tryed your jar, i thought you mean lag in a other way Cheesy
now i see what you mean.
But i dont see any twiching there.
The bad thing is the switch case there. Because if the user presses both buttons, or one button directly after the other button. Or he lets on pressed after that you get strange results, like holding "A" button and the player standing still and stuff. And by pressing the buttons really fast you create situations like these.
Offline Zeta

Junior Devvie


Medals: 8
Exp: 1 year



« Reply #2 - Posted 2014-05-04 23:04:00 »

Thanks for the reply. I know that my explanation isn't the best, but the .jar says it all Grin

I removed the switch case in replaced it with if statements (without else) and it still does not work.
I dont want to use applyForce(); for the player.I've had this with all my projects and because I just can't fix it I usually stop doing those projects Sad.

A clever person solves a problem. A wise person avoids it. -Albert Einstein
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline T1663R

Junior Devvie


Medals: 2



« Reply #3 - Posted 2014-05-05 10:35:35 »

I removed the switch case in replaced it with if statements (without else) and it still does not work.
ive got a similar programm and do it like this :
1  
2  
3  
4  
5  
6  
7  
if (Gdx.input.isKeyPressed(Keys.W) && Gdx.input.isKeyPressed(Keys.A)) {
         DO-Stuff;
         return;
      }
if (Gdx.input.isKeyPressed(Keys.W)) {
DO-Stuff;
return;

and so on, i tryed it before with key up and keydown in a similair way like your trying it and it caused problems.
but i think you could try to solve it with some booleans.
Like :
1  
2  
boolean aPressed = false;
boolean dPressed = false;

and the keydowns set these, the keyups reset these and if "a" was pressed, pressing "d" gets ignored and if "d" was pressed "a" gets ignored. until they got resseted with the keyup.
But no idea if this works.
Offline Zeta

Junior Devvie


Medals: 8
Exp: 1 year



« Reply #4 - Posted 2014-05-06 10:04:45 »

Fixed it finally. I removed the keyDown,keyUp and update method for player and instead replaced them with this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public void move() {
         if(Gdx.input.isKeyPressed(Keys.A)) {
            movement.x = -velocity;
            body.applyLinearImpulse(movement, new Vector2(), true);
         }
         
         if(Gdx.input.isKeyPressed(Keys.D)) {
            movement.x = velocity;
            body.applyLinearImpulse(movement, new Vector2(), true);
         }
         
         if(!(Gdx.input.isKeyPressed(Keys.A)) && !(Gdx.input.isKeyPressed(Keys.D)))
            body.setLinearVelocity(new Vector2());
   }

I'm just wondering is there a better way to do this? Anyways Thanks alot for helping out.

A clever person solves a problem. A wise person avoids it. -Albert Einstein
Offline T1663R

Junior Devvie


Medals: 2



« Reply #5 - Posted 2014-05-06 10:49:40 »

yeah i think it may be possible to solve this with booleans the way i described it in my previous post, but i havent tryed it.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (463 views)
2019-09-04 15:33:17

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

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

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

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

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

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

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

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

nelsongames (4782 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

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