The Stack Overflow podcast is back! Listen to an interview with our new CEO.
237

Preamble: A few objections have been raised in the comments, and I think they largely stem from a misunderstanding of what we mean when we say "premature optimization" - so I wanted to add a little clarification on that. "Don't optimize prematurely" does not mean "write code you know is bad, because Knuth says you're not allowed to clean it up until the ...


155

Figure out your bottleneck A few hundred moving objects should not be a problem. However, a few hundred colliding objects might be. It will take some testing, yet I believe the bottleneck you have are the collision. By the way, about how many objects are we talking? What is the platform? A hundred objects for a mobile game is not the same as a hundred for ...


91

Engineering for Performance Follow vendor recommendations. Use the correct data structures. Implement the correct usage patterns. Don't do anything stupid. Optimization When already written code is running slow, measure it, find out why, implement what is required to make it fast. Premature Optimization Make assumptions about what is fast or slow ...


78

I can certainly see why you would think that it would be hard to simulate those, but there are enough constraints on bullets (all projectiles, really) to make them easier. They are generally simulated as a single point, instead of as something with volume. This makes collision detection significantly easier, as now I only need to do collisions against ...


71

First of all, that game mechanic looks like it could be super fun, so congrats on thinking of it! Here are two simple solutions that could be used independently or together to solve this particular issue: Remove Very Small Pieces There isn't much of a point to keeping around tiny pieces of collision that really won't impede the player or have any real ...


49

By simply only loading those parts of the world into memory which are close to the player. Anything else is suspended to hard drive. When there is a tiny object laying around two kilometers away, then the player can not see it and can not interact with it. So there is no reason to update it or send it to the GPU for rendering. The smaller the object and its ...


45

Some ideas on avoiding searches that result in failed paths altogether: Island ID One of the cheapest ways to effectively finish A* searches faster is to do no searches at all. If the areas are truly impassible by all agents, flood fill each area with a unique Island ID on load (or in the pipeline). When pathfinding check if the Island ID of the origin of ...


45

Probably one of the most efficient ways to implement bullets is using what is known as hitscan. It is rather simple in its implementation - when you fire, you check to see what the gun is aiming at (possibly using a ray to find the closest entity/object/mesh), and then you 'hit' it, doing damage. If you want to make it seem more like an actual, fast moving ...


45

But my impression is that in game development, unless you have a reason to do otherwise, everything should be nearly as fast as possible. Not necessarily. Just like in application software, there is code in a game which is performance-critical and code which is not. If the code is executed several thousand times per frame, then such a low-level ...


43

There are couple of things you can do to increase drawing performance. You said they were pretty far away. You could use LOD to decrease the vertex count of those trees, and thus decreasing time required to go through all the vertices being drawn. Even though this is most likely not the issue at hand (GTX1080 with just 10k trees with 200 tris each, puny ...


42

note: this answer began as a comment on DMGregory's answer, and so doesn't duplicate the very good points he makes. "Would it not be incredibly difficult to change some of the core structures of the game at the end, rather than developing them the first time with performance in mind?" This, to me, is the crux of the question. When creating your original ...


41

Yes, this is normal for a real-time game to try to use 100% CPU to perform as fast and good as it can. So that player sees as much frames per second or as good physics simulation or anything else as his PC can provide. In your case - No, this looks like an inefficient design, to take a thread and make it poll for events in a loop (while true do ...


35

It will cause one CPU core to always run on 100%. This usually doesn't cause any harm to the system. CPUs are designed to run on 100% for hours. But on a mobile device it will drain the battery quickly and heat up the device, which will likely cost you about a stars in your store ratings. On a desktop computer this is less of a problem, but it will consume ...


34

I know you do not conceptualize this as collisions, however what you are doing is colliding a circle centered at the creature, with all food. You really do not want to check food that you know is distant, only what is nearby. That is the general advice for collision optimization. I would like to encourage to search for techniques to optimize collisions, and ...


29

Console: static hardware that never varies across every single iteration. Home PC: hardware that changes from day to day, with a million different chip designs. Console: closed system that lives in its own, secure environment from birth to death. Home PC: wild west, barroom brawls and your OS is the sheriff keeping everyone from getting shot. Carmack: ...


29

If you need something that stays linear over any distance (unlike distance^2) and yet appears vaguely circular (unlike the squarish Chebyshev and diamond-like Manhattan distances), you can average the latter two techniques to get an octagonally-shaped distance approximation: dx = abs(x1 - x0) dy = abs(y1 - y0) dist = 0.5 * (dx + dy + max(dx, dy)) Here is ...


27

This would depend on the game and the indexing structure used for the chunks. Though, at such a high level, it's not too likely it has much to do with memory or a specific performance enhancement. More than likely it's an arbitrary decision for sizing chunks in a predictable way. It allows for some counting and indexing tricks using bit shifting that wouldn'...


26

TL;DR; Your problem is not with performing the distance function. Your problem is performing the distance function so many times. In other words you need an algorithmic optimization rather than a mathematical one. [EDIT] I am deleting the first section of my answer, because people are hating it. The question title was asking for alternative distance ...


26

AStar is a complete planning algorithm, meaning if there exists a path to the node, AStar is guaranteed to find it. Consequently, it must check every path out of the start node before it can decide the goal node is unreachable. This is very undesirable when you have too many nodes. Ways to mitigate this: If you know a priori that a node is unreachable (e.g....


25

Agner Fog's optimization guides are excellent. He has guides, tables of instruction timings, and docs on the microarchitecture of all recent x86 CPU designs (going back as far as Intel Pentium). See also some other resources linked from https://stackoverflow.com/tags/x86/info Just for fun, I'll answer some of the questions (numbers from recent Intel CPUs)....


24

"Do not optimise early" doesn't mean "pick the worst possible way to do things". You still need to consider performance implications (unless you're just prototyping). The point is not to cripple other, more important things at that point in development - like flexibility, reliability etc. Pick simple, safe optimisations - choose the things you limit, and the ...


23

Is there a substantial overhead to allocating / deallocating VBOs (I mean the mere act of setting up a buffer)? Define "substantial." It is generally wise not to create them in the middle of frames; they should be set up during initialization or wherever. But this is true of most OpenGL objects, like textures, renderbuffers, or shaders. If I'm updating ...


23

texelFetch is quite different from texture. texture is your usual texture access function which handles filtering and normalized ([0,1]) texture coordinates. texelFetch directly accesses a texel in the texture (no filtering) using unnormalized coordinates (e.g. (64,64) in the middle-ish texel in a 128x128 texture vs (.5,.5) in normalized coordinates).


22

If you want to do optimization at the right times, have slow machines and use them. For a small shop, a good option is to use a slow laptop on the commute and a fast desktop in the office. As an additional benefit, if you're a one man shop this also forces you to properly back up the entire build environment. By using a slow machine you'll know when you ...


22

You have two very different things to manage: The server must manage the entire world, in an authorative manner. For that, communication with N clients (where N is "massive") is necessary. The client could, in principle, know about the entire world, but it needs not. For the client, it is sufficient to know about what's nearby the player. Assuming for ...


21

Sometimes this question can arise not because of the cost of performing distance calculations, but because of the number of times the calculation is being made. In a large game world with many actors, it is unscalable to keep checking the distance between one actor and all the others. As more players, NPCs and projectiles enter the world, the number of ...


21

Run a dual A* search from the target node in reverse as well at the same time in the same loop and abort both searches as soon as one is found unsolvable If the target has only 6 tiles accessible around it and the origin has 1002 tiles accessible the search will stop at 6 (dual) iterations. As soon as one search finds the other's visited nodes you can also ...


20

I like to think of performance in terms of "limits". It's a handy way to conceptualise a fairly complicated, interconnected system. When you have a performance problem, you ask the question: "What limits am I hitting?" (Or: "Am I CPU/GPU bound?") You can break it down into multiple levels. At the highest level you have the CPU and the GPU. You might be CPU ...


18

The way OpenGL works, whenever you use non-VBO data, the driver has to make a copy of it - in practice creating a temporary VBO - since nothing stps you from modifying your user-space naked arrays between calls to OpenGL. There may be some driver-side trickery to make the temp allocation faster, but there's nothing you can do to avoid the copying. So yeah, ...


18

Luckily, as you pointed out, the COMPACT Mono builds use a generational GC (in stark contrast to the Microsoft ones, like WinMo/WinPhone/XBox, who just maintain a flat list). If your game is simple the GC should handle it just fine, but here are some pointers you might want to look into. Premature Optimization First make sure this is actually a problem ...


Only top voted, non community-wiki answers of a minimum length are eligible