Rotate the bone using Quaternions until it is aligned with a Vector3 in Three.js

I am trying to align a bone with a specific Vector3 (directionToTarget) by rotating it. Here is the code snippet:

const directionToTarget = new THREE.Vector3(random, random, random); //random pseudocode values
directionToTarget.normalize();

var Hand2worldQ = new THREE.Quaternion();
this._anchor['LeftHandIndex1'].getWorldQuaternion(Hand2worldQ); // retrieves quaternion for LeftHand bone

this._mesh.skeleton.bones[ 0 ].quaternion.set( SomeFunctionThatMakesVecintoQuarternion(directionToTarget ); 
// applies rotation of LeftHand to _mesh skeleton bones

Currently, I am in search of a function called SomeFunctionThatMakesVec3intoQuarternion(directionToTarget )

Answer №1

Object3D initially aligns with the 0, 0, 1 axis. You can utilize the Object3D.lookAt() method to adjust this object's orientation towards a specific target vector. Afterward, you can extract the resulting rotation for any further use:

// Create an abstract object facing the default direction
const obj = new THREE.Object3D();

// Rotate it to face the target coordinates
obj.lookAt(targetX, targetY, targetZ);

// Obtain the final rotation
const quaternion = obj.quaternion;
console.log(quaternion);

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

Update the content that corresponds to the regular expression in jQuery

I am on a quest to locate specific content that matches a regular expression across an entire webpage and then replace it with different text. Below is the code I have been utilizing for this task. var reg = /exam+ple/; $("body").html(function () { ...

I'm attempting to retrieve information from my vuex store, however, encountering an error in the process

I've encountered an issue with vuex getters while working on my project. I have a route that showcases all users, and upon visiting this route, the AllUsers.vue component is displayed. Within this component, I'm utilizing the UsersList.vue compo ...

Guidelines for managing UnprocessedItems with the AWS JavaScript SDK for dynamoDB

Currently, I'm facing an issue while attempting to utilize an AWS Lambda function for handling events from SendGrid. The event is expected to be in the form of an array containing a variable number of JSON objects, each representing a specific event. ...

JS templating language inspired by Jinja

I find the django/jinja2 templating languages to be truly exceptional. Their syntax is straightforward yet incredibly flexible. I'm wondering if there is a JavaScript library that offers similar simplicity and versatility, or at least matches their ca ...

JavaScript -Error: Unable to access the 'children' property as it is undefined

I'm currently working on creating a tree and calculating its height. The map array is properly initialized within the for loop, but when I run console.log(map+" this is map");, it displays [object Object],[object Object],[object Object],[object Objec ...

What is the best method to remove a value from a JSON object in a CSV file?

I received a JSON response like this: xxx: ["fsd,das"] I am looking for a way to remove the value "fsd" from the JSON object. The issue is that the response inside the JSON is not actually an array, but rather a CSV format. How can I go about deleting it ...

ways to undo modifications made to a value within an input field using jquery or javascript

$(document).ready(function () { //Highlight row when selected. $(function () { $('#Cases tr').click(function () { $('#Cases tr').removeClass('selectedRow'); $(this).addClass(&apos ...

Is there a way to prompt the browser's default handler to execute prior to the event propagation?

When working with a textbox (<input type="text">) in the browser, it's important to note that keystrokes are not processed until after all JS event handlers have completed execution. In my application, I encountered a scenario where a ...

Is there a way to link to mouseover and mouseleave actions while utilizing ng-options?

I am trying to create a directive that will handle the mouseover and mouseleave events for the option elements within this select element: <select class="form-control" custom-directive="" ng-model="vm.selectedPersonalInfo" ng-options="personalInfo as p ...

The Node.js POST request is unexpectedly returning 'undefined'

I'm currently working on a project for an online course and I'm encountering an issue with making an API call. It seems that I am getting undefined responses to my post request because the user input is not being retrieved properly. Below are the ...

Substitute the nested object's value with a variable structure

I have an object structured like this: const obj = {a: {x: 0, y: 0}} but it can also be structured like this: const obj = {a: {x: 0, y: 0}, b: {x: 10, y: 3}, abcd: {x: -1, y: 0}} This means that the obj can contain multiple keys with variable key names. ...

What is the most effective way to retrieve all child Elements within a parent container?

Is there a way to retrieve all descendant elements within the parent container and store them in an array? <div class="parent"> <div class="child1"> <span class="child2"> <div class="child3"> ...

Switch input trigger for immediate page refresh rather than standard input clearing

I want to customize how the clear input option functions. Normally, when you click on the X clear button, it clears the search input field. Instead of clearing the search input, I would like the clear button to trigger a page reload. This is the X button ...

Is it necessary to create distinct "response scripts" for each device?

The Responding script is the one that will handle requests from Showing scripts. This script has the ability to add, retrieve, update, and delete data while returning the result in JSON format. An example of the output: {"success":0,"error":"userNam ...

Obtain information from server-side data received from dynamically generated input fields

In my current situation, I have a text input field and a button that, when clicked, adds additional input fields to the page. The JavaScript code I use to achieve this functionality is as follows: var myBtn = document.getElementById('myBtn&apos ...

The Angular JS controller failing to trigger the Spring MVC function

I'm currently working on integrating Angular JS with an existing Spring MVC project and encountering an issue when calling a Spring controller from the Angular JS controller. Here is a snippet of my app.js: 'use strict'; var AdminApp = ang ...

Should jQuery be used alongside AngularJS for a better development practice?

After using jQuery for many years, I've decided to explore new ways of building single page websites. My choice is AngularJS. I'm currently contemplating whether it's a good practice to combine jQuery with AngularJS? Here's an example ...

What is the best way to incorporate multiple variables in a MySQL query when using Node.js?

I'm facing a challenge where I need to input student data into a table using the parent key as a foreign key. The parent information is included in the same JSON object, with an array of students inside it. My goal is to retrieve the parent Id from th ...

Unable to view the refreshed DOM within the specifications after it has been altered

For my current project, I am working on writing a functional spec that involves using Mocha/JSDOM and making assertions with 'chai'. The specific use case I am tackling is related to the function called updateContent: When this function is exec ...

What is the process for changing search box suggestions in Google and Wikipedia to radio buttons?

I recently integrated Google and Wikipedia search boxes into a webpage and I'm seeking a method to modify the suggestions based on the selected radio button. Specifically, the suggestion language should adjust according to the checked language option ...