Provide two instances of xyz along with the timestamp and calculate the speed

Utilizing this script to calculate the distance between two sets of xyz coordinates.

function getDistance(x0, y0, z0, x1, y1, z1){
  let deltaX = x1 - x0;
  let deltaY = y1 - y0;
  let deltaZ = z1 - z0;
  let distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
  return distance;
}

Given the following coordinates:

arr1 = [1524519105.309092, 575131.076859, 4141688.619666, -14.086608]
arr2 = [1524519105.310092, 575131.082497, 4141688.628375, -14.086852]

Where index 0 is the timestamp and the next 3 numbers represent xyz values. The objective is to compare these coordinates and determine speed. Here's the current progress:

let x2 = arr2[1] // Array 2 x
let y2 = arr2[2] // Array 2 y
let z2 = arr2[3] // Array 2 z
let x1 = arr1[1] // Array 1 x
let y1 = arr1[2] // Array 1 y
let z1 = arr1[3] // Array 1 z
var c = getDistance(x1, y1, z1, x2, y2, z2)
var timeDifference = arr1[0] - arr2[0] // difference in time 
var speed = Math.round(c / timeDifference);

The formula is not yielding the correct speed at times; occasionally resulting in -0, indicating a possible error in the time difference calculation. Ultimately, aiming to obtain the speed in meters per second.

Answer №1

Utilize the full potential of Three.js if you are working with it.

var arr1 = [1524519105.309092, 575131.076859, 4141688.619666, -14.086608]
var arr2 = [1524519105.310092, 575131.082497, 4141688.628375, -14.086852]

var vec1 = new THREE.Vector3().fromArray(arr1, 1);
var vec2 = new THREE.Vector3().fromArray(arr2, 1);

var timeDiff = arr2[0] - arr1[0]; // calculate the time difference

var speed = vec2.sub(vec1).divideScalar(timeDiff).length(); // determine the speed based on the calculations

console.log({speed});
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>

Answer №2

var time_difference = arr1[0] - arr2[0]

If the timestamp in arr1[0] is earlier than the one in arr2[0], the result of arr1[0] - arr2[0] will be negative. To ensure a positive value for speed regardless of the order of timestamps, you can apply Math.abs to the subtraction result:

function dist(x0,y0,z0,x1,y1,z1){
  deltaX = x1 - x0;
  deltaY = y1 - y0;
  deltaZ = z1 - z0;
  distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
  return distance;
}

arr1 = [1524519105.309092, 575131.076859, 4141688.619666, -14.086608]
arr2 = [1524519105.310092, 575131.082497, 4141688.628375, -14.086852]

let x2 = arr2[1];
let y2 = arr2[2];
let z2 = arr2[3];
let x1 = arr1[1];
let y1 = arr1[2];
let z1 = arr1[3];

var c = dist(x1,y1,z1,x2,y2,z2);
var time_difference = Math.abs(arr1[0] - arr2[0]);
var speed = Math.round(c / time_difference);

console.log(speed);

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

How to Customize the Size and Color of secureTextEntry Inputs in React Native

When it comes to styling a password input like the one below: <TextInput name="Password" type="password" mode="outline" secureTextEntry={true} style={styles.inputStyle} autoCapitalize="none" autoFocus={true} /> I also ap ...

What is the best way to restrict event handling to only occur once every X seconds using jQuery or JavaScript?

Is there a way to limit handling the event of a rapidly-firing keypress to once per X seconds using jQuery or vanilla JavaScript? Check out this jsfiddle that demonstrates rapid keypress firing without any limiting on the handling: View here ...

What is the best way to trigger a function following the blur event on an input field

My form includes a jquery-ui datepicker as one of the inputs. When a user moves away from an input field, I want to show a specific message (e.g. "This field is required"). The jQuery code below accomplishes this for basic <select> and <input type ...

Ways to retrieve every span within a string variable that possesses a specific class designation

I'm having trouble listing out all spans with the class "ansspans." There may be one or more span elements with the "ansspans" class, and I need to retrieve them along with their content in order to iterate through them. Can someone explain how to do ...

Unsure if values can be imported from one component to another in Vue.js using props

Currently experimenting with Vue and ran into a minor challenge - wondering if there's a solution to this. I am looking to add a new value to my items array using my Button component so that I can display it using v-for. While consolidating everything ...

Checkbox Hierarchy: Children marked as either Checked or Unchecked based on parent selection

Hello, I have a form that contains nested checkboxes on three levels. Using jQuery, I am trying to check/uncheck all the children when I check a parent level... and if any of the children are unchecked, uncheck the parent as well. I have attempted this m ...

Dynamic Parameter (?) in NodeJS Sqlite WHERE IN Query Statement

In my Node application, I encountered an issue with using dynamic parameters in my SQLITE statements. Specifically, when trying to implement a WHERE IN query, no results were returned. This suggests that the dynamic parameter is being misinterpreted. Below ...

Retrieve Latitude and Longitude by clicking using JavaScript

When I click on the map, I want to retrieve the latitude and longitude coordinates and display them in an alert. Here is the code I have: <!DOCTYPE html> <html> <body> <div id="googleMap" style="width:100%;height:400px;"></div&g ...

Moogonse request for modification

json:{ data:{ id:"123"} , res:{ message:false, "resId":"afdsfd" } } I need to make changes to the res field in the JSON above, but I'm having trouble doing it using mongoose in my NodeJS app. Currently, I am attempting t ...

What could be the reason my object is not re-rendering even after the state of the object has been updated

I am currently working on implementing an infinite scroll feature by iterating through my JSON data. Initially, I display the first 2 records upon loading the page. The goal is to update the object when a user scrolls to the bottom of the page in order to ...

Using CSS to Position a Circle at the Top of a Border

Breaking down the complex issue into a simple statement, I am dealing with a blue circle inside a box with a red border. How can I ensure that the circle remains centered while overlapping the top horizontal line of the box's border? I tried differe ...

Using jQuery does not automatically pre-check multiple checkboxes. This issue may arise when using PHP and AJAX together

I'm attempting to dynamically check multiple checkboxes based on previous entries from a MySQL database. I have the data stored in a variable: var = ObjektLevHur; The potential data values are: frittlev, frittfabrik, or mont. When I make my first sele ...

Node.js web server operating on port 3000 is running a script

I have developed a Node.js script that extracts data from a database and saves it in a file. Additionally, I have set up a Node.js web server listening on port 3000 using forever to keep it running constantly. However, the script currently lacks a web inte ...

Encountering an error while trying to load a CSS file in a React project using webpack due

I'm currently working on a React project that utilizes styled components, and I've been attempting to integrate a CSS file as part of the react-image-gallery package. Following the instructions provided, I included the css-loader and style-loade ...

apostrophe cutting off a portion of the input field's value

I am facing an issue with a reloaded input box on a web page that is updated through an ajax call. Whenever the input contains a single quote, the rest of the value gets cut off. How can I resolve this problem? The value assigned to myVal dynamically from ...

Guide on sending a POST request and fetching JSON data in Ruby on Rails

Despite going through numerous questions, I am still perplexed. My current project involves creating a Rails application that interacts with the Healthgraph API (link here ). I am required to send a POST request using Rails based on a received parameter ...

The direction of view decides the reversal of Three.js Materials

Working on my latest project using Three.js to create a virtual model of a house. The main purpose is to showcase the design to an architect for further discussion. The interactive aspect allows users to walk through the home virtually on this website. To ...

Navigating through a jQuery array while utilizing the $.inArray method

Below is an example of my Array that I am working with, retrieved from a successful jQuery .post call: data = JSON.parse(data); $(".chk_permission_").each(function() { if ($.inArray($(this).val(), data)) { $(thi ...

Attempt to create a truncated text that spans two lines, with the truncation occurring at the beginning of the text

How can I truncate text on two lines with truncation at the beginning of the text? I want it to appear like this: ... to long for this div I haven't been able to find a solution. Does anyone have any suggestions? Thanks in advance! ...

How to integrate a While loop within an RXJS subscription

I have a piece of Angular code where I am attempting to subscribe to my first API and include a while loop within this subscription. Additionally, I need to subscribe to another API inside the while loop. The reason for this is that I need to subscribe t ...