What is the process for rotating a vector around a random axis?

Imagine I have an imaginary axis created by two vectors. For instance, one vector points upwards at x = 10:

const startingPosition = new Vector3(10, 0, 0)
const endPosition = new Vector3(10, 0, 1)

To find the direction of this axis, you can do the following:

const axisDirection = new Vector3().subVectors(endPosition, startingPosition).normalize()

Now, how do I go about rotating a vector (for example, Vector3(50, 0, 0)) around this original axis?

I attempted using

Vector3.applyAxisAngle(axisDirection , radians)
, but since the axis was normalized, the rotation occurred around the world center (0, 0) instead of around the initial position of the axis.

Answer №1

I managed to solve this problem by identifying the precise point on the axis around which the point rotates. I utilized a helpful solution from this answer and converted the pseudocode into typescript:

getPivotPoint(pointToRotate: Vector3, axisStart: Vector3, axisEnd: Vector3) {
    const d = new Vector3().subVectors(axisEnd, axisStart).normalize()
    const v = new Vector3().subVectors(pointToRotate, axisStart)
    const t = v.dot(d)
    const pivotPoint = axisStart.add(d.multiplyScalar(t))
    return pivotPoint
  }

Following @Ouroborus's guidance, I then proceeded to translate the point, apply the rotation, and revert the translation:

rotatePointAroundAxis(pointToRotate: Vector3, axisStart: Vector3, axisEnd, radians: number) {
    const axisDirection = new Vector3().subVectors(axisEnd, axisStart).normalize()
    const pivotPoint = getPivotPoint(pointToRotate, axisStart, axisEnd)
    const translationToWorldCenter = new Vector3().subVectors(pointToRotate, pivotPoint)
    const translatedRotated = translationToWorldCenter.clone().applyAxisAngle(axisDirection, radians)
    const destination = pointToRotate.clone().add(translatedRotated).sub(translationToWorldCenter)
    return destination
  }

The above code is functioning smoothly, so I am documenting it here for my own reference and for others who may benefit from it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

attempting to employ the method of copying by converting result into a JSON string

Currently, I am utilizing the browser console to scrape and organize content with JS. Below is the code snippet: This represents my result array var arr = [ "George\nPresident & Founder", "content", "Ronald\nCount ...

What is the best way to incorporate a mute/unmute button into this automatically playing audio?

Seeking assistance in adding a mute button for the background sound on my website. Can anyone provide guidance on how to achieve this? Below is the HTML code responsible for playing the sound: <audio id="sound" autoplay="autoplay" ...

Guide on determining if the value in a JSON object is a string or an array in Node.js

Running a Node.js application, I encountered the following JSON array structure. First JSON object: var json1= { bookmarkname: 'My Health Circles', bookmarkurl: 'http://localhost:3000/', bookmark_system_category: [ '22&apos ...

Transferring Data in Vue.js Components through Props

I've encountered an issue while trying to pass a prop from the main Vue instance to a component. While one of the props is being successfully passed, the second one seems to be causing some trouble. Main Instance var app7 = new Vue({ el: &apos ...

Exploring the integration of query parameters in Postman using mongoose

In my code, I have a driver.js file that holds a driver schema, and a driverController.js file with REST methods including GET, POST, DELETE, and PUT. What I want to achieve is to send a GET request to http://localhost:3000/drivers?available=true This re ...

Is there a new option to replace cascade shadow now that it has been taken out of three.js?

My scene is quite extensive, with the directional light positioned far above the ground resulting in weak and unattractive shadows on objects. With the removal of CSM from three.js, I am seeking an alternative solution to enhance shadow quality in a larg ...

React/MUI Popover not aligning properly due to anchorPosition issue

I'm currently implementing a React/MUI Popover within a List element from react-window. However, I'm facing an issue with positioning the Popover correctly, as it always ends up in the top left corner of the window. This occurs because the compon ...

Utilizing the variable $this outside of an object context within an Ajax function

Encountering the following error: Fatal error: Uncaught Error: Using $this when not in object context in E:\xampp\htdocs\StudentGuideBook\Version1.0\models\AjaxChecking.php:4 Stack trace: #0 {main} thrown in E:\xampp&b ...

When the JSON array is converted into a string, it appears as undefined

Below is a snippet of my service.spec.ts file: service.spec.ts it('should mock the http requests', inject([Service, MockBackend], (service, mockBackend) => { let result:any; mockBackend.connections.subscribe((connection) => { ...

Aligning a quadrilateral to be perpendicular to a sphere through rotation

Currently, I am developing an OpenGL application that involves a sphere with a specific radius and an array of latitude/longitude points. My goal is to display the sphere and then position a square at each latitude/longitude coordinate. However, I am stru ...

Using React to map through a nested array within an object

In an attempt to extract the nested array "records", my current approach shows the array in the react console, but with an error. I will try to be brief while still providing necessary details. Upon encountering an error, there are 3 lines specifically po ...

Adding a property during runtime to an object does not display in the current "this" context

Can anyone explain why the dynamic property added using Object.defineProperty does not show up when printing 'this' on the console? Here is a Decorator example. It seems to be working fine, but the success and error properties do not appear in & ...

Accessing the parent scope from a directive within a nested ng-repeat in AngularJs

Seeking guidance on accessing the parent scope within a directive nested in an ng-repeat. Here is an example: <div ng-app="myApp" ng-controller="myCtrl"> <div ng-repeat="section in sections"> {{section.Name}} <div ng-rep ...

How can I position text in the top right corner of a v-card's v-img component in Vuetify.js?

I am using Vuetify.js and I am trying to show a single word on the right side of an image within a v-card: <v-card> <v-img src="https://cdn.vuetifyjs.com/images/cards/desert.jpg" aspect-ratio="2.75"> <span class= ...

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

What is the best way to utilize a portion of the data retrieved from an API call as the output for a function?

After extensive research and testing, I have been exploring ways to make API calls in node js. Currently, my focus is on utilizing a portion of the JSON object returned from an API call within a module to generate a Token. var request = require("request") ...

Separating Main Page Content from Sidebar for Independent Scrolling

In the layout of our website, there is a narrow column on the left containing menu options, while the right side is occupied by the main page content. The challenge I am facing is figuring out how to ensure that the page content can scroll independently ...

Managing a large number of records in a for loop on a Node.js server can be challenging, especially when dealing with nearly

After setting up a NodeJS server and connecting it to a MySQL database with around 5000 users, I needed to read the data from MySQL and update a MongoDB database. I managed to write code for this process. https://gist.github.com/chanakaDe/aa9d6a511070c3c78 ...

Ways to reload the page following the submission of a form

I implemented a fade dialog to present a form, and successfully submitted the form using ajax to create a new record in the database. Now, I am facing an issue where I cannot refresh the page after the ajax call finishes. Despite attempting to use JavaScr ...

When attempting to use the search bar to filter in ReactJs, an error occurs: TypeError - Unable to access properties of undefined (specifically 'filter')

When I receive data from my JSON server on the console, everything looks good. But when I try to type something in order to filter it, an unhandled error occurs with the message: 1 of 1 Unhandled Error Unhandled Runtime Error: TypeError: Cannot read prop ...