1

I've been reading up on Trilateration via its page on Wikipedia and I've attempted to make a 2D version of my own. However, I've ran into some issues I was hoping someone with a better grasp on math could help me with.

I'm doing this in Unity with C#.

My scene comprises of 3 spheres in the centre of the screen. Sphere 1 is always at mark 0,0,0. Beside these spheres I have a cube for which I want to move to the intersection point of the 3 spheres.

Here is my code so far:

public GameObject sphere1, sphere2, sphere3;
float x, y;
float P1, P2, P3;
public GameObject cube;

// Update is called once per frame
void Update () 
{
    P1 = ( sphere1.transform.position.x * sphere1.transform.position.x) + ( sphere1.transform.position.y * sphere1.transform.position.y);

    P2 = ((sphere1.transform.position.x - sphere2.transform.position.x) *(sphere1.transform.position.x - sphere2.transform.position.x)) + sphere1.transform.position.y;

    P3 = ((sphere1.transform.position.x - sphere3.transform.position.x) *(sphere1.transform.position.x - sphere3.transform.position.x)) + 
        ((sphere1.transform.position.y - sphere3.transform.position.y) *(sphere1.transform.position.y - sphere3.transform.position.y));

    x = (P1 * P1) - (P2 * P2);
    y = ((P1 * P1) - (P3 * P3)) + (sphere3.transform.position.x * sphere3.transform.position.x) + ((sphere3.transform.position.y * sphere3.transform.position.y) / 2 * sphere3.transform.position.y);

    cube.transform.position = new Vector3(x, y, 0);

}

P1, P2 and P3 are my interpretation of the following math formulas from the wiki page:

enter image description here

enter image description here

enter image description here

And my X and Y formulas follow these:

enter image description here

enter image description here

When I hook all of that up, I get the following debug logs:

P1: 0
P2: 0.0989904
P3: 0.4478847
X is: -0.0097991Y is: 0.247284 // X and Y of cube

However, when I run my code, the cube doesn't go inside the intersection point, it just moves to the centre of sphere 1. When I move sphere 1 and sphere 2, my cube does move, but often in the completely different axis. When I move sphere 3, it seems to have no impact on the program at all.

Have I set up my triangulation properly? Am I missing something?

N0xus
  • 2,674
  • 12
  • 65
  • 126
  • Is the cube at the root of the scene or is it a child of something? – McAden Mar 28 '14 at 16:53
  • Is it because your `P2` formula has a `y` term rather than a `y^2` term? You have `+ sphere1.transform.position.y` rather than `+ (sphere1.transform.position.y * sphere1.transform.position.y)`. – Jim Mischel Mar 28 '14 at 18:19
  • Also, I think your formula for `x` should be `(P1 - P2 + d^2)/2d`. You're using `(P1^2 - P2^2)`, which seems quite wrong, given the notation. – Jim Mischel Mar 28 '14 at 18:22
  • Thanks for the commnets, I'll look into the X and Y issue. The cube is in the root of the scene. Not a child of anything. – N0xus Mar 29 '14 at 10:24
  • I think he is asking something like this....https://stackoverflow.com/questions/53203162/how-to-find-the-user-position-on-a-2d-floor-plan-if-other-coordinates-3-or-more?noredirect=1#comment93331493_53203162 – zyonneo Nov 09 '18 at 09:12

2 Answers2

2

You did not understand wiki's formula. The first set of equations is used to derive the second set of equations you showed here from it, not to derive values of r1, r2, and r3 and then put them into second set of equations, like you do.

You are calculating r1, r2 and r3 the wrong way. According to your code r1 always equals 0, when it should be equal to radius of the sphere 1, not to sum of squares of its coordinates. The same goes for r2 - radius of sphere 2, and r3 - radius of sphere 3.

Also, accroding to Wiki, center of sphere1 is always (0,0,0) and center of sphere2 lies somewhere on x axis - coordinates are (d,0,0), where d is some value. So, you code should look smth like this:

public GameObject sphere1, sphere2, sphere3;
float x, y;
float r1, r2, r3;
public GameObject cube;

void Update () 
{
    r1 = sphere1.transform.localscale.x * sphere1.GetComponent<SphereCollider>().radius;

    r2 = sphere2.transform.localscale.x * sphere2.GetComponent<SphereCollider>().radius;

    r3 = sphere3.transform.localscale.x * sphere3.GetComponent<SphereCollider>().radius;

    x = (r1 * r1 - r2 * r2 + sphere2.transform.position.x * sphere2.transform.position.x)/(2*sphere2.transform.position.x);
    y = ((r1 * r1 - r3 * r3 + sphere3.transform.position.x * sphere3.transform.position.x + sphere3.transform.position.y * sphere3.transform.position.y) /( 2 * sphere3.transform.position.y)) - (x * sphere3.transform.position.x)/sphere3.transform.position.y;

    cube.transform.position = new Vector3(x, y, 0);

}
1

As final steps, you should calculate the resulting coordinates by adding vectors ex*x and ey*y to your point p1.

result = p1 + e*ex + y*ey;
padawan
  • 1,295
  • 1
  • 17
  • 45