I'm trying to make a fully-automated game engine which needs to be working based on the events. The problem I'm facing is that I've made a map class and it's initialized in the game class. It's almost static and only one map exists for a game. New maps are loaded by clearing the objects in the current map and adding new maps. You can see the source of the map class here.
http://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/map/Map.java
The main problems comes in the collision detection, where I'm using the brute-force collision detection where I should not. This slows down the game a lot and I wanna check the collisions only for objects which are nearer to the object. I've been using the MapLoader interface to construct the maps. I'm thinking that calling the collision() method of the objects in another thread might help. How-ever it all the map objects are updated in the Game class.
Here's the game class if in case it might help
http://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/core/Game.java
There's another problem that some-times, the objects aren't destroyed. I'm calling the map's removeObject() method but it takes a 1-second delay and some times wont remove at-all.
It gives me 48-64 fps in a platform game with 158 objects in the game. But in a space-invaders style game, it gives me only 20-30 fps. Any advice on optimization is greatly appreciated...
If anybody could get me a tutorial for binary spacing etc., I would be thankful.
Two suggestions by looking at your code: first thing is that you should try to minimize object allocation in collision detection, don't create new Rectangles, work on data you already have by writing directly the collision detection algorithm.
Second an more important thing: a collision detection engine should work by using two levels:
a coarse level of collision already excludes objects that are surely too far to collide (you can use many techniques here, like a binary spatial partition algorithm of big colliding blobs, or hierarchical structure of objects)
a fine level which will compute specific details for the collision that can occur, with more precise algorithms
Related
I am making a voxel-based game where large structures made of voxels can collide with each other. The game runs smoothly until two structures get to close to each other where it quickly drops to about 3 updates/second.
My current solution to adding these colliders to JBullet is by using a compound shape and using a greedy-meshing algorithm to make larger colliders out of adjacent blocks. While this is a drastic improvement over each voxel having its own collider, it still isn't fast enough to simulate large structures colliding.
I did a bunch of research and it seems that octrees are the only way to make this run in real time, but can't figure out how to add support for them in JBullet.
To clarify, I know why my game is running slow. I have a lot of different objects in the current area and it has to tick and render all of those objects. I just don't know how to fix the problem without just making less objects.
The answer I am looking for is more of a concept of how I can go about fixing this problem rather than just making a bunch of code for me to paste into my game.
I am designing my games based off of tutorials by RealTutsGML. There where some issues I had to work around with his method of building games, but I figured them out.
So every tick in my game, I have to look through all of the objects that currently exist. The more objects that exist, the longer it takes to process all of them. I need to find a way to help free up memory if those objects are currently not in view, for example. I know games like Minecraft use chunks to free up unused memory. (Blocks outside of the view distance are not generated) What can I do to allow for an environment with many objects without causing so much lag? I want to be able to have a big level without having so much lag from all the objects that have to be ticked and rendered.
Another thing that I will clarify is that all of the objects loaded into the levels are held in a LinkedList so that I can easily create and destroy objects. Every tick, I run a for loop through those linked lists to process every objects behavior and how they are rendered.
[EDIT APRIL 28]
The objects in the game I was working on are organized in a very grid-like format. So that includes the tiles, the player, and all of the other game objects.
You haven't given too much information about your game (I'm not going to look through the tutorial either). You may want to give more background information, and maybe some code snippets.
I know one thing about your code with certainty: you are using linked lists. Linked lists, especially when you add and remove things from the middle, are slow. The reason for this is memory (or cache) locality. When they say computers are growing exponentially faster, they meant the processor is. Your data needs to be transported from its home in memory to another location to a place where it can be used. When data is needed, it is transported by a bus, which also brings neighboring data. (Note that "bus" is actually the technical name for the component.) Linked lists, especially how you're using them, manipulate data in a way that destroys neighborhoods of data. As a result, the bus essentially becomes a "taxi", getting data one at a time. And the bus, according to the graph, is a stunning 10x faster than the computers of the 1980's (remember, the graph has an exponential scale).
Also, it seems to me like you probably don't need to tick EVERY object EVERY frame. Yes some objects, like mobs, will need to tick every frame (if they are close enough to be active). From what I assume your game looks like, you have each block of grass being its own object and ticking every frame. Stop watching the grass grow.
MineCraft, for example, will only tick sand blocks when a neighboring block changes (which is why sand generated in the air will only fall when disturbed).
You may want to check these pages out:
Question about memory locality.
Question about linked lists and memory locality
Site explaining cache locality, and source of picture.
Question about slow graphics loop.
Code Review is a good place to get feedback on your code.
Game Development will give more game-based answers.
In an organized environment like such, I think it would be very obvious that making chunks(holds tiles/game objects) and mega-chunks(holds chunks, chunks were still running slow) would be a clear solution especially in an organized environment like this. Like someone on here said, clearly not everything needs to be processed at once and not even exist all at once.
Here is how I see chunks being useful.
I tried this, but didn't see much of a difference, so I am going to try making chunks to hold chunks and hopefully that will help.
I have been working on a voxel game for some time now, but all that I have really accomplished was the main menu and an Item system. Now its time to make the voxel engine. I have been searching for a while now to find some tutorials or an ebook that will teach me such, but the best i could find were someones tutorials in c++, but I am making mine in Java. I have dabbled in c++ and c# in the past but it was too difficult to translate i.e. it relied on a class that java doesn't have. What I know is that there are different methods for voxel engines, they all begin with rendering a single cube, and Perlin and Simplex noise can be used to randomize terrain generation.
If anyone could point me in the correct direction, most appreciated.
I will be checking back at least once a hour incase someone feels this thread is dead.
I'm not entirely sure what you are asking, if you are asking how to make simplex noise, implement it in a voxel engine or how to start making a voxel engine.
If you are asking how to start making a voxel engine I would recommend practising with quads first (2D version) and focus on getting an understanding for the theory. Once you are happy with your understanding you should focus on the voxel class (one cube) - it is very important to learn as much as you can from it, and then add more so you can optimize rendering as much as you can, such that hidden faces are not rendered and even vertices are shared, voxel engines can be the most wasteful renderers if not optimized!
EDIT:
Optimization can be done through many methods, The first and most important is hidden face removal, this involves removing the faces of voxels that are touching which will mean you will need to check of a voxel exists on any given side of any voxel before rendering that face (e.g before rendering the left face, check if there isn't a block to the left). Next is the rendering method, do not render each face or each group individually, group them so they can be rendered faster, this can be done by using display-lists or the more technical VBOs, these ensure the data is in the GPU or the data can be given to the GPU faster, For example Minecraft groups them in chunks of huge 16x16x128 groups and uses display lists. If you really want to reduce every single vertex in memory you can also consider using strip drawing methods (in OpenGL), these will require you to define certain vertices at a certain time in rendering but allow you to reuse a vertex for multiple faces.
Next would be understanding simplex noise, I can relate to there not being much material online for noise generation algorithms, unfortunately I cannot link material that I used as that was years ago. You can implement your noise algorithm in the 2D version to prove it works in a simpler environment and then copy it to the voxel version. Typical usage would be to use the values as heights in the terrain (e.g white=255 = 255 high).
I would recommend using Unity. The engine is already made and you can add menus and titles with just a few lines of code. All of the game creation is either in C# or Javascript which shouldn't be any huge change from C++. Good luck!
I'm writing a 2d Top-down RPG using the LWJGL alongside with Java 1.6. By now, I have the rendering and input methods working fine and just started to program the game's logic.
So I have a class called World, which holds an ArrayList of Entities. I want to implement simple collision in the game (using intersecting squares), which should be no problem. The only problem I now have is how to access single cells of my List without having to iterate all over it. I have only been able to come up with collision methods which are executed inside each Entity and iterate over all of the entities in my World. That is not fast at all, but I really don't know what to do in order to make it faster.
My game is tile based, but the movement is not tile-to-tile, one can walk smaller portions, which avoids me to simply use a two-dimensional array...
Is there a standard way to handle entities and their collisions? (or maybe a way to handle collision between entities which are located inside an ArrayList?)
The standard way to handle colliding entities is by space partitioning. Your world is a 2 dimensional plane made of discrete points.
Each piece can be located on one of the points. The amount of the points determines the resolution of the plane- the more points, the better visual effect, the more calculations you have to perform. This is the trade off.
You can maintain a map between the location of an entity and the entity itself, where the location is represented by an object that overrides equals and getHashCode. The location object contains two members- the X and Y coordinates.
Note- you have to override in a proper and efficient manner.
So you you can access each entity very quickly if you know its coordinate, reshuffling is simply removing an entity and adding it with new coordinates (this can be optimized for locality) and collision detection is trivial- just check whether the same location is occupied by a certain entity.
I would also refer you to this question on SO.
I'm being really ambitious and working on a 2D Shoot 'em Up game that will have, hopefully, hundreds of entities running around.
What I'm having trouble wrapping my brain around, is how the bullet will detect when it makes a collision with an object, without it checking for every object on the map. The reason is that I feel that if I have four dozen bullets on the screen, each checking for collision with every entity on the map, every cycle, I will see some fairly significant performance loss.
So what would be the best way to detect for collisions without checking every single entity?
I can handle the collision algorithm when I have my two objects, I just can't seem to find a way to get those two object to see each other without checking everyone else first.
I'm working in Java and OpenGL with (soon to be textured) QUADS.
You should investigate quadtrees; they're often used for efficient 2D lookup.