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:
And my X and Y formulas follow these:
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?