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)