Is there a way to restore the face's original orientation after it has been rotated

Just to note, I am aware that this question has been asked before. However, the previous answers did not address my specific situation and requirements.

Currently, I am developing a Rubik's cube using three.js. To achieve lifelike rotations, I am rotating the cube around a specific point. Here is the function I am utilizing for this purpose:

THREE.Object3D.prototype.rotateAroundWorldAxis = function () {

    // Rotates the object around a specified axis in the world space (the axis goes through the point)
    // Assumption is that the axis is normalized
    // Also assumes that the object does not have a rotated parent

    var q = new THREE.Quaternion();

    return function rotateAroundWorldAxis(point, axis, angle) {

        q.setFromAxisAngle(axis, angle);

        this.applyQuaternion(q);

        this.position.sub(point);
        this.position.applyQuaternion(q);
        this.position.add(point);

        return this;

    }

}();

Additionally, I have developed a manual function to calculate the face normals based on 3 points. I am aware that three.js already provides a similar function, but this is part of my debugging process. The function works correctly, however, when I rotate a face of the Rubik's cube using a function, neither the built-in normals nor my function give the correct direction. They simply return the original facing direction. Below is the function for calculating the normals:

function computeNormal(p,face) {
    let a = p.geometry.vertices[face.a]
    let b = p.geometry.vertices[face.b]
    let c = p.geometry.vertices[face.c]
    let d = new THREE.Vector3(0,0,0)
    d.crossVectors(b.sub(a),c.sub(a))
    console.log(a, b, c, d)
    return d.normalize()
}

Any assistance on this matter would be greatly appreciated. If you require any additional segments of my code, I am more than willing to provide them.

Answer №1

As usual, I tend to figure it out only after everything is already published! It dawned on me that we can determine the rotation of the section containing the face and add that rotation to the faces. Here's the revised function:

function calculateNormal(point, face) {
    let normal = face.normal
    let rotation = point.rotation
    normal.applyEuler(rotation)
    console.log(normal)
    return normal
}

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

Determine the y-coordinate of terrain in Three.js

I have acquired a Collada model of terrain and now wish to incorporate other models onto this terrain. To achieve this, I believe I need to retrieve the height coordinate (y) of the terrain based on specific x and z coordinates. Can someone please guide ...

Issues with Node JS app's handling of php mailer code

I've made a basic website using the Node JS framework and included a php mailer for handling the contact form. Unfortunately, I'm facing issues getting it to function properly. Could it be possible that there is an underlying problem with Node JS ...

"Looking to trigger a server click using JQuery or Javascript in your code? Here's how you can

I am facing an issue with triggering a Server-Side OnClick event on an ASP Server Button within a User Control using JavaScript or JQuery. The current methods I have tried do not produce the desired result as they do not actually simulate a user clicking t ...

How to style text in CSS with a numbered underline beneath

Is there a way to apply underlining to text and include a small number underneath the underline in a similar style shown in this image, by utilizing css, html, or javascript? ...

Jquery Plugin for Click Selection Activation

After receiving no response to my previous inquiry, I decided to proceed with this particular plugin setup. (function ( $ ) { $.fn.myPlugin = function (options) { options = $.extend( {}, $.fn.myPlugin.defaults, options ); return this.each(function (i ...

Angular JS has a unique feature of a scrollable drop-up menu that allows

https://i.sstatic.net/MOUqO.pngHere is the code snippet for my Dropup component: <div class="dropup"> <button class="btn btn-primary btn-raised dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false> ...

Error encountered: The initMap function from the React Google Maps API is not recognized. No relevant

Encountering an issue where initMap is not recognized as a function. I attempted to avoid utilizing any additional packages for asynchronously loading the script or Google Maps API. My initial approach was to simply console log initMap to track when the sc ...

What is the best way to include an Interval in a button element?

I am currently working on developing an application that will automatically take a picture every minute using the camera. I want to implement a button tag with an interval so that when the application is running, it will capture an image every minute. i ...

Developing a unique bundle with tailored elements

I am interested in developing a custom Material UI component and making it available as a standalone package within my company's private NPM repository. Specifically, I have customized the Date Picker to create a Date Range Picker since Material UI d ...

Use regular expressions to exclude occurrences of the character 'n' from your text

Looking for a regular expression to validate input, specifically filtering out all special characters except "underscore". All characters within the range [a-zA-Z0-9\underscore] are permitted and can appear multiple times. However, my expression shoul ...

Creating a custom dialog box using JavaScript

How can I create a customized dialog box in the center without displaying "the host name says..." using CSS? function myFunction() { alert("Record Save"); } Thank you in advance! ...

Three.js launching projectiles

As a newcomer to three.js, I have been tasked with creating a basic FPS game. However, I am encountering several issues with the gun and bullets in the game. Whenever I press the "spacebar" key to shoot, the bullets initially go in the correct direction bu ...

Exploring the integration of Django Rest Framework with Angular

I am just starting out with Django Rest Framework (DRF) and AngularJs. I am trying to figure out the best way to integrate these two technologies. Combining DRF and AngularJs in one project (Most of the tutorials recommend this) Using DRF as the backend ...

What is the issue with this asynchronous function?

async getListOfFiles(){ if(this.service.wd == '') { await getBasic((this.service.wd)); } else { await getBasic(('/'+this.service.wd)); } this.files = await JSON.parse(localStorage.getItem('FILENAMES')); var ...

During the rendering process, the property "instance" was attempted to be accessed but is not defined

I am having trouble creating a Contact Us page using v-model. My code keeps throwing these errors: Property "inputted_name" was accessed during render but is not defined on instance Property "inputted_email" was accessed during render but is not defined o ...

Every time I employ window.location, the Iframe becomes nested

I am experiencing an issue with my HTML structure: <html> <body> This is the container of the iframe <iframe src="/foo.html"> </iframe> </body> </html> (/foo.html) <html> <script type="text/javascript"> $( ...

Ways to retrieve data from a URL and pass it to JavaScript code

I am currently experiencing difficulties in obtaining the values from an object requested through a link on my website. The issue arises from the fact that I have created a PHP method to retrieve data from the database for the values of a particular objec ...

Is it possible to encrypt data using a private key in Angular and then decrypt it using a public key in PHP using RSA encryption?

Attempting to Encrypt data using the RSA Public Key in Angular and Decrypt it with the public key in PHP. Encryption is done with the JsEncrypt library in Angular, while decryption takes place in PHP. :openssl_public_decrypt($signature, $decrypted, $public ...

The function is missing a closing return statement and the return type does not specify 'undefined'

It seems like the function lacks an ending return statement and the return type does not include 'undefined'. In a recent refactoring of the async await function called getMarkets, I noticed that I had mistakenly set the return type as Promise: ...

Generating JSON Data with the Power of JavaScript and JQuery

Looking to dynamically generate a JSON Object with the specified structure: { "deleteId":[1,2,3], "pointId":[1,2,3], "update":[ { "what":"mission", "id":1, "value":"adsda" }, { ...