Spin Three.js camera around central point with mouse control

I am attempting to achieve camera rotation around the central point (0, 0, 0) using mouse controls. However, when rotating 180deg to the right or left, and then attempting to rotate vertically, the vertical rotation does not occur at all.

My approach involves using latitude and longitude coordinates which are then converted to Cartesian coordinates like so:

this.camera.position.x = Math.sin(this.lat) * this.alt;
this.camera.position.z = Math.cos(this.lat) * Math.cos(this.lng) * this.alt;
this.camera.position.y = Math.cos(this.lat) * Math.sin(this.lng) * this.alt;
this.camera.lookAt(new THREE.Vector3(0, 0, 0));

For a simple demonstration, please refer to the following link: https://jsfiddle.net/qsor9w27/2/

Answer №1

In my opinion, the most straightforward approach would involve utilizing either OrbitControls or TrackballControls

Take a look at this particular demonstration that showcases the use of OrbitControls.

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

I'm unsure how to utilize the generic type in this particular scenario. It's a bit confusing to me

Recently, I delved into TypeScript generics and applied them in specific scenarios. However, I encountered some challenges. While working with two different interfaces, I faced a need for flexibility. For instance, I needed to make server requests. func ...

What is the best way to obtain the clicked ID when a user clicks on an element within

As a budding web developer, I've been working on a code snippet to dynamically load data into an iframe: $(document).ready(function () { function loadMaterials(type, query) { $.ajax({ type: 'GET', url: &ap ...

When using body-parser, req.body may sometimes unexpectedly return undefined

My current project involves creating an API that handles POST requests for user creation. Unfortunately, I'm encountering undefined errors for all the req.body calls. Here's a simplified overview of how my application is structured: User control ...

Having trouble formatting the date value using the XLSX Library in Javascript?

I'm having trouble separating the headers of an Excel sheet. The code I have implemented is only working for text format. Could someone please assist me? const xlsx = require('xlsx'); const workbook = xlsx.readFile('./SMALL.xlsx') ...

Angular: Concealing a Component within a Controller

Being new to Angular, I am trying to figure out how to programmatically hide/show a component using the controller. I am having trouble understanding how to access my component and set ng-hide to false. Currently, my controller includes a service call. Af ...

Inserting additional entries into a pre-established data table

Here is a code snippet that creates a new table: var html = '<table>'; $.each( results.d, function( index, record ) { html += '<tr><td>' + record.ClientCode + '</td></tr>'; }); html += ' ...

Overwriting values in a JavaScript array

Within my function, I am running the code snippet below: stores = []; console.log('in stores d.length is ', districts.length); districts.forEach( function ( dis ) { dis.store.forEach( function( store ) { store.structure = dis.structu ...

What is the best way to allocate values within a for loop?

I am in the process of designing an interface for individuals who have no background in programming. My goal is to allow them to input certain details, and then be able to simply copy and paste the code to make everything function seamlessly. Here is a sa ...

Is there a way to identify and count duplicate data-items within an array, and then showcase this information using a react view?

If we have an array like this: [{id: 1, name: "chocolate bar"}, {id:2, name: "Gummy worms"}, {id:3, name:"chocolate bar"}] Is there a way to show on the dom that we have "2 chocolate bars and 1 Gummy Worms"? ...

Error: Unable to execute setState in React Native

Why am I receiving an error stating that this.setState is not a function? I'm having trouble understanding why my code isn't working as expected. import React from 'react'; import axios from 'axios' import { StyleSheet, Text ...

Building Ext JS packages is an essential step in the development

Sencha command offers a seamless way to create, build, and distribute custom packages. The Ext JS library itself is constructed using Sencha command. I am curious about the process for ensuring that the built package maintains proper order, especially whe ...

Using jQuery to set the background-image on the body's after pseudo-element with CSS

I am currently utilizing body:after for setting the page wallpaper. body:after { background-image: url('assets/img/wallpapers/<?php echo $getWallpaperFile; ?>'); } CSS content: ' '; display: block; position: absolute; left: ...

What is the best approach for finding the xPath of this specific element?

Take a look at this website Link I'm trying to capture the popup message on this site, but I can't seem to find the element for it in the code. Any ideas? ...

When an array is prototyped as a member of a JavaScript object, it becomes a shared property among all instances

Is anyone else surprised by this behavior? It really caught me off guard... I was expecting prototyped arrays to be private to each instance of a class rather than shared across all instances. Can someone confirm if this is the intended behavior and provi ...

How to address hover problems in D3.js when dealing with Path elements and updating tooltip information after brushing the focus

Seeking assistance with a Multi Series, Focus + Context D3 chart and hoping to address my main queries all at once. The questions that need resolving are: How can I prevent the tooltips I've generated from being affected by the hair-line (which t ...

Utilizing a repetitive function in AngularJS

In my controller, I have a repetitive function that looks like this: //FUNCTION 1 $scope.selectedage = []; $scope.pushage = function (age) { age.chosen = true; $scope.selectedage.push(age); console.log($scope.selectedage); }; $scope.unpushage ...

Tips for organizing and refining ngTable data that is stored in milliseconds?

I'm new to Angularjs and I could use some assistance with sorting dates in milliseconds in descending order, as well as implementing a filter for the table columns. I've set up a plunker example, but when I enter a filter parameter, the data does ...

Exciting jQuery animations and transitions

Is there a way in JavaScript to call function or method names using a string? For example, when writing jQuery code that applies an effect to specific targets, can the effect be dynamic and changeable by the user? I'm hoping for something like jQuer ...

Introduce regex in the middle

If I have a string in JavaScript like so: "test test hello test test" I understand how to replace 'hello' with 'byebye': replace("hello","byebye"); However, what is the most efficient method to add 'byebye' immediately afte ...

How can one activate a function using jQuery and then proceed to activate another function once the first function has finished executing?

My current assignment involves implementing a task using jquery. Upon loading the page, the initial jquery function will be executed. After the successful completion of the first function, it should seamlessly transition to the next function on its own. ...