Using this question: 3D point rotation algorithm I was able to assimilate a basic rotate function for my arbitrary vector library in javascript.
The issue is, the question above doesn't explain how to rotate around any point (Like an orbit)
This vector library extends the Array object type (I originally forked it from an old project, and then adapted it for my own projects), so think of each vector like an array with extra methods and getters (You can find the whole source code here)
This is the method I use to rotate the current vector with a Vector plugged in as the angle (So each axis can be controlled separately)
It grabs the cosine and sine values required for all three axi (pitch, roll, yaw) and then stores them as three vectors in an array.
Finally it multiplies each vector into each coordinate accordingly.
rotate(angle) {
let cos = (angle.copy).map(val => Math.cos(angle));
let sin = (angle.copy).map(val => Math.sin(angle));
let other = [new Vector([
cos.z * cos.y,
cos.z * sin.y * sin.x - sin.z * cos.x,
cos.z * sin.y * cos.x sin.z * sin.x
]), new Vector([
sin.z * cos.y,
sin.z * sin.y * sin.x cos.z * cos.x,
sin.z * sin.y * cos.x - cos.z * sin.x
]), new Vector([
-sin.y,
cos.y * sin.x,
cos.y * cos.x
])];
let stored = point.copy;
this.map((val, i) => (other[i].mul(stored)).sum);
}
My main question is, how can I adapt this tool to allow me to rotate around any point in 3D space
CodePudding user response:
If you got a rotation matrix R
and want to rotate a vector v
around a point a
, then the formula for the result is
a R * (v-a)
This applies the same if the matrix-vector multiplication is from the left.