Manipulating the orientation, position, and size of an object using direction and the camera's view in Three.js

I'm currently developing a simple editor for Three.js. My goal is to enable the rotation, movement, and scaling of objects with ease.

While utilizing the basic functions gets the job done, I find it slightly perplexing at times.

cube.rotation.z = Math.PI * val;
cube.rotation.x = Math.PI * val;
cube.position.z = val * -1;
cube.scale.y = val;

My aim is to rotate the object in a direction rather than along an axis, considering the angle of the camera's view.

You can see this concept demonstrated in action through a codepen: https://codepen.io/arpo/pen/LxyoRJ

The functionality works smoothly when all other inputs remain untouched. When you adjust the "Rotate Left Right" range, the cube rotates on the z-axis. However, if you move the "Rotate Back Front" range all the way to the right and then manipulate "Rotate Left Right" again, the rotation seems to happen around the Y-axis. This behavior applies to the other inputs as well.

Although I comprehend why this occurs, I am uncertain how to achieve the desired effect. Additionally, I would like this interaction to be based on the camera's perspective. For example, if you shift the camera to the left and then modify the "Move Back Forward" range, the cube should either move away or closer to the user.

Essentially, I wish for the cube to swing left or right from the user's viewpoint when using the rotate function and to be pushed further away if adjusting the move feature.

Answer №1

To fully grasp the answer to your inquiry, it is essential to have a fundamental understanding of how 3D coordinate systems operate. There exist three key coordinate systems that you should acquaint yourself with:

  1. Local: Every object in the scene possesses its distinct coordinate system. This comprises a trio of invisible axes positioned at the object’s center, steadfastly indicating specific areas of the object despite any transformations applied.
  2. Global: Commonly referred to as world coordinates, this universal coordinate system denotes the positioning of objects within the overall scene. The X and Z global coordinate axes, featured as red lines in your demo, play a pivotal role in this system.
  3. Camera: Equipped with its unique coordinate system, the camera introduces another layer of complexity. These discrete set of three axes point towards designated directions—leftwards, upwards, and towards the scene.

The alignment among these three coordinate systems is not static. While they may begin as aligned by default, rotations carried out on objects or the camera can disrupt this alignment.

Rotating an object via its rotation property entails spinning it around its local axes. Initially, the cube’s Z axis aligns closely with the world’s Z axis, akin to a line extending from its front face mirroring the global red line. Following a 90-degree rotation 'back to front', the cube repositions its Z axis downward.

Subsequently, the 'left to right' rotation—an actual manipulation of the Z axis—shifts the cube revolving around this now downward-facing Z axis.

If intending to rotate the cube around an axis coinciding with the camera's coordinate system, multiple rotations and translations must be implemented.

I strongly recommend delving into the intricacies of coordinate systems, model transformation, view transformation, and other core facets of 3D visualization. Without a solid foundation in these principles, navigating through such concepts can prove quite bewildering.

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

Unexpected behavior found in certain parts of jquery due to the implementation of Ajax

Every time I try to use the ajax code on the same page or an external .js file, it seems to cause issues and prevents other parts of the code (jquery) from functioning properly. The ajax should only trigger when clicking on a specific li element with the c ...

The proper way to cancel useEffect's Async in TypeScript

I'm facing an issue with this straightforward example: useEffect(() => { axios.get(...).then(...).catch(...) }, [props.foo]) warning: can't perform a react state update on an unmounted component After some investigation, I found this ...

Assign false to all properties in the nested object with the exception of one

In order to manage the open/close state of my panel, I am using setState similar to the method described in this post. My goal is to only allow one panel to be open at a time, meaning there will be only one true value in the state. Here is the snippet of ...

How to adjust parent height for Table component in Material UI?

I am looking to make the height of the table fit the available space within its parent. Specifically, I would like: The footer to always be anchored at the bottom of the parent container. If the table has only a few rows, I want the footer to stay at ...

Nuxt: Encountered an unexpected < symbol

https://i.sstatic.net/MbM9f.png I've been working on a nuxt project where I'm currently in the process of creating a map component using Google Maps with the help of the plugin https://www.npmjs.com/package/vue2-google-maps. After installing the ...

Invoke a function on React whenever the context is updated

Within my functional component, I have a scenario where it returns another function: function Lobby() { const user_context = useContext(UserContext) return ( <div>{renderData(user_context.state.data)}</div> ) } export default L ...

Safari experiences occasional failures with pre-signed post uploads to S3 when using multipart/form-data for file uploads

Lately, I've encountered issues with pre-signed post uploads to S3 that seem to be unique to Mobile Safari browsers. Interestingly, the error has also shown up occasionally on Desktop Safari. Whenever this error occurs, it triggers a response from S3 ...

Style binding for background image can utilize computed properties or data for dynamic rendering

In my code, I am trying to pass an object that contains a string path for its background image. I have experimented with using data and computed properties, but so far I haven't had any luck getting them to work within the :style binding. However, if ...

JavaScript event/Rails app encounters surprising outcome

I have encountered a strange bug in my JavaScript code. When I translate the page to another language by clicking on "English | Русский" using simple I18n translation, my menu buttons stop working until I reload the page. I suspect that the issue ...

Implementing Nodejs middleware across multiple files

Hello, I am currently facing an issue with my code. I have two files - server.js and index.js. In server.js, I am trying to call index.js from within app.get function, but it seems like it's not entering the app.get function block. Can someone please ...

Dealing with a throw er; uncaught 'err' event while configuring a server with nodemon

I am currently in the process of setting up my local server using node.js and nodemon. Initially, everything runs smoothly on localhost, but as soon as I refresh the page or navigate to another page, the server crashes with an 'unhandled error event&a ...

Are there any other benefits to utilizing the Decorator pattern besides enhancing dynamic behavior?

Currently, I am diving into the intricacies of the Decorator design pattern and a particular thought has been nagging at me. What if we simply had one base class with boolean values representing its features? Consider this example: Imagine a textview tha ...

Storing the JSON response from axios into a variable: a beginner's guide

Exploring the concept of saving the JSON data returned from axios into a variable has left me puzzled. Currently, I am only able to visualize it when I utilize console.log(response.data) with the code snippet below: function status() { const url = " ...

Conceal the <p> tag if the content inside is either "0" or blank

I'm currently working on a basic calculator project and I've hit a roadblock. I need to hide certain elements based on conditions. The code snippet with explanations is provided below. function calculateArea() { var length = document.getElem ...

Error in JScript encountered during the downgrade to .NET Framework 2.0

Working on a Web Application project has brought about some challenges for me. Originally created in .NET 3.5, I recently found out that it has to be converted to .NET 2.0 (sigh). This transition not only caused issues with the LINQ functionalities but als ...

Troubleshooting a Problem with AngularJS $.ajax

Oops! Looks like there's an issue with the XMLHttpRequest. The URL is returning a preflight error with HTTP status code 404. I encountered this error message. Any thoughts on how to resolve it? var settings = { "async": true, "crossDomain": ...

Looking for guidance on sending data from a JS file to HTML using Nodejs? Seeking advice on various modules to achieve this task effectively? Let's

Looking for advice on the most effective method to transfer data from a JS file (retrieved from an sqlite db) to an HTML file in order to showcase it in a searchable table. My platform of choice is NodeJS. As a beginner, I am willing to put in extra time a ...

Calculating variables in JavaScript

Explanation from Mozilla Documentation: console.log((function(...args) {}).length); // The result is 0 because the rest parameter is not counted console.log((function(a, b = 1, c) {}).length); // The result is 1 because only parameters before th ...

extracting PHP values using AJAX

My goal is to fetch two values separately from PHP via AJAX and use them to auto-fill different input tags. However, when I console.log(data), I receive all the data at once instead of individually. I need to be able to retrieve them separately in order to ...

What is the best way to access a JSON Array in php without using any specified keys?

I'm dealing with a JSON object created in JavaScript and passed to PHP via AJAX. The issue I'm facing is that I can't figure out how to assign keys to each JSON object within the JSON array. Here's an example of how the JSON array looks ...