[Solved] Handling movement

So I know this is a mega newbie question, but I am trying to use an angle and hypotenuse to calculate the delta x and y.

Diagram:

The blue dot is a point that I already know, the 5 is a constant, and I know the angle theta. I just need to get dx and dy. My LWJGL is setup so that y 0 is at the top. The player’s position is a vector2f, and I just need to add the that vector.

My code so far:


float speed = Launcher.getSpeed();
float angle = getAngle(player.x, player.y);
if(Input.isKeyDown(Keyboard.KEY_W)){
	float dx = speed * (float) Math.sin(Math.toRadians(angle));
	float dy = speed * (float) Math.cos(Math.toRadians(angle));
	player.x -= dx;
	player.y -= dy;
}

Again, sorry for such a newbie question!

CopyableCougar4

Is that really where the angle is? Is the blue dot the player, or what?

Either way, you have sin and cos switched for x and y.

The angle is there, so I had to switch sin and cos for which side was opposite and adjacent.

CopyableCougar4

To be honest if you know the distance from the point to the player then its much more efficient to just figure out the DX and DY through the equation of a straight line y = mx where m is the local variable in a loop , x is the gradient (Playery - pointy)/(Playerx - pointx) and y is y of course. If you then wanted to apply this to transforming your player position you would do transformfunction(m,y); As m is the increment and y is being increased by the increment * gradient. For implementing speed you would for example


float gradient = (Playery - pointy)/(Playerx - pointx);//calculate the gradient
for(int i = 0; i < (Playerx - pointx); i+=speed){//only go on for the distance (you might want to add checks because of that speed variable.
    transformfunction(i + Playerx,(i * gradient) + Playery);//apply the transformation.
}

So I guess the issue is mostly solved, here is the code:


private Vector2f scratch = new Vector2f(); // Scratch vector
	public void move(float angle){
		float theta = (float) StrictMath.toRadians(360.0f - angle);
		float hypotenuse = 1.0f * Launcher.getSpeed();
		float dx = (float) Math.sin(theta) * hypotenuse;
		float dy = (float) Math.cos(theta) * hypotenuse;
		Vector2f.add(player, new Vector2f(-dx, -dy), scratch);
		if(map.collision(scratch, new Rectangle(0, 0, 100, 100))){
			Vector2f.add(scratch, new Vector2f(dx, dy), scratch);
		}
		player.set(scratch);
	}

To actually call this function


float angle = getAngle(Launcher.getWidth() / 2, Launcher.getHeight() / 2);
			if(Input.isKeyDown(Keyboard.KEY_W)){
				move(angle);
			}
			if(Input.isKeyDown(Keyboard.KEY_S)){
				move(180.0f + angle);
			}

CopyableCougar4