Reputation: 644
So Im working on a project in javascript and need to set x momentum and y momentum based on the angle. So say there is a 45 degree angle it would set the x momentum and the y momentum to 20 each so it goes in a line. How would I got about converting any angle in either degrees or radians into momentum?
Upvotes: 0
Views: 149
Reputation: 2207
To use Math.sin
and Math.cos
, you have to convert degree to radian:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin
Math.sin(x)
:x
- a number (given in radians)
If you have radian, then you can use Math.cos
to calculate the X
factor and Math.sin
to calculate the Y
factor.
function degreeToRadian(degree)
{
return degree * Math.PI / 180;
}
function getMomentumFactors(radian)
{
return {x:Math.cos(radian),y:Math.sin(radian)};
}
var example = getMomentumFactors(degreeToRadian(45));
var momentum = 20;
console.log('x',momentum*example.x,'y',momentum*example.y)
Upvotes: 2