Adding Damping (Inertia) to Panorama Rotation for Smoother Movements

I understand that OrbitControls.js has a damping feature, which adds a smooth dragging of panorama, also known as easing. I am looking to achieve the same functionality without relying on this library. This is because I want to streamline my code and have more precise control over mouse or tap events.

I have created a Plunker to demonstrate the initial project I use for my panorama view.

https://plnkr.co/edit/eX2dwgbrfNoX9RwWaPaH?p=preview

In this demo, mouse coordinates are converted to latitude/longitude to adjust the camera position. It's a basic example showcasing a minimal panorama from the three.js site.

While experimenting with damping in OrbitControls.js (view this line), I couldn't quite replicate the same smooth behavior - the interaction caused the panorama to jump:

if ( scope.enableDamping === true ) {
    sphericalDelta.theta *= ( 1 - scope.dampingFactor );
    sphericalDelta.phi *= ( 1 - scope.dampingFactor );
    panOffset.multiplyScalar( 1 - scope.dampingFactor );
}

I'm struggling to apply this damping effect to my Plunker example.

Can someone provide guidance on how to implement damping in my Plunker project?

Update:

I made some progress by introducing new delta values for longitude and latitude: refer to latDelta and lonDelta in the updated Plunker. I now have a better grasp of how it works in OrbitControls.js. You can notice a smooth scroll on page load due to lonDelta = 0.35. However, I'm unsure about adjusting this during user mouse scrolling. Nonetheless, I feel like I'm heading in the right direction.

Answer №1

Almost there! The key concept is to determine the user's panning speed and maintain that speed while gradually decreasing it to mimic friction. In case you're wondering, 'delta', or d, commonly represents distance. By finding out how far the user moved and how long it took them to do so, you can calculate the velocity and apply it using basic physics principles.

For more details, check out this link.

This section is crucial, extracted from the update function:

// Calculate time elapsed since last frame
var now = Date.now();
var dT = now - then;

if (isUserInteracting) {
    // Determine distance traveled since last frame
    var dLon = lon - prevLon;
    var dLat = lat - prevLat;
    // velocity = distance / time
    lonVelocity = dLon / dT;
    latVelocity = dLat / dT;
} else {
    // old position + (velocity * time) = new position
    lon += lonVelocity * dT;
    lat += latVelocity * dT;
    // handle friction
    lonVelocity *= (1 - dampingFactor);
    latVelocity *= (1 - dampingFactor);
}

// Store values for the next frame
then = now;
prevLon = lon;
prevLat = lat;

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

Is it possible to insert HTML content as plain text into a div?

Currently, I have a code that extracts HTML stored in a string and places it into a div with the contenteditable attribute set to "true". However, I'm facing an issue where the HTML is being executed as part of the page instead of being treated as tex ...

leveraging UI-Router for navigating based on app state and data

Is there a way to dynamically adjust Angular's ui-routing based on certain data conditions? For instance, let's say we need to create a subscription process where the user is informed of whether their subscription was successful or not. As the f ...

How can we implement a feature that allows users to save a page as a bookmark by clicking a specific button, and then store that bookmarked page link in their user profile for easy access

Is it feasible to create a function where clicking on a specific button saves the current page as a bookmark, storing the link in the user's account on the website instead of saving it directly into the browser? I am looking to implement this feature ...

After two HTML injections, Javascript ceases to function properly

I am in the process of developing a chat feature for my website. The layout includes a conversation section on the left and a list of users on the right. When a user's div is clicked, their ID is transmitted to the Django back end. Subsequently, full ...

Incorporating Array elements into a table designed with Bootstrap

In my HTML file, I have a script containing two arrays: one for names and one for scores. These arrays are populated using Bootstrap 4 and Firebase's Firestore services. Currently, I am facing an issue where I cannot pass these array variables direct ...

The lightbox feature seems to be malfunctioning, despite having successfully loaded jQuery

Struggling to activate lightbox on my gallery. Jquery is functioning as verified with an alert() in the documet.ready(). However, lightbox does not seem to be working properly. Only the links are operational. Below is a snippet of my code: <!DOCTYPE ht ...

Utilizing the no-data-text attribute in v-combobox with Vuetify: Tips and tricks

My application includes a simple combobox, and I am trying to set a default text message to display when there are no entries in the items props. To achieve this goal, I utilized the no-data-text prop by passing a specific variable to it. <v-combobox ...

The pageSize in React's Material Table does not reflect dynamic updates

Currently, I am attempting to implement pagination for material table data using TablePagination. One issue I am facing is that the pageSize property, initially defined as a state variable, does not update when the selected pageSizeOptions change. Despite ...

Loading Ajax content into div (Wordpress) without pre-loading the font can cause display issues

Currently, I'm implementing a JavaScript function within Wordpress using Ajax to load the content of a post into a modal div when specific elements are clicked. Here is an example of how the JS function appears: function openProjectModal($that) { ...

AngularJS: Utilizing $http to fetch XML data instead of JSON

Looking to extract data from a website using angularjs / javascript. I am familiar with the $http object in angularjs which can make get requests. I have used it before to retrieve json, but I'm wondering if I can use it for XML (HTML) as well? (I th ...

Error: Element type is invalid: a string was anticipated, but not found

I recently experimented with the example provided in React's documentation at this link and it functioned perfectly. My objective now is to create a button using material-ui. Can you identify what is causing the issue in this code? import * as R ...

Controller encounters a error when requiring a module

Struggling to set up Stripe for my app, I've encountered some issues with the module implementation. Typically, I would require a module at the top of the file to use it. However, in the paymentCtrl file, when I do this, it doesn't work and I rec ...

What is the process for importing module dependencies from other npm dependencies?

I have a question about building an NPM module with specific behavior. My goal is to have all dependencies work perfectly in the project once installed and imported. Here's what I currently have: https://i.sstatic.net/WZvME.png In my "my-module" pr ...

Delivering seamless css integration using express.js

Lately, I've been struggling with serving CSS using Express.js. After some trial and error, I managed to make it work. However, I'm puzzled as to why my new code works while the old one doesn't. Here's the code that now works for me: co ...

Low frame rate in a straightforward Three.js environment on Safari for MacOS

I have created a demo that runs smoothly on Chrome and Firefox but lags terribly on Safari. Disabling antialiasing slightly improves the FPS, but not significantly. I am wondering if there are ways to optimize my code, perhaps by implementing caching techn ...

Having difficulties in Vue while attempting to access a particular row

I encountered an error message stating Uncaught TypeError: snapShotChnaged.forEach is not a function. My goal is to retrieve specific row data, which is why I am utilizing the doc method. retrieveSpecificDonation() { firestore .collection('d ...

The 'fetch' operation could not be completed on the current window due to a TypeError

The error message I am receiving is: TypeError: Failed to execute 'fetch' on 'Window' : The provided value is not of type '(sequence<sequence> or record<ByteString, ByteString>)'. My current challenge involves fetc ...

Showing numerous skinned meshes on canvas using react-three-fiber

Greetings to all the coding wizards out there in the community! I recently delved into the world of react-tree-fiber and encountered an interesting challenge. I have a 3D skinned model that I've placed on a canvas, which you can check out on this sand ...

What are the best ways to stop jQuery events from propagating to ancestor elements?

I have a collection of nested UL's that follow this structure: <ul class="categorySelect" id=""> <li class="selected">Root<span class='catID'>1</span> <ul class="" id=""> <li>First Cat<span ...

Having issues with django-autocomplete-light triggering JavaScript errors

My implementation of django-autocomplete-light is causing some issues with rendering autocomplete options. There is a section on the website where it functions perfectly, but in another section, it only works partially. The autocomplete options display c ...