A
A
Arthurmiko2018-05-01 18:25:18
JavaScript
Arthurmiko, 2018-05-01 18:25:18

How to rotate a vector without error?

There are the following elements:
Vector Coordinate rotation function
a = {x: 1, y: 0}

function rotate(angle) {
  a.x = a.x * Math.cos(angle) - a.y * Math.sin(angle);
  a.y = a.x * Math.sin(angle) + a.y * Math.cos(angle);
}

Which is called in this way The problem is that with each turn, accuracy is lost. This can be detected by checking the length of the vector. Initially, the length is 1 after 10 calls - 0.9999671067997294 after 100 - 0.9831222411821932 And so on. Moreover, if you increase the rotation angle when calling rotate, then the error increases. Please tell me what is the reason and how to get rid of this error? Maybe it's worth using another way to rotate the coordinates? Which?
rotate(1 * Math.PI / 180);
Math.sqrt( Math.pow(a.x, 2) + Math.pow(a.y, 2) )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Codebaker, 2018-05-01
@Codebaker

To maintain accuracy, you will have to store the entire history of rotations with the preservation of the sign (clockwise +, counterclockwise -) and to calculate the latter - sum the degrees.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question