Advertisement

How to spawn objects near players?

Started by May 02, 2014 04:53 AM
5 comments, last by bernatripoll 10 years, 6 months ago

Hello!

I have a database with all the objects (static) that have to appear in every player world. My first approach is when one player is, let's say, 30m from an object, the server sends "hey player, there's an object at x,y,x" and the player client adds it to the world.

But this means that the server has to calculate the distance between every player and all the objects in the database (array list in memory) in every snapshot (at least 2 times per second) and I think that would melt the servers...

Any better approach?

Thanks!

You could have your static objects on a grid. That way you can just iterate through all the grid cells that are within 30m, and query the grid about which objects it contains. To implement it efficiently you could:

- Only worry about updating a player's static object list when he moves from one grid cell to the next.

- When a player moves from one grid cell to the next, only worry about the grid cells that are included now and not included before (so assuming the player isn't moving very fast, it should only be a single line of cells at a time (or a square of cells if we're talking 3D)

- If you have large sparse worlds, then instead of having some crazy 100000x100000 grid which is mostly empty cells, you could have a 1000x1000 grid in memory, and objects can put themselves in an appropriate grid slot by calculating iGridSlot % 1000, so an object at grid slot (64363, 25783) would be in grid cell (363, 783) in your structure.

Google spatial hashing.

Advertisement

Look into spatial datastructures, such as grid partioning, quadtrees, spatial hashing, etc... that let you answer the "what is the closest object to the player" question efficiently. Also consider that maybe the server could give more information to the client so that it can figure this out for itself, instead of the server doing absolutely everything.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Simplest is a grid (voxel in 3D) representation. It's easy to compute, easy to query, very fast. It take comparatively more memory. Spatial hashing is kind of the same principle, with better memory footprint.

There are other spatial partitioning structures. Physics engines use them all the time, on 1,000's of interacting and static objects (check out NextCarGame / BeamNG).

The other common strategy is using portals / PVS (Potentially Visible Set). You build a list of areas that are potentially of interest from another area.

Everything is better with Metal.

As other said, you need a spatial indexing structure, the good news is that nowadays, a lot of database vendors (SQL & noSQL) support spatial indexing smile.png, whether in memory or on disk.

You could probably use one of these out of the box implementations, and perform spatial queries on them to get the list of players to be notified when you move an object. The main drawback being that what you really want is not "querying" but rather be "notified" when you move objects around. Something that Elasticsearch does through its percolation feature... But I really don't know if the kind of load and latencies you expect are compatible with this tool. I didn't experiment the thing yet, sadly.

At the end, I think the best solution for you depends on these factors:

- You require really low latencies and a lot of queries, but persistence and dataset size is not the core concern: Build your own grid memory based system.

- Your main concern is the dataset size and you don't need to put everything in the process memory: an existing spatial database could possibly do the trick, with a little experimentation.

the good news is that nowadays, a lot of database vendors (SQL & noSQL) support spatial indexing smile.png, whether in memory or on disk


You do not want to base a real-time spatial query on a SQL database implementation. You don't want to base *any* real-time information on direct queries to persistent databases.

This is why we have game servers, as separate from application servers or database servers. The game servers keep a game-specific, "reified," up-to-date in-RAM copy of the game objects and can enforce the rules and update the world state very cheaply. Things that really matter go back to the persistent store in an asynchronous manner. If the game server crashes, you may lose some amount of progress, but that's hopefully rare, and the orders of magnitude of efficiency gains are worth it. The kind of real-time gaming experience you can deliver from an in-RAM custom server compared to what you can deliver AT ALL from a persistent server (much less can deliver with economics that make sense) make it "not a choice."

How often do the objects change location or otherwise mutate? Most games pre-install a "level" that contains the object locations, and update this through "patching." If that's too static, then can you send all the objects within a kilometer when a user connects, and just update this when the user moves more than half a kilometer?
enum Bool { True, False, FileNotFound };
Advertisement

Hey! Thank you all!

I've implemented this with a basic quadtree dividing the world in zones, and each zone with a quadtree :)

Video demo link!

https://mega.co.nz/#!qFwTUJTb!s5PDqdCB9WZfWzsOsrG-b1t6G_FMGoEkAbVfuiCqdoQ

:)

This topic is closed to new replies.

Advertisement