I've run into a bit of a snag - Up/Left movement slightly faster than Down/Right

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.

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)

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.

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().

Ah, no problem.

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.

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.

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


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.

Thanks! That worked! But I think you mean:

x + Math.round(dy * delta);

and not

Math.round(x + dy * delta);

as the latter doesn’t do anything :slight_smile:

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: