"Unleash the Surprise: Three.js Cube Spinning to an Unexpected

I'm working with a Three.js Cube that has six distinct sides.

I've created a random number generator that produces a number between 1-6. When a button is clicked, I want the cube to rotate to that specific side based on the generated number.

While the fiddle from Stallion (http://jsfiddle.net/majman/6vrH8/) looks great, it doesn't work for me because I'm unsure how much to rotate the cube to reach a particular side...

In my attempt, I set the rotations individually for each face:

   mesh.rotation.x =0;
     mesh.rotation.y =0;
    //face2
     mesh.rotation.x =0;
     mesh.rotation.y =1.571;
    //face3
     mesh.rotation.x =0;
     mesh.rotation.y =3.142;
     ...
     mesh.rotation.x =0;
     mesh.rotation.y =4.714;
     ...
     mesh.rotation.x =1.571;
     mesh.rotation.y =0;
     ...
     mesh.rotation.x =-1.571;
     mesh.rotation.y =0;

Then I tried to adjust the rotation by using += or -= with the above numbers. However, I noticed that after several iterations, the values of mesh.rotation.x/y become extremely high (over 100) which greatly prolongs the time required to reach the desired faces.

Any suggestions on how to address this issue?

Here's the updated fiddle: https://jsfiddle.net/crinca15/2o8n3xox/

Update: It seems there might be some confusion... Upon clicking the Start Button, the cube initiates its rotation. When the Stop Button is pressed, the cube should move to one of the sides numbered 1-6 (randomly). The positions for each face are listed above.

Now, I can utilize

(mesh.rotation.x == wanted.rotation.x; mesh.rotation.x -= 0.02)

The challenge lies in the fact that if the mesh.rotation value becomes excessively high, the cube takes a long time and requires numerous rotations to spin into position...

Answer №1

After reviewing the information on this page, it appears that your code is accurate. However, if the values are continuously increasing, it seems like you may be executing the code multiple times within a loop. Make sure to apply the rotations only once when necessary.

Additionally, keep in mind that your x and y values should stay within the range of 0 <= value < Math.PI * 2.

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

Searching for the index of a nested array in jQuery using JSON

I am currently working on developing an image uploader using Codeigniter3 along with jQuery and Ajax. Problem: I am facing difficulty in understanding how to locate the index of the array received from the ajax response shown below. Here is the data retu ...

Exploring the functionality of arrays in Jest's cli option --testPathIgnorePatterns

Looking to exclude multiple folders, one in the src folder and another in node_modules, when using the --testPathIgnorePatterns option. Does anyone have an example of how to use this pattern effectively? I am unable to configure an array in the package.js ...

Exploring Unicode in JavaScript to iterate through emojis with different skin tones

Currently, I am facing an issue where Javascript splits emojis with different skin colors into multiple characters instead of treating them as one. Emojis with a yellow skin color work fine and give me the desired results. For example: let emojis = [..." ...

Reordering database information using javascript and php

Is the title completely accurate? Here's my issue: I operate a website where users can upload images. I am looking to allow users to organize their uploaded images in a specific order and then preview them exactly as they arranged them. The organizi ...

Does turning off javascript in a browser impact ajax requests and javascript functions?

My mind is troubled I've been thinking of options like turning off JavaScript in the browser. If I do that, then AJAX and JavaScript functions won't work, right? If so, is there a solution? ...

Getting a list of connected users on a PeerJS server using Express is simple and straightforward. Follow these steps to

Trying to incorporate PeerJS, a webRTC library, into a game and utilizing their server for user discovery has proven challenging. The goal is to manage a list of connected users, but grappling with the PeerJS server has been difficult. The documentation s ...

Seeking advice on Javascript prototypes - how can I execute a function within a prototype without using "this"?

let Human = function (userName, userAge) { this.userName = userName; this.userAge = userAge; }; Human.prototype.sampleProto = function () { console.log(this.userName + " == " + this.userAge); ...

Numerous toggles paired with various forms and links

I am currently facing an issue with toggling between forms on my website. Although the toggle functionality works fine, I believe I may have structured the steps in the wrong order. My ideal scenario is to display only the 'generating customer calcul ...

The JSON.stringify function encounters difficulties when trying to format an object into

Why does JSON.stringify refuse to format objects and keep everything on a single line? <!DOCTYPE html> <html> <head> <script> var obj = { "a" : "1547645674576457645", "b" : "2790780987908790879087908790879087098", "c" ...

Trouble with axios post request, data not reaching Django view

I am attempting my initial post request utilizing axios to a Django view that accepts both POST and GET methods. However, I am receiving an empty dictionary from the request.POST: <QueryDict: {}> [13/Aug/2022 16:44:00] "POST /users/1142606705924 ...

Retrieve the file name from the URL while disregarding the query string

Looking for a way to extract the test.jsp from this URL: http://www.xyz.com/a/test.jsp?a=b&c=d Can anyone provide guidance on how to accomplish this task? ...

Upgrade your React component by replacing componentWillReceiveProps with getDerivedStateFromProps

It's clear that componentWillReceiveProps has been deprecated and we are instructed to replace it with getDerivedStateFromProps. However, they don't seem to serve the same purpose at all. Moreover, getDerived... cannot access or setsetate. After ...

What is the process to switch an Editbox from read mode to edit mode in Xpage?

Recently, I tried setting the property Read-Only to true in Xpage for a specific field. However, when attempting to change its mode to edit using client-side JavaScript, I encountered difficulties. Below is the code snippet I used: document.getElementByI ...

Transform a list of H1..6 into a hierarchical structure

I have a task to convert H1 to H6 tags from a markdown file into a JavaScript hierarchy for use in a Table of Contents. The current list is generated by AstroJS and follows this format [{depth: 1, text: 'I am a H1'}, {depth: 2: 'I am a H2}] ...

Automatically fill in a custom entity using JavaScript even when the GUID of the parent entity is unknown within Dynamics 365

Recently delving into client-side scripting, I have been tirelessly searching through Google and various communities for answers without success. Allow me to present my issue for a clearer understanding of the predicament. I have established two custom en ...

Troubleshoot: Why is my AngularJS bootstrap.ui modal failing to

I am experiencing an issue with displaying a modal dialog using AngularJS bootstrap.ui. After calling $modal.open(...), the screen grays out, the HTML from my templateUrl is fetched from the server, but the modal does not appear. When I click on the gray a ...

Overcoming Tabindex Issues with jQuery

Currently, I am working on managing the user interaction with my forms using the tabindex property of form elements. I have specified the tabindex for all elements and I want to ensure that this value is respected and utilized correctly. While developing ...

Modal failing to update with latest information

My webpage dynamically loads data from a database and presents it in divs, each with a "View" button that triggers the method onclick="getdetails(this)". This method successfully creates a modal with the corresponding data. function getdetails(par) { ...

Results that stand out: Mongoose's uniqueness

I am in search of a solution to create a query that can fetch results from my mongo database while excluding any duplicate field values. Here's the scenario: I am retrieving numerous results from the Spotify API and saving them in my database. Howeve ...

Fade in/out overlay effect when clicking on a content block

I've hit a roadblock while trying to implement overlay fading in and out to cover a block created by some JavaScript code. Here is a link to the project. When you click on any of the continents, a series of blocks with country flags will appear. You& ...