27

In this situation, it is trivial and a no-brainer. I'll be bold and not look at the whole code. I'll assume that the function in which this switch statement is (Game::updatestate()) is called once per frame. This switch statement being called once per frame is a droplet in the ocean. Typically, graphics rendering, physics and AI take up much more CPU time ...


27

In fact, switch statements are more efficient than if-else chains for one simple reason: Switch tables! (Also known as branch tables or jump tables). The simple explanation is that in a typical switch statement, each case ends in a break statement, so the compiler can figure out that exactly one path is going to be taken upon entering the switch. So then ...


11

In my experience writing collision detection, mesh-based collision (triangles versus other triangles) are the most expensive form of collision in physics engines (PhysX, Havok). Unity uses PhysX internally, so this is no different. Because each computer and platform perform differently, exact numbers cannot be provided, but generally speaking the relative ...


8

I think it depends on several factors: How big your game is If your game is small and can fit memory, it's much easier to load everything at once and keep it in memory. If your game is strctured as levels, load everything you need and keep it in memory (e.g. UI, main character) and load each level resources when you need it. The nature of the game Open ...


8

There is a surprising amount of confusion about how the compiler optimizes if/else chains vs switch statements. This is probably because there have been many changes over time in how compilers optimize these constructs. And even using the latest compiler versions, the answer depends on: what optimization level you are using, what values you are checking for,...


6

Instead of using LookAt, you can have the shader force the object to be rendered facing the camera. Thus, no script needs to be added to the tree, and the extra work the shader does is very little. Here's an example: Billboard Shader Also, since they don't actually move or rotate (as far as the CPU is concerned) and share the same material, you can turn on ...


6

A move which results in the own king being in check is already an illegal move. So allLegalMoves would already excludes any moves which end up with the own king in check by definition. If fact the FIDE Laws of Chess section 1.2 define checkmate as: The objective of each player is to place the opponent’s king ‘under attack’ in such a way that the opponent ...


5

I found usefull this algoritm for love2d engine (lua language) https://love2d.org/wiki/TileMerging -- map_width and map_height are the dimensions of the map -- is_wall_f checks if a tile is a wall local rectangles = {} -- Each rectangle covers a grid of wall tiles for x = 0, map_width - 1 do local start_y local end_y for y = 0, map_height - ...


5

Given that the rooms are procedural built, portals created and then populated, I have a couple of ideas. A* works really well on navigation meshes, and works hierarchically as well. I would consider building a pathfinding system that works at two levels - first, the room by room level, and second within each room, from portal to portal. I think you can do ...


5

I'm going to take a stab and recommend a hierarchical pathfinding algorithm, such as HPA*. Even though I'm not an expert in AI, I'm fairly confident in this guess because your generator sounds almost identical to the one in a game I'm working on, i.e. I've thought about this problem a bit too. HPA* (Hierarchical Path-Finding A*) is a method of optimising ...


5

I only have some mild knowledge in unity, but here are some suggestions I hope you find useful. Split the whole simulation into a few things: Large waves Medium waves Small waves Boat's reaction water Water's reaction to boat The first two can be done by moving a tiled displacement map over a plane mesh. You have a large, low detail map which represents ...


4

great performance saving comes from caching geometries and materials. In your case, create CubeGeometry and MeshPhongMaterial objects only once and then use them when creating tiles - each tile will be separate Mesh but all pointing to same instance of material and geometry objects. Then do the same for all resources that can be reused - models, textures ...


3

Your points seem reasonable. For static elements that are set up once and never change, you can certainly pack them into a GL_STATIC_DRAW buffer and keep it alive for as long as needed. If you still have to move these static elements around in the screen, then this approach is not very interesting anymore because each would need a transformation matrix that ...


3

enum is actually a good idea here, but what you want is for the enum to be: enum MapTileBuilding { defaultTile, homeBase, farm1, farm2 /*...and so on...*/ }; and a class class MapTile { MapTileBuilding building; ... //other members e.g. //structuralStrength //isOnFire //troopCount //incomeGenerated //etc. } ...which gives you the ...


3

Maybe enum is not the best choice in this case. You could try building a "tile" class and derive several subclasses from it (farm from your example). This way you could implement some sort of levelling system (instead of having to create a value for each level (farm1, farm2 etc)) inside the class and give the several kinds of tiles unique abilities in form ...


3

If you are looking to create destructible terrain, the way I did this in Unity, is to only set colliders on the edge blocks of your world. So for example, this is what you would like to accomplish: All those green blocks contain a collider, and the rest of them don't. That saves a ton on computations. If you destroy a block, you can activate the ...


3

You may want to have each region contain a list of particles which are in it. Then, when you move each particle, check if it is in a new region - add it to the new region's particle list, and remove it from it's current one (based on your checking above). That way you can just tell each region to apply the force to the particles in it's list (the work has ...


3

This depends on what operation you are performing on vectors. vectors are actually really fast. iteration is as fast as possible, since the vector does not store pointers but an array of objects in one straight block of memory, so iterating over a vertex is as fast as iterating over an array. And even the iterator-objects do not add a overhead, in production-...


3

If you already have SAT working, then you can pretty easily extrude an OBB along its linear velocity for a frame (i.e. create a swept OBB). The result will be a polyhedron, so you can use SAT with it. This only handles linear velocity, though, not rotational velocity. That's quite a bit harder because a rotating OBB sweeps out a volume that isn't a ...


3

Use neither, because both approaches involve loading and freeing GPU resources at runtime, which is slow and will lead to constant thrashing of video RAM. Instead what you should so is have knowledge of all UI resources needed at startup time, and load them all during program startup. Then when you need to use a resource for drawing, you just use the ...


3

Each product that exists in your world could be listed in an array or table in a database. Each product can then have a collection of Recipes. Instead of each Recipe actually containing a Product, the can simply reference the Products necessary to produce another Product. For example, let's say Cheese is made from Milk and Chemicals. :) All these things ...


3

Because switch statements work on only a single variable they can be optimized very easily compared to an if-else statement. The most naïve way to do so would be to determine the case using a binary tree. Of course, I doubt this is the reason for using switch in VVVVVVV. The amount of time saved is unlikely to be meaningful especially with branch prediction.


2

You dont want to constantly free things you loaded, once something is loaded keep it in memory, when its needed again, its already loaded in memory. In addition operations like file reading are expensive in themselves. Generally you should only Free something like that when you will never need it again. For example if I have a Mesh for a Boss monster, I ...


2

I forgot to come back to this question when I arrived at an answer, but this has gotten a decent amount of views over time, so better late (even 2 years late) than never! First, prerequisites: Prerequisites: First, one has to be able to calculate the time of collision (TOC) of two circles. Step 3 of this question Small, High-Speed Object Collisions: ...


2

Not clearing the screen can actually degrade performance, because on double buffered framebuffer, a copy has to be performed when you swap buffers, which is apparently more expensive than simply clearing the current framebuffer to some default color value. (see https://stackoverflow.com/questions/2538662/how-does-glclear-improve-performance for example) The ...


2

This is a broad question, so a broad answer is required. You can follow up individual implementations / algorithms yourself, and post separate questions if you need to. The basic approach for games Create some sort of grid in space. This grid can be either: flat (literally, a 2D grid or 3D grid) hierarchical (2D: quadtree or bounding box hierarchy, 3D: ...


1

After some consideration I'm completely changing my previous answer. It would be more effective to implement a OnCollisionEnter method on the player that will do logic when a rocket hits him. That way less collision will have to be calculated and the physics engine will only have to keep track of the position of the rockets colliders and the rockets ...


1

I'm not familiar with LibGDX or openGL so I can't provide code for you, but I believe what you are wanting is possible and called instanced drawing. Essentially, you'll bind 2 (or more) vertex buffers: Vertex buffer - A unit square (0,0) (1,0) (0,1) and (1,1) Instance buffer WorldMatrix + Color for each instance Using the appropriate instanced draw ...


1

Here is a working test class displaying 10,000 rectangles with random colors. The render loop takes <1ms. private ShaderProgram shaderProgram; private Mesh mesh; @Override public void create() { int numberOfRectangles = 10000; int numberOfVertices = 4 * numberOfRectangles; mesh = new Mesh(true, numberOfVertices, numberOfRectangles*6, ...


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