Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I rotate an arbitrary point around an arbitrary origin in Javascript

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading