How are video games programmed with the possibility of the playable character being able to go anywhere on the map without crashing the game?

501 views

How are video games programmed with the possibility of the playable character being able to go anywhere on the map without crashing the game?

In: Technology

3 Answers

Anonymous 0 Comments

Former game developer here,

There’s a big 3D coordinate system, and in it, you have planes, surfaces, and volumes. A plane is just some 2D flat, that expands unto infinity and can exist at any position and orientation. A surface is basically a 2D triangle in this space – unlike a plane, this surface has a boundary defined by it’s three points; we call them polygons. A volume is composed of polygons and is convex. There is also the concept of spheres, a volume which is a position and a radius, a ray, which has a starting point and a direction – conceptually it continues unto infinity, and lines, which has a start and stop point.

So now that we have some simple primitive constructs in a 3D space, we can perform some primitive physics on it: collision detection. The idea is to determine if any two things intersect. Your in-game character is represented by a simple cube. The game logic says, “here are a bunch of surfaces, let’s call it the floor. We know which side of these surfaces is top and bottom, and you can exist anywhere on top.” When you run into a wall, we determine if your box intersects that boundary or even surpasses it completely. We do some math and your character is positioned at the boundary. All this happens before we render a frame.

So there are boundaries all over the place, and anything that moves gets checked to make sure it remains in the bounds. You’ve certainly seen in games where you fall through the world. Of course that’s not supposed to happen, and there are shortcuts we take to reduce computation, and once you break that, sometimes you can sail clear into the void, as it were. Games have gotten better over the years.

Anonymous 0 Comments

Can you explain the question a bit more? Are you asking about how games are coded to account for a lot of possible places/situations a player can be in without making an impossibly long list of every scenario?

Anonymous 0 Comments

Really simplified:
They work very hard to make sure the player can only go where they’re intended to.
Sometimes, there are bugs or oversights that let players go “out of bounds” and there, many things can happen such as the game crashing.
The most common type of bound on a map is the famous “invisible wall”, but some games try a different approach by teleporting you back inside the map with a loading screen if they detect that you’re outside the map.
Performance-wise, not all the map is loaded everytime. Usually maps are designed in chunks or “blocks” and they load the next zone while you’re travelling your current one. And they unload the unneeded zones once you travel far enough from them. That’s typically why you’ve got loading times when fast travelling from one point on the map to another : the game unloads your current zone (npcs, items etc) and loads the one you’re teleporting into.

If you’d try to keep the whole map loaded at all times, with all NPCs, items etc, you would quickly end up having your RAM full, in which case if you try to load anything more, the game won’t be able to and will most likely crash.

Games try very hard to balance their memory use by constantly loading and unloading stuff. That’s why open world games tend to have an average amount of RAM used, they very cleverly load/unload stuff in your RAM as you need them or not. You typically will never see a game like cyberpunk taking your whole ram, otherwise it would most likely indicate what’s called a memory leak : the game “forgot” it had loaded stuff so it will never unload it, and after some time, your RAM will be full, the game won’t be able to load anything more, and will most likely crash