Topic: Silly math...move a marker at fixed speed towards another marker

20 years ago this would have been trivial for me, but I can't wrap my head around what I think is a pretty easy scenario:

I have two types of markers: city markers, and hireling markers. Hireling markers can move (from city to city, or to other locations), and their position is controlled directly by the server, with periodic updates sent down to the client.

This means that every time an update comes down from the server, the hireling marker "leaps" to the new position (long/lat coords).

On the client, I would like to give the hireling marker a small velocity towards the city marker it is moving to, so that I don't have to send so many updates yet still have the marker move more smoothly.

Obviously, markers don't have underlying physics, so I'm more than willing to use observe() to update the marker's position every few seconds to simulate smooth movement...but that's where I'm stuck.

The examples (animating a marker, smooth move of the camera to a marker) all use lerp, but unfortunately don't seem to indicate how to control the velocity of the marker directly with a rate (for example, 5 km/hour). Additionally, I strongly suspect that lerp will fail across lat/lng sign changes (equator, prime meridian), sending my marker off into semi-random space.

On my server, my geocoder implementation has a utility called endpoint, which does the following (the language is Ruby, but should be relatively easy to understand):

    # Given a start point, heading (in degrees), and distance, provides
    # an endpoint.

def endpoint(start, heading, distance, options = {})
      options[:units] ||= Geocoder.config.units
      radius = earth_radius(options[:units])

      start = extract_coordinates(start)

      # convert degrees to radians
      start = to_radians(start)

      lat = start[0]
      lon = start[1]
      heading = to_radians(heading)
      distance = distance.to_f

      end_lat = Math.asin(Math.sin(lat)*Math.cos(distance/radius) +
                    Math.cos(lat)*Math.sin(distance/radius)*Math.cos(heading))

      end_lon = lon+Math.atan2(Math.sin(heading)*Math.sin(distance/radius)*Math.cos(lat),
                    Math.cos(distance/radius)-Math.sin(lat)*Math.sin(end_lat))

      to_degrees [end_lat, end_lon]
    end

Is there anything similar in OnlineMaps? I've searched through the OnlineMapsUtils.cs and some of the pieces seem to be there, but I can't quite figure out how to get it working together.

Any ideas or suggestions?

Thanks in advance...

Re: Silly math...move a marker at fixed speed towards another marker

Hello.

Lerp will work fine when moving over the equator and the prime meridian, but it fails when the longitude goes from 180 to -180. You need to keep it in mind.

In the example «MoveMarkerOnRouteExample», shows the smooth movement of the marker along the route.
http://infinity-code.com/atlas/online-m … ample.html
You can calculate the endpoint in unity, and to adapt the code from the example for your application.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

3 (edited by stephen 2016-03-31 02:39:14)

Re: Silly math...move a marker at fixed speed towards another marker

Good to know about lerp.

I've already looked at the examples like I noted above--they don't document how to set the velocity, which is what I'm trying to figure out smile

I'll play around with that to see if I can get it figured out, but will probably wind up going with endpoint--I've used it before and it does do what I need since it's what I'm using server side.

Thanks for the info.