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
Pages: [1]
 ignore  |  Print
 LWJGL - Diagonal Movement Speed Limitation Issues (using Math.sqrt(2))  (Read 4021 times) 0 Members and 1 Guest are viewing this topic.
Willchill
 « Posted 2014-04-25 09:00:49 »

Hey everyone,

I'm using a system where, at each tick/update, the keyboard is checked for state changes (keys being pressed and released). The player is moved according to what keys are pressed (W for up, A for left, etc.) Recently I was trying to limit the speed of diagonal movement and followed instructions from one post, telling me to move the player by
 1 `speed / Math.sqrt(2);`
. This DID change the speed, but in an unexpected fashion. The player moved slowest when moving up and right, and fastest when moving down and left. Is this a rounding issue or something different?

My code: http://pastebin.com/GBaK435S

Thanks,

Will
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #1 - Posted 2014-04-25 09:11:01 »

It's actually speed * (sqrt(2) / 2)

Which is equal to speed * sqrt(0.5)

Willchill
 « Reply #2 - Posted 2014-04-25 09:26:27 »

I changed the formula to speed * sqrt(0.5). It had no noticeable effect on the movement speed.
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #3 - Posted 2014-04-25 09:52:03 »

Uh, I have no direct solution for this, but usually I just work with vectors:

 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 `// Very simplified class, if you use LWJGL / LibGDX use their Vector classespublic class Vec2 {  // Make them immutable or not, it's up to you  public final float x, y;  public Vec2(float x, float y) {    this.x = x;    this.y = y;  }  // These are all the methods we need ^^  public float length() {    return Math.sqrt(x * x + y * y);  }  public Vec2 div(float v) {    return new Vec2(x / v, y / v);  }  public Vec2 mul(float v) {    return new Vec2(x * v, y * v);  }  public Vec2 normalize() {    return div(length());  }}`

So now that we have your Vec class, let's implement the player logic:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `float speed = 4f;Vec2 velocity = new Vec2(0, 0);// in game logic:velocity = new Vec2(0, 0); // reset velocityif (/*pressed left*/) {  velocity = new Vec2(velocity.x - 1, velocity.y);} if (/*pressed right*/) {  velocity = new Vec2(velocity.x + 1, velocity.y);}if (/*pressed up*/) {  velocity = new Vec2(velocity.x, velocity.y - 1); // plus or minus depending on y up or y down}if (/*pressed down*/) {  velocity = new Vec2(velocity.x, velocity.y + 1);}velocity = velocity.normalize().mul(speed);// The velocity vector is always going to have the length "speed". // Normalize makes the vector have the length 1, mul() does the obvious with the length.x += velocity.x;y += velocity.y;// done.`

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Willchill
 « Reply #4 - Posted 2014-04-25 11:08:41 »

@mattheus23 I tried that, yields the same results.

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #5 - Posted 2014-04-25 11:18:35 »

@mattheus23 I tried that, yields the same results.

Can you please show me the code?

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Willchill
 « Reply #6 - Posted 2014-04-25 11:33:24 »

http://pastebin.com/5iwrM4yz
SHC
 « Reply #7 - Posted 2014-04-25 14:48:11 »

How are you applying your velocity? I think this maybe due to not using delta time.

Willchill
 « Reply #8 - Posted 2014-04-25 21:35:18 »

How are you applying your velocity? I think this maybe due to not using delta time.
The velocity is applied each update (which is limited to 60 per second) when the player is rendered relative to the x and y integers. The same was done with the old method, when this.move(int, int); was called.
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #9 - Posted 2014-04-25 21:53:48 »

How are you applying your velocity? I think this maybe due to not using delta time.
The velocity is applied each update (which is limited to 60 per second) when the player is rendered relative to the x and y integers. The same was done with the old method, when this.move(int, int); was called.

Accidental medal there :/

Anyways, you shouldn't be using integers for storing the state of the position of players. Use
 `float`
s or
 `double`
s for that purpose and only cast them to integers the moment you need to render your character.

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
SHC
 « Reply #10 - Posted 2014-04-26 03:08:44 »

Why not fix the velocities in terms of ppu (Pixels Per Update)? I think the varying speed is due to
 `Math.sqrt()`
is a native method. Not sure though.

ctomni231

JGO Wizard

Medals: 99
Projects: 1
Exp: 7 years

Not a glitch. Just have a lil' pixelexia...

 « Reply #11 - Posted 2014-04-26 12:12:55 »

Do you manipulate
 ` this.x `
or
 ` this.y `
in any way before rendering? Obviously this action triggers a lot more moving in a positive direction than in a negative direction. The only explanation for that is that maybe the keyboard handler is biased for up and right...

Did you use an else if ladder to disable and reenable key presses and releases? Can you please show the code for the press and release handler?

SHC
 « Reply #12 - Posted 2014-04-26 12:25:50 »

Or even better, use the resultant velocity.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `float vx, vy;if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){    vx -= 4;}if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){    vx += 4;}if (Keyboard.isKeyDown(Keyboard.KEY_UP)){    vy += 4;}if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)){    vy -= 4;}this.x += vx;this.y += vy;`

Hope this helps.

Pages: [1]
 ignore  |  Print

 ivj94 (584 views) 2018-03-24 14:47:39 ivj94 (48 views) 2018-03-24 14:46:31 ivj94 (382 views) 2018-03-24 14:43:53 Solater (62 views) 2018-03-17 05:04:08 nelsongames (109 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (702 views) 2018-02-28 16:59:18 buddyBro (92 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (733 views) 2017-12-31 17:15:51
 KaiHH 12x SHC 10x NuclearPixels 10x Zemlaynin 10x ByerN 7x Spasi 6x Damocles 6x Guerra2442 6x VaTTeRGeR 5x ags1 4x orangepascal 4x philfrei 4x princec 3x ndnwarrior15 3x mesterh 3x Phased 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-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