Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  I've run into a bit of a snag - Up/Left movement slightly faster than Down/Right  (Read 398 times)
0 Members and 1 Guest are viewing this topic.
Offline Nausica
« Posted 2014-05-13 01:49:04 »

The title says it all. Here is the code I use to change velocity based on input, and the code used to update coordinates based on 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  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
private static void playerControlledWASD(Entity a) {
            if (a.check(ID.Tag.velocitytag)) {
               Double speed = (Double) a.fetch(ID.Component.VELOCITY,
                     ID.Feature.SPEED);
               if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
                  a.echo(ID.Component.VELOCITY, ID.Feature.DY, -speed);
               } else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
                  a.echo(ID.Component.VELOCITY, ID.Feature.DY, speed);
               } else
                  a.echo(ID.Component.VELOCITY, ID.Feature.DY, 0d);

               if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                  a.echo(ID.Component.VELOCITY, ID.Feature.DX, -speed);
               } else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
                  a.echo(ID.Component.VELOCITY, ID.Feature.DX, speed);
               } else
                  a.echo(ID.Component.VELOCITY, ID.Feature.DX, 0d);
            }
         }

         private static void updateByVelocity(Entity a, int delta) {
            if (a.check(ID.Tag.velocitytag) && a.check(ID.Tag.positiontag)) {
               Double dx = (Double) a.fetch(ID.Component.VELOCITY,
                     ID.Feature.DX);
               Double dy = (Double) a.fetch(ID.Component.VELOCITY,
                     ID.Feature.DY);
               Double minDx = (Double) a.fetch(ID.Component.VELOCITY,
                     ID.Feature.MINDX);
               Double minDy = (Double) a.fetch(ID.Component.VELOCITY,
                     ID.Feature.MINDY);
               Double maxDx = (Double) a.fetch(ID.Component.VELOCITY,
                     ID.Feature.MAXDX);
               Double maxDy = (Double) a.fetch(ID.Component.VELOCITY,
                     ID.Feature.MAXDY);
               if (dx >= minDx
                     || dy >= minDy
                     || dx <= maxDx
                     || dy <= maxDy) {
                  int x = a.fetch(ID.Component.POSITION, ID.Feature.X);
                  int y = a.fetch(ID.Component.POSITION, ID.Feature.Y);
                  a.echo(ID.Component.POSITION, ID.Feature.X, x += dx
                        * delta);
                  a.echo(ID.Component.POSITION, ID.Feature.Y, y += dy
                        * delta);
               }


Is it something to do with using '+='? I can't find out why movement with -speed is faster than movement with +speed, but I know it's a noobish error, so go easy on me. (Also, I know this code could be better, this isn't about that though)
Offline Rayvolution

JGO Kernel


Medals: 216
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #1 - Posted 2014-05-13 01:57:37 »

Is up/down/left/right correct, it's just the 2 diagonal directions differ from each other?

Most of the time that's due to a math error when rounding numbers somewhere. But I can't identify the issue in your code anywhere (But admittingly I'm having trouble reading it)

I did notice you're using a value casted to a double in a lot of places, for example:
Double speed = (Double) a.fetch(ID.Component.VELOCITY,ID.Feature.SPEED);

Is the original value from a.fetch a float? if so, that may be your problem. When it's rounding it off to a double it may be mucking up the math.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline BurntPizza
« Reply #2 - Posted 2014-05-13 02:00:58 »

Up-casting a float to a double won't muck up anything, plus it doesn't require an explicit cast. I am wondering why you use boxes everywhere though, doesn't seem like there's any particular reason from the little bit I can see here.

I'm also having trouble deciphering this, is that some kind of ECS your using?
Mind just giving an english run-down of what this code does?

My only guess is also that it's a rounding issue, involving truncating the doubles to ints or something; that isn't a symmetric round. Try surrounding the dx * delta, dy * delta in a Math.round().
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nausica
« Reply #3 - Posted 2014-05-13 02:09:28 »

Ah, no problem.

1  
a.fetch(ID.Component, ID.Feature)
gets a component from the component hashMap. Since each Component is a Number array (Since I don't know if I may have to use doubles(like in Textures), or integers(Like frame# or x,y position)), I have to cast the returned object to the proper type.

echo does the same thing, but I don't have to cast anything since it autoboxes the input values to a Number. And yes, I am using an ECS architecture for the program.

I'm sorry the code is hard to read. I haven't decided to go through and clean it up yet, so for now I use Eclipse's built-in source formatter, which doesn't help much.
Offline Rayvolution

JGO Kernel


Medals: 216
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #4 - Posted 2014-05-13 02:21:04 »

Up-casting a float to a double won't muck up anything, plus it doesn't require an explicit cast. I am wondering why you use boxes everywhere though, doesn't seem like there's any particular reason from the little bit I can see here.

I'm also having trouble deciphering this, is that some kind of ECS your using?
Mind just giving an english run-down of what this code does?

My only guess is also that it's a rounding issue, involving truncating the doubles to ints or something; that isn't a symmetric round. Try surrounding the dx * delta, dy * delta in a Math.round().

Errr, my mistake, I got doubles and longs backwards in my head when I wrote that, and was thinking he was rounding off the float. Personally I've never had to cast a float to a long though, the few times I cast floats it's usually to an int.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline BurntPizza
« Reply #5 - Posted 2014-05-13 02:26:18 »

He is rounding (casting actually) the doubles to ints though, on the last two lines. I'm betting that's the problem.

1  
2  
3  
4  
5  
6  
int x = 4;
double d = 3.7;

x += d; // hidden down-cast!

System.out.println(x); // prints 7


Either have x and y be doubles if a.echo accepts doubles (and doesn't round them later sown the line), or use Math.round(x + dy * delta); for a nice, symmetric (read: proper) round.
Offline Nausica
« Reply #6 - Posted 2014-05-13 02:30:37 »

Thanks! That worked! But I think you mean:

1  
x + Math.round(dy * delta);


and not

1  
Math.round(x + dy * delta);


as the latter doesn't do anything  Smiley
Offline BurntPizza
« Reply #7 - Posted 2014-05-13 02:34:40 »

Ah, well it depends on exactly what dx, dy is, but whatever works.

For future reference, it is something to do with the += operator, probably one of the most underestimated operators in that most think they know what it does when they don't:

http://stackoverflow.com/questions/8710619/java-operator
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (44 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

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

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