Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  LWJGL - Diagonal Movement Speed Limitation Issues (using Math.sqrt(2))  (Read 979 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
Online HeroesGraveDev

JGO Kernel


Medals: 294
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)

Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 113
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 classes
public 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 velocity
if (/*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
Offline Willchill
« Reply #4 - Posted 2014-04-25 11:08:41 »

@mattheus23 I tried that, yields the same results.

VIDEO: https://www.youtube.com/watch?v=3Jj7pU0o5hU
Offline matheus23

JGO Kernel


Medals: 113
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.

VIDEO: https://www.youtube.com/watch?v=3Jj7pU0o5hU

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
Offline Willchill
« Reply #6 - Posted 2014-04-25 11:33:24 »


http://pastebin.com/5iwrM4yz
Online 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.

Offline 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.
Offline matheus23

JGO Kernel


Medals: 113
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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online 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.

Offline 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?

Online 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.

 

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

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 17:13:56

xFryIx (73 views)
2014-11-13 17:34:49

digdugdiggy (52 views)
2014-11-13 02:11:50

digdugdiggy (46 views)
2014-11-13 02:10:15

digdugdiggy (40 views)
2014-11-13 02:09:33

kovacsa (65 views)
2014-11-08 00:57:14

TehJavaDev (69 views)
2014-11-04 03:04:50

BurntPizza (68 views)
2014-11-03 23:54:52

moogie (83 views)
2014-11-03 11:22:04

CopyableCougar4 (82 views)
2014-11-02 04:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-10 03:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-03 03:36:02

List of Learning Resources
by Longor1996
2014-08-16 15:40:00

List of Learning Resources
by SilverTiger
2014-08-06 00:33:27

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

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

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

List of Learning Resources
by SilverTiger
2014-07-31 21:26:06
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!