Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

How to spawn objects near players?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 bernatripoll   Members   


Posted 01 May 2014 - 10:53 PM



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?



#2 C0lumbo   Members   


Posted 01 May 2014 - 11:11 PM

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.

Edited by C0lumbo, 01 May 2014 - 11:11 PM.

#3 Bacterius   Members   


Posted 01 May 2014 - 11:13 PM

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.”

#4 0BZEN   GDNet+   


Posted 02 May 2014 - 07:06 AM

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.

Edited by 0BZEN, 02 May 2014 - 07:09 AM.

Everything is better with Metal.

#5 JMDeruty   Members   


Posted 02 May 2014 - 08:34 AM

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.

Edited by JMDeruty, 02 May 2014 - 09:24 AM.

#6 hplus0603   Moderators   


Posted 02 May 2014 - 10:28 AM

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?

Edited by hplus0603, 02 May 2014 - 10:30 AM.

enum Bool { True, False, FileNotFound };

#7 bernatripoll   Members   


Posted 03 May 2014 - 10:10 PM

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!




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.