Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Getting positions from distance

Discussion in 'Scripting' started by Deuce2008, Oct 18, 2023.

  1. Deuce2008

    Deuce2008

    Joined:
    Sep 30, 2020
    Posts:
    9
    I don't post here often but I'm kinda drawing a blank.

    I'm trying to get the position of an object when given its distance from 3 others.

    So basically I have 3 float distances between an object an a point. I want to know where to instantiate a prefab so that it's that far away from all three fixed points.

    Thank you
     
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    293
    Your question is not entirely clear. What does 'far away from all three fixed points' mean? You can create an object at coordinates x: 10.000, y: 10.000, and that also qualifies as 'far away.' Please describe the situation more specifically. It might also help to have an illustration of what you want.
     
  3. Deuce2008

    Deuce2008

    Joined:
    Sep 30, 2020
    Posts:
    9
    Sorry,
    upload_2023-10-18_11-20-25.png

    Go from each point and narrow down the possible locations, by knowing the distance from the new object.
    So the first point means that the new object will be on any point of that circle.
    The second point narrows it down to just two points.
    The third eliminates one of the options, so there's only one possible point the new object could be.

    I want to, given the distances between each of the points, calculate where the new object will be.

    Apologies for the poor drawing, I know the circles are not centered.
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,164
    Code (CSharp):
    1. spawnPosition=(p1+p2+p3)/3;
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,661
    A lot of people might say you're describing "triangulation," but the more technically accurate term is "trilateration," because you know the distances, not the angles.

    This is C# code, but not Unity code, so there are some differences. Good description of what I think you're trying to do. The math is a little tricky but shouldn't be too bad. http://csharphelper.com/howtos/howto_trilateration.html

    @samana1407 , they used the phrase "it's that far away from all three fixed points", not "it's far away from all three fixed points." It's a known distance.
     
    Bunny83 and Deuce2008 like this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    What Zulo posted above is known as the "centroid," or "center of mass." But it assumes all circles are the same and does not consider radii of the circles.

    As for your drawing, there's many more possibilities to consider, such as none within range, or all three in range within a line, so there would be 4 possible spots, etc.

    Mathematically the answer can come from a system of three distance-equality equations and isolating for the unknowns. When there is a no-match value, I am pretty sure the equations would return an imaginary number.

    There is likely also a linear math approach with matrices and discriminants that I'm simply not up-to-speed on anymore.

    If it's just for a fast-twitch game, Zulo's suggestion is a great starting point, then you can push the spawn point away in random directions until it satisfies (or nearly satisfies) your needs.

    EDIT: and Halley posts the actual mathematical answer.
     
    Deuce2008 likes this.
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,164
    And my answer requires the points but it seems he wants to be able figure out the position just from distances alone which seems impossible. So I dunno what's going on... :)
     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,661
    Trilateration is done every day, you almost surely have a device which does it in two different ways within arm's reach.

    A cell phone figures out its position (on our Earthy sphere, no less) by measuring the time delays between GPS satellites, and by measuring the signal strength to the nearest towers. Both algorithms convert their measurements to distance, and then trilateration or its 3D equivalent from there.
     
  9. Deuce2008

    Deuce2008

    Joined:
    Sep 30, 2020
    Posts:
    9
    Yeah, I know the distances and that there will be a point that satisfies all three constraints,
    on the intersection of the circles(not just within range)
    And that the points don't all rest on the same line.

    At first I thought of triangulation, but I realized that that requires angles which I don't have(I'd never heard of trilateration before today).

    Just for reference I'm making a map, so I want it to be reasonably accurate. Which is why I wanted to figure the exact point.

    I'm working in 2D just to make it easier, so that reduces a lot of the possible mistakes.
    I'll read that article and figure out the math,

    Thanks for the help
     
    Last edited: Oct 18, 2023
  10. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,164
    it's a disc, you fool! :mad:
     
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,261
    Well, if you never came across the term, of course you can't know it. Though most people know GPS which also works by trilateration (or multi-lateration to be more specific). Technically you need 4 fixes to determine a point in 3d space and 3 in 2d space. Though in many cases you could infer the correct location based on certain constraints. At least when we talk about GPS when you only have 3 ranges you get 2 potential locations, though the second one is not on our planet but equally distant from the plane that the 3 satellites are in.

    Funny enough, I once "rewrote" the "GPS" lua script in computercraft (a minecraft mod that adds ingame computers). The mod adds wireless modems and each message that is received also contains an exact distance value. So you can setup fix "satellites" in the world which broadcast their location and other devices can determine their own position based on the received messages. Though in minecraft a "turtle" (a little one block robot) can only move one block at a time. So you could even determine your location with just two if you take a measurement, move and take another. Since everything is in increments of full blocks, you can even analyse the fractional part of the distance and infer the lateral offset between two measurements. Was a fun exercise years ago.

    Here's the original gps api that ships with the mod. The "trilaterate" function contains most of the magic. The rest is mostly about sending a request to the satellites and receiving the responses.