Guidelines for attaining seamless motion animation utilizing devicemotion rotationRate information

I am utilizing the rotationRate data obtained from the devicemotion eventListener to manipulate a three.js scene by tilting. The scene does respond to the data accurately in terms of angle adjustments, but it produces an unsmooth motion effect. Is there a technique to achieve a smoother motion effect?

Currently, I am assigning appropriate angles for the tilt of the three.js camera by mapping the event.beta and event.gamma data. However, since the rotation data is directly used for angle updates, the outcome lacks smoothness. Even after attempting to decrease the angle value, it failed to completely smooth out the motion.

function handleDeviceMotion(event) {
    rotateX = Math.clip(parseInt((event.rotationRate.beta*10).toFixed(0)), -rotationContain, rotationContain);
    rotateY = Math.clip(parseInt((event.rotationRate.alpha*10).toFixed(0)), -rotationContain, rotationContain);
    rotateXMapped = rotateX.map(-rotationContain, rotationContain, -paneAngleContain, paneAngleContain);
    rotateYMapped = rotateY.map(-rotationContain, rotationContain, -paneAngleContain, paneAngleContain);
    rotateXMapped += 0.3;
    rotateYMapped += 0.3;
}

The variables rotateXMap and rotateYMap are subsequently utilized in the orbitControls.js library provided by three.js. Is there any possible way to introduce a delay between the current angle and the updated one? As someone who isn't well-versed in mathematics, I have been struggling with this issue for quite some time.

Any assistance would be greatly appreciated.

Answer №1

It appears that the issue you are encountering is related to the jerky animation caused by utilizing the devicemotion event, which primarily detects changes in acceleration, rather than the recommended deviceorientation event designed for capturing orientation changes. In order to ensure smoother animation, it is advised to switch to the deviceorientation event used by Three.js in its DeviceOrientation controls. You can see this functionality in action at:

As a precautionary measure, have you attempted running your scene with a simplified setup? This may help identify if low frame rate issues originate from other performance limitations.

Answer №2

It turns out I discovered the solution to my problem - implementing the concept of "lerp interpolation" to smoothen the motion of the tilt data. This involves utilizing a formula that takes into account the future and current points, subsequently averaging them, resulting in a much smoother animation.

To gain a better understanding of lerp interpolation, I came across an impressive demonstration on CodePen. You can check it out here.

Now, let me showcase how I've integrated this updated code:

function handleDeviceMotion(event) {
    // Implementing lerp interpolation to map rotationRate data to scene tilt angles for a visually pleasing effect
    rotateX = Math.clip(parseInt((event.rotationRate.beta*10).toFixed(0)), -rotationContain, rotationContain);
    rotateY = Math.clip(parseInt((event.rotationRate.alpha*10).toFixed(0)), -rotationContain, rotationContain);
    rotateXMapped = rotateX.map(-rotationContain, rotationContain, -paneAngleContain, paneAngleContain);
    rotateYMapped = rotateY.map(-rotationContain, rotationContain, -paneAngleContain, paneAngleContain);
    rotateXLerp = lerp(rotateXLerp, rotateXMapped, 0.15); // 0.1 = default
    rotateYLerp = lerp(rotateYLerp, rotateYMapped, 0.15); // 0.1 = default
}

function lerp(start, end, amt) {
    return (1-amt)*start+amt*end;
}

I hope this solution proves beneficial to anyone facing a similar challenge in the future!

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

Verify the presence of a GET parameter in the URL

Working on a simple log in form for my website using Jade and ExpressJS. Everything is functioning correctly, except for one issue - handling incorrect log in details. When users input wrong information, they are redirected back to the log in page with a p ...

Error: The $filter function in AngularJS is not recognized

I have been attempting to inject a filter into my controller and utilize it in the following manner: angular .module('graduateCalculator', []) .filter('slug', function() { return function(input) { ...

Not enough test coverage with Jest

Here is the code snippet I am working with: <ReturnToLastSearch href={'/listings'} onClick={(e): void => { e.preventDefault(); router.back(); }} /> The function ReturnToLastSearch( ...

In JavaScript, unchecking a radio button results in all options becoming uncheckable

I have a pure CSS accordion and I want to enhance it with some JavaScript functionality for users who have JavaScript enabled. The CSS accordion currently utilizes the :checked pseudo-class. The new feature I am looking to add is: if a button that is alre ...

The initial return value of $(document).height may be inaccurate, but is accurate upon recalculation

I am working on implementing a pop-up screen and I would like to darken the background when it opens. Below is the Javascript code: $(document).on('click', '.item', function(){ $("#popUp").css("display" , "block"); ...

How can we efficiently loop through all the icons in React Material-UI?

I am looking to iterate over all the icons from @material-ui/icons in a React application. If I want to import a single icon, I can do so like this import IconNameIcon from '@material-ui/icons/IconName' and then use it in my component like th ...

What is the process for converting a string literal into raw JSON and then storing it?

When trying to edit object values using a text input element and JSON.stringify(txt, null, 2), everything seems fine initially. However, after submitting the input, I end up with unwanted characters like "/n" and "\" within the string literal. Despite ...

Is it necessary to specify a data type for the response when making an AJAX POST request?

After carefully reviewing my code, I have ensured that all the variables in my JavaScript match and are properly set up. Additionally, I have confirmed that all the variables in my data array for my AJAX request correspond correctly to my $_POST['var& ...

Windows location does not change after an XMLHttpRequest is made

Here is my code that uses XMLHttpRequest: function SignUp() { signUpConnection = new XMLHttpRequest(); signUpConnection.onreadystatechange = processRegistration; signUpConnection.open('GET', 'index.php?registrarse=&username= ...

Verify whether an email is already registered in firestore authentication during the signup process using Angular

When a user signs up for our app, I want them to receive immediate feedback on whether the email they are attempting to sign up with already exists. Currently, the user has to submit the form before being notified if the email is taken or not. This desire ...

Start a timer in CodeIgniter when a button is clicked and show the time elapsed

Just a heads up: I am currently in the learning process. While I have some experience with PHP, my knowledge of Java is very limited or non-existent. In the past, I've received negative feedback due to this lack of experience. So, here I am giving it ...

Accessing the current clicked item in $scope using an Angular Directive within ng-repeat

I have set up a custom directive called calendar which includes a date picker created in JQuery. To associate the ng-model with the date picker, I am using the following code successfully: var clickedID = "#" + $scope.indexid + "_datePicker"; $(clickedID ...

The ajax client is encountering an undefined response, but it is correctly processed when accessed through the

I am in the process of setting up an ajax client with a dummy server for testing purposes. I have successfully resolved the cors issue, but now I am facing a problem where the response from the ajax client is showing as undefined. Interestingly, when I acc ...

Is a fresh connection established by the MongoDB Node driver for each query?

Review the following code: const mongodb = require('mongodb'); const express = require('express'); const app = express(); let db; const options = {}; mongodb.MongoClient.connect('mongodb://localhost:27017/test', options, fu ...

Why am I encountering http://localhost:3000/api/auth/error?error=AccessDenied when implementing Google signin in my Next.js application?

Can someone please assist me in resolving an issue I am facing with my NextJs application using Google signin? Whenever I try to sign in, I get the error message "http://localhost:3000/api/auth/error?error=AccessDenied". Why is this happening? Here is a ...

Transform the assurance into a JSON entity

Currently facing an issue with converting the promise returned by the service to the controller. My goal is to generate an array of JSON objects using the data within the promise. In the controller, this is what I'm receiving: https://i.stack.imgur.co ...

utilizing javascript once form elements are dynamically inserted

While dynamically constructing form elements, I encountered an issue with generating unique IDs when the form is submitted. Everything works fine except for the JavaScript function responsible for populating the year in a dropdown selection. The issue ari ...

Storing data from a form by utilizing AJAX with PHP

Is there a way to save the form data either in a file or a local database using AJAX, while still sending the data through the form action to an external database? If you want to view the source code for my form, you can find it here: http://jsbin.com/ojU ...

Dynamic TypeScript class constructor argument typing determined by user input

I am working on creating a dynamic class that can adapt its argument properties based on a certain value. To illustrate this concept, let's consider a simple example: Imagine I have a class called Customizer, and depending on the value of the mode pr ...

Unexpected JavaScript behavior triggers Safari's crash on iOS platforms

In my Sencha Touch application, I am implementing a feature where users can download 5000 records in JSON format and display them in an Ext.List control. The downloading of records works smoothly using JSON.parse() and storing the data locally. However, u ...