Aframe Descend Rotation

I am currently working on a project in Aframe and I'm looking to implement a control/event that enables an entity to rotate downward.

While attempting to create a new animation and add it as a child object to the entity, I have achieved successful results in turning left and right along the x-axis. However, I am facing difficulties in getting it to work for rotating the box downward.

In certain scenarios, such as {z: 90, x:90, y: 0}, there seems to be no effective way to rotate the face pointing towards the camera downwards, regardless of changing the values for y or z axes.

To illustrate the issue, I have prepared a minimal plunker: https://plnkr.co/edit/B6apT3?p=preview

Edit: Including the code snippet from the original plunker.

For achieving the desired downward rotation, I am implementing the following logic:

if(rotateOnY){
  z -= 90;
} else {
  y -= 90;
}
changePosition(x, y, z);

And the function used to change the position is as follows:

function changePosition(x, y, z){
  let animation = document.createElement('a-animation');
  animation.setAttribute("attribute","rotation");
  animation.setAttribute("dur", 300);
  animation.setAttribute("repeat", "0");
  animation.setAttribute("to",`${z} ${x} ${y}`);
  document.getElementById('box').appendChild(animation);
  document.getElementById('position-text').setAttribute('text', `value: Position(x, y, z): ${x} ${y} ${z}`)
}

Although there was a helpful response, the rotations using Tween are not functioning perfectly.

We can consider modifying the logic as shown below, but the mathematical computations do not align correctly, as evidenced in http://plnkr.co/edit/hr2l83?p=preview

const originalEuler = new THREE.Euler(x, y, z);
const originalQuaternion = new THREE.Quaternion();
originalQuaternion.setFromEuler(originalEuler);

var tarQ = originalQuaternion.premultiply(q);

var euler = new THREE.Euler();
euler.setFromQuaternion(tarQ);
let rot = { // rot not pos
  x: THREE.Math.radToDeg(euler._x),
  y: THREE.Math.radToDeg(euler._y),
  z: THREE.Math.radToDeg(euler._z)
};
// update to neares 45
for(let axis of Object.keys(rot)){
  rot[axis] = nearest45(rot[axis]);
}
// update global x, y, z;
x = rot.x; y = rot.y; z = rot.z;
changePosition(rot.x, rot.y, rot.z);

Answer №1

UPDATE: I want to clarify that my initial response, while accurate, may have lacked the level of detail needed to fully assist you. After delving deeper into the subject with some research, I stumbled upon a simple and effective method for applying world rotation without requiring advanced mathematical knowledge. Here's a summary:

var q = new THREE.Quaternion(); // create this once and reuse it
q.setFromAxisAngle( dirVec, Math.PI/2 );// defines the desired world rotation axis, 90 degrees in radians
var tarQ = box.quaternion.premultiply( q );

Take a look at the updated plunker

(Please note that for transitioning smoothly between rotations, I chose to clone the original quaternion, perform the operation on it, and then set it as the target for the animation.)


Welcome to the complex realm of 3D rotation.

In terms of 3D mathematics, the goal is to rotate your object in world space rather than local/mesh space. The latter can lead to various issues, including what seems to be the problem you are facing - gimbal lock.

Converting between these spaces requires some matrix calculations and may take some time to grasp, but fortunately, Three.js offers useful helper functions. For more information on these conversions, refer to this informative post: Set an object's absolute rotation around the world axis

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

Trouble arises when trying to load angular ui-bootstrap using require.js unless I change the name to ui.bootstrap

Code Overview: main.js: require.config({ paths: { 'uiBootstrap': '../lib/bootstrap/angular-bootstrap/ui-bootstrap-tpls.min' }, shim: {***}, priority: ['angular'], deps: ...

Tips on sending arguments to functions associated with ...mapActions...()?

After analyzing the passage below export default { methods: { ...mapActions(["updateData", "resetData"]); } } I wanted to include a parameter in the called functions. Unsure of the proper way to do this while still using the ...mapAction() call, ...

Dealing with null exceptions in Angular 4: Best practices

Hi there, I am encountering an issue with binding my model data to HTML fields where when I try to edit the data it returns an error saying "cannot read value of null". How can I resolve this? Here is the HTML code snippet: <div class="form-group"> ...

What is the method for utilizing the onmouseover function to emphasize a specific part of an SVG graphic?

I am attempting to create an interactive SVG map of Europe. Each country is represented by a path element, and I want the opacity of a country to change to 0.5 when it is hovered over. Despite trying to define a JavaScript function for this purpose, noth ...

TABULAOTR, The complete table calculation is failing to be retrieved

Apologies for any language mistakes, as I am Russian :)I am using Tabulator but facing an issue where the table footer is not being printed. I am also unable to retrieve data from the footer. The footer simply doesn't show up and I'm unsure of wh ...

Invalid controller function path specified in Laravel framework for AJAX request

I am struggling to find the correct ajax URL as the one I am currently using is not working. The console shows the following error message: GET XHR localhost:8000/Controller/getUnitSellingPrice [HTTP/1.0 404 Not Found 203ms] In the create.blade View: ...

Moving through content on a single page

import React, { Component, useState } from "react"; import { Content, List, ListItem, Left, Right, Icon, Container, Header, Body, Button, Title, } from "native-base"; //Chapter One expor ...

Activating a function that requires locating a dynamically loaded element on the webpage using AJAX

I have a unique page on my website that allows users to make edits. Whenever they click on an item, the edit form is seamlessly loaded into a specialized section of the page using AJAX. Depending on the chosen item, the form fields are already prefilled w ...

exploring the digital archives for a specific song file on the computer

Currently in the process of building a website, and I am looking to incorporate a search and filter feature for all music content so that users can play it on my player. Wondering if this is possible? Seeking some assistance and creative ideas to make thi ...

Exploring the power of media query breakpoints within Chakra UI's global styles

Trying to incorporate a media query breakpoint in Chakra UI global styles has presented some challenges. An attempt was made using template literals for property names, but it was discovered that this approach is not supported: import { ChakraProvider, e ...

shallow rendering does not recognize this.props as a function

I'm currently facing an issue while trying to test my (legacy) component using jest/enzyme. Here is a snippet of the component: export default class MyComponent extends Component { constructor( props ) { super( props ); this.handl ...

Validating alpha-numeric passwords with JavaScript

I created a JavaScript function to validate if a password is alphanumeric. However, I am facing an issue where the alert message is not being displayed when the password is not alphanumeric. Below is my code snippet: if (!input_string.match(/^[0-9a-z]+$ ...

Switch the array's value if the key is a match?

I'm currently facing an issue where my code does not push the object when the key matches. How can I update the value of the key instead when there is a match? this.state.data.concat(items).filter(function (a) { return !this[a.key] && (th ...

Adjust the size of a div by clicking a button using Javascript

<script type="text/javascript"> var btn = document.getElementById("btn"); var panel = document.getElementById("panel"); var btn2 = document.getElementById("btn2"); function expandPanel() { btn.style.display="none"; panel.style="overflow:hidd ...

What is the most effective way to reset the state array of objects in React JS?

I'm facing a challenge in resetting an array of objects without having to rewrite the initial state value again. Initial state const [state, setState] = useState([ {name: 'abc', age: 24}, {name: 'xyz', age: 20} ]) Is there a metho ...

What is the best way to differentiate between a JSON object and a Waterline model instance?

Note: I have posted an issue regarding this on the Waterline repo, but unfortunately, I have not received a simpler solution than my current workaround. Within my User model, along with default attributes such as createdDate and modifiedDate, I also have ...

How to extract only the truthy keys from an object using Angular.js

I am looking to retrieve only the keys with a true value in the data object and display them in the console with the specified format: Object { Agent=true, Analytics / Business Intelligence=true, Architecture / Interior Design=false } The catego ...

Eliminate specific content from within a div tag

Looking for a solution with the following HTML. <div id="textDiv"> <p> MonkeyDonkey is not a bird. Monkey is an animal. </p> </div> In this case, I am trying to delete the word "Donkey". I attempted the code below but it did no ...

Error: Unable to modify the value of a protected property '0' in the object 'Array'

I am facing a challenging issue with implementing a Material UI slider in conjunction with Redux. Below is the code for the slider component: import { Slider } from '@material-ui/core' const RangeSlider = ({handleRange, range}) => { ...

Cross-origin resource sharing (CORS) allows for the secure transfer of data across different

Currently, I am faced with a challenge in making an XmlHTTPRequest POST request from a page loaded via HTTPS to a different domain using an HTTP URL. The HTTP server in question is local and does not support HTTPS due to being within a home setup (like a s ...