How to implement a location based player finder

In this post I’ll show you how you can use the GameSparks platform to implement a location based player finder feature in your game.

There are times when you may want your game to be able to find players which are in the nearby geographical location of the current player.  For example

  • Suggesting location based clans
  • Gamification applications
  • Physical to virtual reality crossover games

In this article we will see how to store player location data in a collection within the GameSparks platform and then query that data using geospatial query operators.

To register for a GameSparks account please visit https://portal.gamesparks.net/register.htm.

Configuring the Game Events

First create an event that the game will use to register a player’s current location.  This call would be made on a regular basis whilst the player’s session is active in order to keep their location in the database current.

store-loc-event

Next create an event that the game would use to query for players that are near to the current player and have registered their location recently.

near-me-event.png

Configuring the Cloud Code scripts

We now need to bind a cloud code script to the STORE Event that saves player location data in a MongoDb collection.

Notice that as well as storing the player location data we also need to ensure that there is a geospatial index on the MongoDb collection, ‘player_location’, that we are using.  A timestamp value is recorded with the location data so that we can filter out stale records in our query.  We could also have used Mongo’s ‘Time To Live’ collection feature but for this article we’ll keep it simple.

Next we bind a cloud code script to the NEAR_ME Event that will allow the game to query  the player location data in order to find players who are within a given number of kilometers of the current player..

 Execution of the configuration

Register three users.

Log the locations

Next, authenticate with each player in turn and log the STORE event for each passing in a longitude and latitude value for their positions.

map

York Player

Leeds Player

Sheffield Player

Query for players close to Player One

Now authenticate again with the York player, player one.

Next make the query by logging the NEAR_ME event with the distance set to 70km and a time of 15 minutes (longer if you called the STORE event some time ago).

All there player records are returned by the query with the distance set to 70km.

Next make the query by logging the NEAR_ME event with the distance set to 36km and a time of 15 minutes (longer if you called the STORE event some time ago).

Only the Leeds and York player records are returned by this query.

Further Reading

In this example we’ve used the $nearSphere query operator to perform the search which returns geospatial objects in proximity to a point on a sphere.  MongoDb also provides several other operators which you should ch check out such as:

  • $geoWithin – selects geometries within a bounding GeoJSON geometry
  • $geoIntersects – selects geometries that intersect with a GeoJSON geometry.
  • $near – returns geospatial objects in proximity to a point on a flat 2D plane

More details on MongoDb’s excellent geospatial query features can be found here http://docs.mongodb.org/manual/reference/operator/query-geospatial

For the GeoJSON format specification, see http://geojson.org/geojson-spec.html.

  • سینا مرشد سلوک

    Hi!
    : )
    A small code typo discovered where the $maxdistance have to be within the parentheses prior to the stated:

    var query = {
    loc: { $nearSphere : { $geometry : {
    “type” : “Point”,
    “coordinates” : [longitude, latitude]
    }, $maxDistance : maxDistance
    },
    },
    timestamp : {$gte : timeStamp}

    };

    var result = geoLocCollection.find(query)

  • Simon Flandre

    Hi !
    Thanks for this great article, it s very helpful !
    I d like the response to contain the calculated distance, I saw online there is a way to do that with aggregate and $geoNear. But I don t understand how to use aggregate.
    Could you please provide an exemple on how to use those ?

Who uses GameSparks?