Sync shape's rotation to it's movement direction

66 views Asked by At

While practicing wanted to create basic game-kind-of(?) - there is just a basic shape (rectangle) and you move forward with one button, move backwards with another, and rotate it with 2 additional buttons, that shape as well should move to other direction accordingly to it's rotation. What i have done so far is:

Input handling:

if (input.isKeyDown(Input.KEY_UP)) {
        rectX += Math.cos(Math.toRadians(rotation)) * (0.2*delta);
        rectY += Math.sin(Math.toRadians(rotation)) * (0.2*delta);
    }
    if (input.isKeyDown(Input.KEY_DOWN)) {
        rectX -= Math.cos(Math.toRadians(rotation)) * (0.2*delta);
        rectY -= Math.sin(Math.toRadians(rotation)) * (0.2*delta);
    }
    if(input.isKeyDown(Input.KEY_LEFT)){
        rotation-=(0.1*delta);
        rectangle = rectangle.transform(Transform.createRotateTransform(rotation, rectX, rectY));
        if(rotation <= 0f) {
            rotation = 360f;
        }
    }
    if(input.isKeyDown(Input.KEY_RIGHT)){
        rotation+=(0.1*delta);
        rectangle = rectangle.transform(Transform.createRotateTransform(rotation, rectX, rectY));
        if(rotation >= 360f) {
            rotation = 0f;
        }
    }
    rectangle.setCenterX(rectX);
    rectangle.setCenterY(rectY);
}

rectX, rectY are obviously center coordinates of rectangle (floats) and rotation is... well rotation.. And I think that's it, other code isn't worth mentioning. In my opinion this should work, but the thing is while movement direction is changin at normal speed, my rectangle is spinning wildly, probably about 100 times or so faster than its movement direction.

Delta is the number of milliseconds since the last call to update().

0

There are 0 answers