Specific camera positioning in three.js

When trying to position the camera in a specific location, I usually move the camera and adjust it accordingly to get the desired coordinates. I then store these values for future reference by utilizing the "update" function.

console.log(camera.position);

After logging the camera's position values as:

x: 2.4457938219139463, y: 0, z: 17.37913738929295

I proceed to update the camera position using the retrieved coordinates next time my application is run:

camera.position.set(2.4457938219139463, 0, 17.947541735897405)
camera.lookAt(scene.position);

However, upon running the code, the camera does not necessarily end up at the exact same position as initially set, instead appearing to be positioned close but not exactly where intended originally.

Answer №1

One issue arises from the precision of floating point numbers in JavaScript, with 7 digits after the decimal point for a 32-bit operating system and 13 digits after the decimal point for a 64-bit operating system. Therefore, it is important to limit the accuracy to 7 digits and round off when necessary. For example, you can use lat.toFixed(7)/1;

Consider this example:

Code

var x = 2.44579380000034;
var y = 0.0000000000001;

console.log(x);
console.log(x+y);

var x = 0.1;
console.log(x);
console.log(x*x);
console.log(x*x*x);
console.log(x*x*x*x);
console.log(x*x*x*x*x);
console.log(x*x*x*x*x*x);

Output

2.44579380000034
2.4457938000004398

0.1
0.010000000000000002
0.0010000000000000002
0.00010000000000000003
0.000010000000000000004
0.0000010000000000000004

Link to Example

P.S. Learn more about Double-Precision Floating Point Format

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

Show the values of an array if the array name matches the string name

Is there a way to dynamically display the values of an array based on a matching string in Angular 4 or 6? Let's say we have two arrays, A and B: A = ['aaa','arrr','aeee','aqqqq','awwww','axxxx& ...

Tips on converting Sequelize response to an array of strings instead of objects

When utilizing Sequelize on a join query, I am currently facing the issue where the data from the join table is presented as an array of objects instead of strings. The client specifically needs an array of strings. Is it possible and advisable to achieve ...

Unable to place within div

Utilizing HTML5 to implement the "DnD" function. There is a button that adds divs. I want to be able to drag items into these newly added divs. Currently, I can only drag into existing divs and not the ones added later. How can I resolve this issue ...

Explore various jQuery functions to manipulate an array

Is there a way for me to condense the jQuery code below using a for loop? I want to input array numbers and selector positions in one block. jQuery(document).ready(function(){ jQuery('.hoverB0x:eq(0)').html('<a style="background:whi ...

Unresponsive radio buttons in React failing to display as checked

My child component is designed to receive props from a parent. Within the child component, there are radio buttons that render like this: <div> <div className="radio"> <label> <input type="radio ...

Countless unsuccessful connection attempts on Node.js and socket.io

Good day awesome community at SO! I've been struggling to resolve this issue, and now I'm turning to you for some guidance because I can't seem to figure it out on my own. I have a node.js server that serves an index.html through express, an ...

How can I effectively organize an Angular2 component library for optimal efficiency?

Looking to develop an Angular2 datepicker component with the intention of releasing it and using it in multiple projects. Wondering about the best way to structure the project for this specific purpose compared to a typical Angular2 project created with an ...

Creating a dropdown menu for an extensive list of 100 menu items: a step-by-step guide

In my React JS front-end project, I have implemented a drop-down menu using Material-UI. Currently, the menu items are hardcoded which works fine for a small number of items. However, this approach becomes cumbersome when dealing with a larger number of ...

Tips for extracting information from a select box that is dynamically populating

I have a dropdown menu displayed in the image below. It contains a "More..." option that, when clicked, loads the next ten data items. Subsequent clicks on "More" will load another set of ten data items each time until the entire list is loaded. How can I ...

Ensure the presence of an editable column within a table using a jQuery function

Within the table, the first column is editable. Upon making a change to it, I want an alert to appear indicating that a change has occurred. The check function is being called after a delay of 5000ms. Embedding Code Snippet for Reference I suspect there ...

What is the best way to change the innerHTML of an element with AJAX before it has been loaded into the DOM?

Currently, I have a main feed that displays a list of blog post titles. Whenever a title is clicked, I want to show the details of the corresponding blog post in a new HTML file. Here's my existing code: window.location.href = "/viewpost.html"; ...

Ways to dynamically trigger a function in the componentDidMount lifecycle method?

Let's consider something interesting: componentDidMount() { const { currentUserId, userList } = this.props; //I receive these values from the mapStateToProps function in Redux; //they are not standard props. Mousetrap.bind([' ...

Encountering an issue with a basic jQuery custom function

Here is the code snippet that I am using: $(document).ready(function() { $.fn.addRemoveButton = function() { alert(1); }; $.addRemoveButton(); }); When I run this code, I encounter the following error in Firebug: TypeError: $.addRem ...

Customize Cell Styling with Bootstrap Full Calendar CSS

I am attempting to implement a Bootstrap calendar feature where cells are colored green if the timestamp is greater than today's date. This can be achieved by: $checkTime > $today cell.css = green background I came across this code snippet on St ...

Display blanks rows after running a v-for loop

I am currently developing a vue component file where I have successfully fetched data using Laravel WebSockets. The issue I am encountering is that when I try to display the selected data in a table within the template tag, the rows in the table are bein ...

Steps to hide a div with jQuery when a user clicks outside of a link or the div itself:1. Create a click event

Here's a simple question - I have a link that when clicked, displays a div. If the user clicks away from the link, the div is hidden, which is good. However, I don't want the div to be hidden if the user clicks on it. I only want the div to disap ...

What is the best way to run a JavaScript function once another function has finished executing?

I am looking to ensure that a JavaScript function runs only after another one has finished executing. Here is the code I currently have: $(window).load(function(){ $('#dvLoading').fadeOut(2000); }); window.addLoadEvent = function() { $('#p ...

The navigation bar options are not collapsing as intended

When I resize my page to width:750px;, my navigation bar is not collapsing the items. Despite trying to edit my CSS, I am still clueless. Here is my live page. This is the CSS for my slidebar: @media (max-width: 480px) { /* Slidebar width on extra small ...

Tips for concealing lengthy text within a select box and automatically wrapping it when displayed as an option

Is there a way to conceal lengthy text within a select box and display it only when the option is selected? I want to hide long text in a select box options, but have some sort of signal to show that more text is available. Is there a javascript solution ...

Is there a way to customize the animation for a button in material UI?

Trying to implement material UI in React and looking for a button that does not have the standard wave animation effect upon clicking, which you can see demonstrated here. Instead, I am searching for an animation that instantly fills the entire button wit ...