0
\$\begingroup\$

I am developing an AR application where a number of historical buildings are spawned around the player depending on his position when a target / QR Code is recognized.

I know the Lat/Lon of these buildings and I have the Lat/Lon of the player. I have already implemented the Harvesine algorithm and can calculate the relative bearing of the buildings:

public static double Harvesine(double lat1, double lon1, double lat2, double lon2)
{
    double R = 6371000; // metres
    double omega1 = ((lat1 / 180) * Mathf.PI);
    double omega2 = ((lat2 / 180) * Mathf.PI);
    double variacionomega1 = (((lat2 - lat1) / 180) * Mathf.PI);
    double variacionomega2 = (((lon2 - lon1) / 180) * Mathf.PI);
    double a = Math.Sin(variacionomega1 / 2) * Math.Sin(variacionomega1 / 2) +
                Math.Cos(omega1) * Math.Cos(omega2) *
                Math.Sin(variacionomega2 / 2) * Math.Sin(variacionomega2 / 2);
    double c = 2 * Math.Asin(Math.Sqrt(a));

    double d = R * c;

    return d;
}

public static double Bearing(double lat1, double lon1, double lat2, double lon2)
{
    double radians;
    double x = Math.Cos(lat2*Mathf.Deg2Rad) * Math.Sin((lon2 - lon1) * Mathf.Deg2Rad);
    double y = Math.Cos(lat1 * Math.PI / 180) * Math.Sin(lat2 * Math.PI / 180) - Math.Sin(lat1 * Math.PI / 180) * Math.Cos(lat2 * Math.PI / 180) * Math.Cos((lon2 - lon1) * Math.PI / 180);

    radians = Math.Atan2(x, y) * 180 / Math.PI;

    return radians;
}

From here, how can I spawn the building relative to the player position? I have the distance (Harvesine) and the angle (Bearing), how can I get the Vector3 location?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I made a library for this purpose, documentation is scarce and it's been over a year since I wrote it, but check it out: github.com/starikcetin/Eflatun.GeoUnity \$\endgroup\$ Commented Sep 10, 2020 at 1:16
  • \$\begingroup\$ I'll definitely check it out, thanks! \$\endgroup\$
    – Steffan
    Commented Sep 10, 2020 at 1:29

1 Answer 1

2
\$\begingroup\$

Presumably you considered just converting your angle and distance from polar coordinates into a Cartesian vector in the usual way?

public static Vector3 FromPolar(float angleRadians, float radius) {
    return new Vector3(Mathf.Sin(angleRadians), 0 Mathf.Cos(angleRadians)) * radius;
}

You can now add that offset to the player position to spawn the building at that displacement from the player.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks! It's so simple when you know the math! I'm from the humanities so I struggle sometimes with trig and stuff... \$\endgroup\$
    – Steffan
    Commented Sep 10, 2020 at 0:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .