Bizarre rotation in THREE.js around the X-axis

Here is the complete code snippet in jsFiddle:

http://jsfiddle.net/73v15kb6/1/

While rotating around the Y and Z axes yields expected results, there seems to be some additional effect applied by THREE.js when rotating around the X axis - which is not what I intended.

After looking at similar discussions, it appears that the issue lies within my rotateX function:

camera.position.y = y * cos - z * sin;
camera.position.z = y * sin + z * cos;
camera.lookAt(scene.position);

Upon initializing the 3D environment, I set the camera's initial position to maintain the desired viewpoint:

camera.position.x = -60;
camera.position.y = 30;
camera.position.z = 0;

If anyone has insights on where I may have gone wrong, I would greatly appreciate your help! Thank you!

Answer №1

I am completely new to THREE.js and this is my first time working with it, so my explanation might not be the best :(

One cool feature that stood out to me in THREE.js was the camera.lookAt method.

camera.lookAt(sphere.position);

Check out this modified sample on http://jsfiddle.net/73v15kb6/3/

Experiment with the animate function for each axis and try enabling the lookAt option. Spending some time playing around with it will help you grasp the concept :)

function animate() {
    requestAnimationFrame(animate);
    rotateX(5); // Try with Y & Z; (also with toggling lookAt())
}

The lookAt function can make the transformation functions feel strange because even if the camera transforms as expected, the rendering area remains the same.

Answer №2

Appreciation to Sen Jacob for repairing my fiddle!

I've discovered a solution to make the camera always focus on the center point (0,0,0) while ensuring that all rotations occur around the axis. I had to implement the following steps for each rotation:

For rotations in the Z and Y axes:

camera.up = new THREE.Vector3(0,1,0);
camera.lookAt(new THREE.Vector3(0,0,0));

For rotation in the X axis:

camera.up = new THREE.Vector3(1,0,0);
camera.lookAt(new THREE.Vector3(0,0,0));

Take note of the difference in the up vector between the X axis and the Y & Z axes.

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

jQuery - contrasting effects of the before() and after() functions

I'm working with a sortable list that is not using jQuery UI sortable, but instead allows sorting by clicking on buttons. Sorting to the right using after() works perfectly, however, sorting to the left with before() is causing issues. First, here&ap ...

Click on a link to use jQuery to sort the order of the <li>

Looking to rearrange list items by clicking either move up or move down. <div> <ul> <li>Item A <a class="moveUp">Move Up</a> <a class="moveDown">Move Down</a></li> <li>Item B <a class="m ...

Problem with Gulp-Watch Functionality on Windows 7 - Node.js, NPM, and Gulp All Up and Running Fine

As someone who is new to the world of Node.js, NPM, and other modern tools designed to improve productivity and workflow, I find myself faced with some challenges. Below are the specifications: Node version - v8.10.0 Gulp CLI version - 2.0.1 Gulp Loca ...

Navigating after sending AJAX GET request in Django

Just starting out with Django and AJAX (javascript), I've been able to successfully send arguments to a Django view. This view then renders an edit form for me. The issue arises when I try to redirect to the rendered form from the view. $.ajax({ ...

An issue arose following the utilization of "<style lang="scss" scoped>"

After using code from GitHub, I encountered an error. Any tips? I am new to JavaScript and Vue.js, and I'm constantly having issues with modules. I tried: npm uninstall webpack and then: npm install webpack@^4.0.0 --save-dev It's still not wo ...

There is an issue with the JSON format being received by fetch in JS when using json_encode in php

Recently, I encountered an issue with my JavaScript function that retrieves data from a PHP page. In the JS code block: fetch("print.php) .then(function (r) { return r.json() }) .then(function (values) { ...... ...... ...

When utilizing a clone in a Draggable and Droppable scenario, the data/attribute is obtained from the

Currently, I am struggling with retrieving data/attributes from the original item when using jQueryUI draggable/droppable to move rows between tables. I have set helper: 'clone' but am finding it difficult to access any data from the dragged item ...

Issue with Angular 2 data table search filter - The property 'first_name' is not found in the 'type {}'

Currently in my Angular-cli project, I have implemented the following code for a data table with a search filter. The tutorial referenced in the link below was used, leveraging loadsh. http://plnkr.co/edit/grhag1P85teN4ftggkms?p=preview You can find the p ...

In Javascript, merge two objects while maintaining their original references

Here's a basic illustration of my goal: data = {name: 'fred'}; newData = {}; newData.name = data.name; newData.name = 'ted'; console.log(data.name); // I expect this to be ted instead of fred I am wondering if it is feasible i ...

Tips for adjusting the size of various div elements by dragging in Ionic using AngularJS

I am developing an exciting ionic application that is designed to mimic the layout shown in the image below. The app will feature three dynamic div elements that adjust their size based on the movements of a draggable red circle on the screen. Check out t ...

What are the best ways to utilize getElementById in React?

As a beginner in React, I am attempting to create an auto text animation for my project. While I have successfully implemented it in VanillaJS, I am facing challenges with doing the same in React. import React, { Component } from 'react' class A ...

Navigate to a different subdomain and place a cookie on the newly redirected subdomain

The version of Express is 4.x NodeJS is running on version 12.x At a.example.com, I have a listener for the GET method that redirects to b.example.com and sets a cookie on b.example.com. app.get('/foo', (req, res) => { res.cookie(' ...

Is there a way to prevent a page from automatically redirecting to another page without relying on user action or using the StopPropagation and window.onbeforeunload event?

function confirmExit(e) { var f = FormChanges(); //checking if the page has been modified if (f.length > 0){ if (submitForm == false) { if(!e) var e = window.event; e.cancelBubble = true; e.returnValue = "You ...

The most recent update of Three.js, version 0.130.1, is experiencing issues with rendering shader

Previously, our application was using version 0.115 of Three.js and everything was functioning as expected. However, due to vulnerability concerns for versions below 0.125, we made the decision to upgrade to the latest version. Unfortunately, after the upg ...

Alignment of content layout across two wrapper elements

My goal is to achieve a specific layout where each pair of cards in a two-column layout is aligned based on the card with the largest content. Both cards share the same content layout and CSS. If you have any ideas or implementations that could help me ac ...

A step-by-step guide on incorporating HTML into a div element with jQuery

<div class="segment"> <div class="segment-inner"> <p>Content Goes Here...</p> </div> </div> <script type="text/javascript"> jQuery(document).ready(function () { jQuery('<i class="fa f ...

Certain images fail to load when the properties are altered

I am facing an issue where the images in my child components do not show up when props are changed, unless the page is manually refreshed. There are no 404 errors for the images, and even when I inspect the image element, I can see the correct image link b ...

Tips for managing, showcasing, and modifying checkbox controls within an AngularJS environment

I have successfully completed the saving part of the code. Below, I am demonstrating how I displayed the saved data and my attempts to edit the form upon clicking the edit button. Here is my AngularJS code: var module = angular.module('myApp&apos ...

Guide to implementing the onchange event in JavaScript for this specific scenario

How can the onchange event be utilized in this scenario using JavaScript? First, input data into the input with id="one". Subsequently, the data in the input with id="two" will be updated to match the data in the input with id="one". However, when the da ...