WASD Movement - Questions

So I’m working on a 2D game right now, and I am working on WASD movement. When I am moving, it seems to work fine, but then the sprite rendered by the updated coordinates just either goes in circles or turns and changes direction, and I need to know if somebody can shed some light on the issue. If you need more code posted, just ask :).

Is there something I am missing in my calculations or how the translations are handled?

Method that gets the rotation from the mouse and position
[icode]
/**
* Calculate the rotation for the character
*/
public float getZRotation(Rectangle mouse, Vector character){

	return (float) Math.toDegrees(Math.atan2(mouse.x - character.getX(), mouse.y - character.getY()));
	
}

[/icode]

Method that handles movement (I just do handleMovement(0.5f) every frame). The Vector object just stores and x and y.
[icode]
/**
* Handle movement for the player
*/
public void handleMovement(float speed){

	// get the angle
	Vector position = DisplayManager.getInstance().getRenderer().getCamera().getPosition(); 
            // gets the current position of the camera / player
	Rectangle mouse = DisplayManager.getInstance().getRenderer().getMouse(); 
           // gets the current location of the mouse and the rectangle representing it.
	float rotation = getZRotation(mouse, position);
	// handle the coordinates
	Keyboard.poll();
	float x = 0.0f;
	float y = 0.0f;
	if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
      y += (float)(Math.sin(Math.toRadians(rotation)) * speed);
      x += (float)(Math.cos(Math.toRadians(rotation)) * speed);
    }
	else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
      y -= (float)(Math.sin(Math.toRadians(rotation)) * speed);
      x -= (float)(Math.cos(Math.toRadians(rotation)) * speed);
    }
	// flip sine and cosine to be perpendicular.
	else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
      y -= (float)(Math.sin(Math.toRadians(90.0f - rotation)) * speed);
      x -= (float)(Math.cos(Math.toRadians(90.0f - rotation)) * speed);
    }
	else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
      y += (float)(Math.sin(Math.toRadians(90.0f - rotation)) * speed);
	  x += (float)(Math.cos(Math.toRadians(90.0f - rotation)) * speed);
    }
	
    y /= -1; // flip the y
    // change movement. This adds to the camera's last position.
    camera.addToPosition(x, y);
	
}

[/icode]

The issue that I see at the moment is where you are defining the characters position. You seem to be using the camera, but what location does that actually give you?

If it is not where the character is then your angle of rotation will be wrong.

Hi

The location of the camera is the center of the screen which is also where the player’s icon is centered. The angle calculated is between the center of the mouse and player.

Here’s a diagram for the angle and what the mouse and ‘player’ look like.

Hope this helps!
CopyableCougar4

Looking at it more, the method that bothers me is the Math.atan2(). What I think you should be using is Math.atan(mouse.y - character.getY()/mouse.x - character.getX());

I’m not familiar with the atan2 function so it might work but the atan should work, just make sure it is y/x.

If you want me to explain the maths just ask.

Hi

Math.atan2 just takes one step out of Math.atan :slight_smile: I believe it may also handle degrees up to 360 degrees (don’t quote me on this, not sure).

CopyableCougar4

The only difference in the atan2 function is that you won’t get divide by zero errors, because it does a check.

This, there is literally no reason to use atan if you got atan2.

But I do believe that you’ve got y and x the wrong way round.

Hi

Yeah thanks :slight_smile: I found that out last night when I was checking the javadocs for Math.atan2 :slight_smile:

CopyableCougar4