I in the very beginning of making a small game, and I'm having some problems getting the basic movement of my character.
My character is a square that can be moved by rotating around a corner. The user chooses what corner to rotate around by pressing a button. Here is the (malfunctioning) code I've come up with this far(Java and slick2d):
// square is an org.newdawn.slick.Image and is rendered by calling square.draw()
@Override
public void update(GameContainer container, int delta) throws SlickException {
Input input = container.getInput();
if (input.isKeyDown(Input.KEY_W))
{
square.setCenterOfRotation(0, 0);
square.setRotation(square.getRotation() + 1);
}
else if (input.isKeyDown(Input.KEY_E))
{
square.setCenterOfRotation(square.getWidth(), 0);
square.setRotation(square.getRotation() + 1);
}
else if (input.isKeyDown(Input.KEY_S))
{
square.setCenterOfRotation(0, square.getHeight());
square.setRotation(square.getRotation() + 1);
}
else if (input.isKeyDown(Input.KEY_D))
{
square.setCenterOfRotation(square.getWidth(), square.getHeight());
square.setRotation(square.getRotation() + 1);
}
}
This will rotate the square correctly, but when I switch rotation direction, the square jumps two times its lenght to the side I switched to. I think the problem is that I never really move the square, just rotate it away from its original position. I think this will also make collision detection difficult.
How would I prevent this 'jumping', and how will I later do collision detection on the image? Or asked in another way, how should I be doing the 'rotation walking'?