calculate distance between two moving elements

how to find changing distance between two moving objects
minimum distance between two moving objects
distance between two moving objects calculator
how to find the shortest distance between two moving ships
minimum distance between two moving vehicles
related rates - distance between two moving objects
related rates two cars start moving from the same point
related rates two cars approaching intersection

I'm trying to calculate the distance between two moving HTML elements. For now, I'm making it move on :hover. But the result doesn't change when it moves.

What's going wrong?

Here's my code:

var lFirst = $("#x").offset().left;
var lSecond = $("#y").offset().left;
var ldist = parseInt(lFirst - lSecond);

var tFirst = $("#x").offset().top;
var tSecond = $("#y").offset().top;
var tdist = parseInt(tFirst - tSecond);

$('#result').append(parseInt(tdist + ldist));
html, body{
  margin:0;
  padding:0;
}
*{
  transition:all 1s;
}
#x, #y{
  width:50px;
  height:50px;
  margin-left:0;
  margin-top:0;
  background:black;
}
#container{
  height:100vh;
  width:100vw;
  background:lightgrey;
}
#container:hover #x{
  margin-left:50vw;
}
#container:hover #y{
  margin-top:50vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div id="result"></div>
  <div id="x"></div>
  <div id="y"></div>
</div>

The result is only set once. You'll need to update it as objects move. One solution is to update the value on a timer, using JavaScript's setInterval(). In the example below, I'm updating the result every 100 milliseconds.

var $x=$('#x');
var $y=$('#y');
var $result=$('#result');

function updateDistance() {

  var lFirst = $x.offset().left;
  var lSecond = $y.offset().left;
  var ldist = parseInt(lFirst - lSecond);

  var tFirst = $x.offset().top;
  var tSecond = $y.offset().top;
  var tdist = parseInt(tFirst - tSecond);

 $result.text(parseInt(tdist + ldist));

}

setInterval(updateDistance, 100);
html,
body {
  margin: 0;
  padding: 0;
}

* {
  transition: all 1s;
}

#x,
#y {
  width: 50px;
  height: 50px;
  margin-left: 0;
  margin-top: 0;
  background: black;
}

#container {
  height: 100vh;
  width: 100vw;
  background: lightgrey;
}

#container:hover #x {
  margin-left: 50vw;
}

#container:hover #y {
  margin-top: 50vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div id="result"></div>
  <div id="x"></div>
  <div id="y"></div>
</div>

Finding the distance between two moving objects, as stated by @N.S.JOHN the terms specify the initial position, velocity with which they come together (since there's a negative sign) and at last the acceleration  *rates as low as $499 subject to availability, move size, distance and location

You can find how to get the (X,Y) coordinates of an html element here:

Retrieve the position (X,Y) of an HTML element

Once you have the (x,Y) coordinates, the distance is just a math problem:

distance = sqrt((x2-x1)^2 + (y2-y1)^2)

calculate distance between two moving elements, The result is only set once. You'll need to update it as objects move. One solution is to update the value on a timer, using JavaScript's  Rates as Low as $999 (prices based on: weight, distance & location)· Got Movers™. Enter Your Information And Start Getting Quotes Today! Got Movers™

You will have to either set a JavaScript Timing Event to trigger on X time and update the info in your result div or set an event handler on the HTML element every time it moves so it updates the result.

setInterval(function, milliseconds)

//Repeats the execution of the function continuously.

Help calculating distance between two moving objects, I am working on a little side project in code and I am trying to figure out how to calculate the distance between two moving objects at a give  Full Service· Local· Long Distance· QuoteRunner -The Quickest Way to Get Quotes™

distance changing between two moving objects, John Y. 5.0 (520) · See more tutors · find an online tutor. In a typical related rates problem, such as when you’re finding a change in the distance between two moving objects, the rate or rates in the given information are constant, unchanging, and you have to figure out a related rate that is changing with time. You have to determine this related rate at one particular point in time.

Distance between two moving particles, Let's say I have two particles moving with constant speeds along straight For each time step of 0.1 second, I need to calculate the distance between and you could get that distance, along with the time point (element) at  Output: Minimum distance between 3 and 6 is 4. Output: Minimum distance between 3 and 2 is 1. Use two loops: The outer loop picks all the elements of arr[] one by one. The inner loop picks all the elements after the element picked by outer loop.

What's the Distance Formula?, Calculating distance requires one of two formulas, with the one you use The calculation assumes that you're moving at a constant rate and that your If you know these two elements, the distance traveled over that period of time is simply a  the distances between elements are: 1-3-5-1: 1-4-1: 1-2-6-1: what I need is to accumulate the distance of elements between every two ones, and if the distance between every two ones is greater than 5 then a penalty of 3 should be added to the total distance of the whole array.

Comments
  • Can it run with css animation or just with transition ?
  • Theoretically, you could use a Mutation Observer to detect changes to the DOM, but you'd need to change the positions by using the style attribute; see Event detect when css property changed. Or, you could use CSS animations and listen for the animation's end. But I'd say the setInterval method is simpler, given your existing code.