carla-simulator / carla

Open-source simulator for autonomous driving research.
http://carla.org
MIT License
11.35k stars 3.68k forks source link

Radius of the towns #564

Closed imran514 closed 6 years ago

imran514 commented 6 years ago

I am trying to calculate distance between vehicles using their transform locations using the below code

lat1, lon1 = vehicle1
lat2, lon2 = vehicle2
radius =  ??????

dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
    * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
distance = radius * c

But i need the radius of the town to get the distance

nsubiron commented 6 years ago

Wow that's some overkill.

You are using the Haversine formula, which computes the shortest distance between two points on a sphere. That's a great approximation for computing distances on Earth but unfortunately, in Carla we use a flat-earth model [*]. But no worries, math got you covered, we can just say our cities have an infinite radius and work out a simpler solution.

The Haversine formula can be expressed as

haversine

you can see how setting the radius to infinite wouldn't work, we can however use the opposite approach, lets assume the radius is some finite constant (say 1) but angles are extremely small compared to this radius. Under these assumptions we can use the small angle approximation for sinus and cosinus

tend_to

applying this approximation to the original formula we get

haversine_approx

multiplying by 4 and changing the variable names to x and y (to follow Carla convention), we get

cartesian_distance

that's a much simpler formula :stuck_out_tongue_winking_eye:

Now really, I'm kidding, in Carla you should use the Cartesian distance.


[*] We actually follow a Discworld model, we just don't simulate the 4 elephants and the turtle for performance reasons.