Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (624) 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 1263 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: 354
Projects: 11
Exp: 3 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: 121
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: 121
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: 121
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

You cannot reply to this message, because it is very, very old.

 ClaasJG (30 views) 2015-04-27 13:36:51 BurntPizza (34 views) 2015-04-23 03:42:11 theagentd (37 views) 2015-04-22 16:23:07 Riven (51 views) 2015-04-16 10:48:47 Duke0200 (60 views) 2015-04-16 01:59:01 Fairy Tailz (43 views) 2015-04-14 20:13:12 Riven (47 views) 2015-04-12 21:36:37 bus hotdog (66 views) 2015-04-10 02:39:32 CopyableCougar4 (67 views) 2015-04-10 00:51:04 BurntPizza (72 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x Spasi 15x wessles 15x alwex 11x kingroka123 11x Riven 7x kevglass 7x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Hanksha 7x chrislo27 7x KevinWorkman 6x trollwarrior1 5x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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