# Given: Latitude/Longitude/Bearing/Distance, Where am I?

This topic is 3398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Alright, so I'm on a perfect sphere at a location with a known latitude and longitude. I then walk some known distance with a known bearing. Where do I end up? What is my new latitude and longitude? Thanks for any insights with this.

##### Share on other sites
convert lat/lon to vector from the center of the sphere. Convert distance/bearing to rotation matrix. Multiply M*v. Convert back to lat/lon.

##### Share on other sites
This site explained it quite well to me when I needed to do the same calculations:
http://www.movable-type.co.uk/scripts/latlong.html

##### Share on other sites
Awesome, thanks guys, that's a big help.

##### Share on other sites
After some pretty extensive testing, I unfortunately ran into another snag, and I hope you guys have a bit more advice.

The circumference of my world is 1024 (unitless). So the radius is 1024/(2PI), or about 162.97 (which I have set to a constant I call WORLDBUILDER_WORLD_RADIUS).

The equations I am using to get a new lat/long based on starting lat/long, distance, and bearing are:

They work *almost* perfectly. But here is the snag.

I set a point at the equator (lat = 0, long = PI) and fed the routine a distance of 512 with a bearing of PI/2 (straight east). As expected, the new point came up exactly half way around the world from where it started (2PI). Cool.

Then I set a point closer to the north pole (lat = 1.0, long = PI) and gave it the same distance and bearing. What I got out of the routine was again, a point exactly half way around the world from where I started (2PI). Boo!

Clearly if someone is walking a distance straight east for 512 units at the equator, they won't end up at the same longitude as someone walking that same distance near the north pole. The latter should have gone a much greater angular distance than the former.

I think my problem is this part of the equation:
That is the angular distance, but it assumes the radius is equal to the world radius (i.e. it's calculating based on the greatcircle of the two points). I think what I actually need is the radius of the circle along the path used (not sure what that is called) rather than the greatcircle.

I can do that if the two points happen to share the same latitude (I derived the equation for that), but if they do not, I'm at a loss.

To break it all down, given a lat/long pair, a bearing, and a distance travelled on the surface of a sphere, what is the angular distance travelled?

At least, I THINK that's the question. Given the description of my issue, let me know if you think otherwise, and if there is any advice you would give.

##### Share on other sites
Wait. When you specify a "bearing", what does it mean exactly? I [and probably Sneftel] thought you were somehow specifying where you are facing initially, and that you then proceed to walk straight, following a great circle.

If this is not the case, you need to explain your problem better.

EDIT: Is this what you are looking for?

##### Share on other sites
Quote:
 Original post by RonHilerClearly if someone is walking a distance straight east for 512 units at the equator, they won't end up at the same longitude as someone walking that same distance near the north pole.
Stand five inches away from the north pole, facing east. Start walking. If you are walking straight, as opposed to walking in a teeny little circle, you will find that you are no longer facing east.

##### Share on other sites
No, it's definitely NOT a Rhumb line. Circling into the poles is definitely NOT what I want, hehe.

You guys might be right. On further reflection, I probably DO want to be walking along a great circle. There may not be a bug here at all. I'll have to do some testing involving graphics to make sure. Sorry, I'm having a tough time visualizing the pathing in my head.

##### Share on other sites
Assuming you care about shortest paths, you'll be going along great circles. Example: Tokyo is roughly due west of New York. If you get in a plane in New York, the plane will head northwest from New York, going in a straight line, passing over Alaska. By the time it gets to Tokyo, it'll be going southwest.

##### Share on other sites
Your equations are correct, you just need to figure out what your inputs actually are.

Latitude:
0 -> You're on the equator
+pi -> You're on the North pole
-pi -> You're on the South pole

Longitude:
Traveling in positive direction -> You're going east
Traveling in negative direction -> You're going west
Traveling (+/-)pi -> You end up on the other side of the world

Bearing:

In your algorithm 'bearing' only mean initial bearing. You will travel on a great circle starting in the direction of 'bearing'.

Also, normally when people talk about bearings, the positive bearing is heading east (clockwise), but since this formula is based om radians counter clockwise is the positive bearing.

The reason you end up in almost the same position is because you're going a pi/2 degrees, which is halfway around the world (not all the way as you said).

To figure out what happens do this (or imagine it):
1. On a ball, decide where the equator, north and south pole are.
2. Find a string (shoelace type, not char[] type :) )
3. Decide on a point on the equator. Find the point on the equator half way around the world towards east.
4. Measure the distance between the points with your string.
5. With the same string and distance, place it very near the north pole.
6. On that point, find the direction of east and go the same distance you measured before.
7. You will end up very close to the previous point.

##### Share on other sites
Quote:

Hmmm, my bearings range from 0->2pi, and they seem to work based on my tests (a bearing of pi/2 is east, a bearing of 3pi/2 is west), but otherwise I am set up the way you describe. My latitudes run from pi/2 (North pole) to -pi/2 (south pole) with 0 at the equator. My longitudes run from 0 to 2pi. And everything seems to go the right direction when I run them through the equations :)

You guys have convinced me that nothing is wrong, I freaked out over nothing, heh. Thanks.

One last question before I put this to bed. From time to time I get a value out of range. For the most part, the equations handle wrapping on their own, but every once in a while I get a result for the end longitude that is over 2pi. For example, this one:

Start Lat: 1.29554
Start Long: 4.95131
Bearing: 0
Distance: 50

End Lat: 1.53925
End Long: 8.0929

This seems to happen only when I start or end near the north pole (south pole doesn't seem to have the same problem). Is this just a rounding issue? Do you think I can just do a -2pi on the value and call it good if it ends up out of range?

So far I haven't been able to get the latitude to go out of range, or the longitude to go below range, only over it. But it may be I haven't tested it enough to hit those cases.

##### Share on other sites
since asin can result in values between −π/2 ≤ x ≤ π/2, latitude can't go beyond range.
The longitude can, since an addition is made. But you should fine if you move it back into range when necessary.

##### Share on other sites
Quote:
 Original post by ilmarinensince asin can result in values between −π/2 ≤ x ≤ π/2, latitude can't go beyond range.The longitude can, since an addition is made. But you should fine if you move it back into range when necessary.

Ah, good point, I should have seen that. Thanks, I think I am satisfied. Time to move on to the next item on the list.

Thanks a lot guys. As always, you've been a huge help to me.

##### Share on other sites

This topic is 3398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628647
• Total Posts
2984035

• 10
• 9
• 9
• 10
• 21