patternjavaMinor
Achieving a rotated Vector magnitude
Viewed 0 times
rotatedachievingvectormagnitude
Problem
I am trying to convert this velocity into another 2 dimensional vector at some specified angle:
At first, I was going to take 1.5, and rotate it after converting the degrees to radians, like so:
Is this the fastest way to achieve this? This will be happening a lot in my application, and I would think all the
float maximumVelocity = 1.5f;
float angleInDegrees = 23f;
Vector2 actualVelocityAs2DimensionalVector = {?,?}At first, I was going to take 1.5, and rotate it after converting the degrees to radians, like so:
double radians = Math.toRadians(angleInDegrees);
Vector2 actualVelocityAs2DimensionalVector = new Vector2(x * Math.cos(radians) - y * Math.sin(radians),
x * Math.sin(radians) + y * Math.cos(radians));Is this the fastest way to achieve this? This will be happening a lot in my application, and I would think all the
cos and sin calls are somewhat expensive.Solution
You're calling
If the angle never changes, then just save the values
Math.sin() and Math.cos() twice as many times as you need to. double cosine = Math.cos(radians)
double sine = Math.sin(radians)
Vector2 rotated = new Vector2(x*cosine - y*sine, x*sine + y*cosine)If the angle never changes, then just save the values
cosine and sine so you don't have to compute them over and over again.Code Snippets
double cosine = Math.cos(radians)
double sine = Math.sin(radians)
Vector2 rotated = new Vector2(x*cosine - y*sine, x*sine + y*cosine)Context
StackExchange Code Review Q#106063, answer score: 6
Revisions (0)
No revisions yet.