I am writing a small game in javascript. It's been a while since I have done any basic maths and I can't get some of my positioning to work properly. Apologies if this question is too simple, but I can't spot my error.
I have the following code
var xPos = 0, yPos = 0, angle = 45, distance = 1;
var yDelta = Math.sin(angle) * distance,
xDelta = Math.sqrt((distance * distance) -(yDelta * yDelta));
yPos += yDelta;
xPos += xDelta;
- Starting at coordinate (0,0)
- Move 1 step at 45 degrees
- To get the new coordinates I need to get the distance moved up
- As sin(theta) = opposite/hypotenuse this should be sin(theta) * hypotenuse
- And the distance moved across
- As this is a right angled triangle this should mean hypotenuse squared - opposite (that we calculated in the previous step) squared.
This gives me (0.5253, 0.8509), but this calculator tells me that it should be 0.7071 for each side. Can anyone explain what's wrong with my reasoning?
put angle in radians, $sin(45) = sin(\pi /4) = 0.707$ (y coordinate)
$cos(\pi /4) = 0.707$ (x coordinate)
const $ \pi = 3.1415926$