Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 Jan 2011
Offline Last Active May 27 2016 03:33 PM

#5264102 Is this loop any good?

Posted by on 29 November 2015 - 09:40 AM

For me this looks like a main loop doing rendering (you call each iteration for frames) and logic. 
The loop you made is not deterministic, which means how long your 60 frames takes, depends on how long your logic takes to execute, and will differ from system or amount of work.
But that is without knowing how you have setup your rendering API, in other words I am not sure your frames is bound to the screen refresh rate. If VSYNC is enabled(should be to prevent screen tearing or jiggered pixel movement), your loop could be rewritten to something like: 

stopwatch frameTime;
setSwapBufferInterval(1); // Swap buffer by an interval of one frame. 
  swapBuffer(); // Will block until it is time to swapBuffer based on screen refresh rate.
  std::cout << "FPS: " << 1.0 / frameTime.seconds << std::endl; // You should probably throttle FPS printing, by for example taking average FPS every sec.

Beware that there exist screens with 120 hz (and more than that too), which may not be suitable for your physics engine or game logic (too low precision per timestep etc.).
Also having a dedicated rendering thread is what is recommended today. There are unfortunately no standard way of doing it, but there exist per platform solutions. On OS X and iOS you have something called CADisplayLink (CVDisplayLink on OS X). On android you have something called Choreographer. In browser you have requestAnimationFrame. On Windows you make your own thread and use https://msdn.microsoft.com/en-us/library/windows/hardware/ff547265(v=vs.85).aspx from GDI+ (Only works on Vista and upwards).

So now you need to sync rendering and logic, since rendering happens in its own thread, and event handling and other things on the main thread. This can be done by simple communication (in hardware) between your main thread and rendering thread. For example by using semaphores.
Also all the per platform methods above gives you the current frame duration, which is very accurate and can be used later in your time independent code to do proper physic integration or movement. 

I guess this may seem a bit daunting to you, however I have seen a lot of weird solutions to this problem, and in my experience the best way to make a main loop is by reading the docs for your target platform.

#5100977 How to create a physics engine

Posted by on 13 October 2013 - 04:12 AM

If you want to make a physic engine, that may be used in a real project, you should hardware accelerate your physic engine for the GPU, by OpenCL or just by OpenGL shaders( a lot harder though, and more hackish...). That's what makes PhysX and Havoks performance so good. 

As people have pointed out, the method and algorithm used in a physic engine is common across the engines. And as L. Spiro said, Real-Time Collision Detection and Game Physics Engine Development are very good books(I have both). So it is up to you, how you gonna implement the engine, however you should have a decent understanding of geometry and physic laws, that you may recall from high-school. Another thing, it takes time to create a physic engine, so it should only be made for learning purposes or as part of your CV.

#5094262 Opengl ES on Desktop

Posted by on 15 September 2013 - 11:47 AM

Yeah, you can use OpenGL ES on desktop computers. However it is both platform and GPU dependent approaches for this to work. If you are using linux, just download the mesa-gles drivers, or even better find the vendor specific drivers for GLES.

I suggest to use the SDK from the GPU vendor on windows, which is http://developer.amd.com/tools-and-sdks/graphics-development/amd-opengl-es-sdk/ for AMD and https://developer.nvidia.com/tegra-resources for Nvidia.
Look here for installation instructions for Nvidia emulator: http://developer.download.nvidia.com/tegra/docs/win_es2emu_pack_v100.pdf


You may also look at http://www.imgtec.com/powervr/insider/sdkdownloads/index.asp for a platform independent solution.


Good luck, and remember I have yet not found a 100 % stable solution for my hardware setup, so don't expect to much, and happy debugging ;) 

#5067297 C++/SDL Game Design Guidance

Posted by on 04 June 2013 - 03:00 AM

Why not use the existing tile system as a grid?
Here is a snippet that maybe useful:

void DrawTilesWithinFrustum(Vec2f worldPos, int windowWidth, int windowHeight)
    Vec2f minIndex = worldPos / Vec2f(TILE_SIZE_X, TILE_SIZE_Y);
    Vec2f maxIndex = (worldPos + Vec2f((float)windowWidth, (float)windowHeight)) / Vec2f(TILE_SIZE_X, TILE_SIZE_Y);
    // We want to be sure that we draw the tiles that are slightly in the frustum.
    maxIndex.x += 1;
    maxIndex.y += 1;

    int numColumns = (maxIndex.x - minIndex.x) + 1;
    int numRows = (maxIndex.y - minIndex.y) + 1;

    for(int row = 0;row < numRows;++row)
    	for(int column = 0;column < numColumns;++column)
    		int index = (unsigned int)(minIndex.x + row) + (unsigned int)(minIndex.y + column) * TILE_SIZE_X;

worldPos is the position of the camera. This approach would run a lot faster than bruteforcing each tile. It will only loop through the tiles that you want to draw. 

Beware that this code snippet is not tested, so it may include type errors etc.


Good luck.

#5066874 what the mechanics behind this ?

Posted by on 02 June 2013 - 07:53 AM

I would have solved this problem with simple classic physics. Look at your problem as a composite system. Your character got a force which makes it move. Futhermore the character would have a force, which makes it jump. It may also have a drag force, and lastly a gravity force.


By using D'Alembert's principle, you would calculate the final force, which is acting on the object. It implies that, if we have a set of forces acting on an object, we can replace all those forces with a single force, which is done by just summing the forces together. 


So I would have calculated the forces, the acceleration, the velocity and the position of you character, by using vector math. Then i would just do a simple bounding box test between the character and the obstacle, to check if the character is inside.

Here is a link for a simple tutorial, which explains what i have mentioned. 

#5063487 Events and Entity Component Systems

Posted by on 21 May 2013 - 06:04 AM

This is one of the biggest problem with component systems, how the components communicate when they don't know about each other.

This problem is often resolved with an internal message system. Unfortunately it does not work well for all cases.


A solution for the health problem, can be that all objects that wants collision information sends a reference to a collision data structure, which is part of the entity, to the physic system.
Then the physic system, fill collision data for entities with collision data.

Now you can just check this collision information in each update, and call the sound and health system.
The problem with this solution, is that some entities may want more information about the collision than others, so unused information may be stored.
Hence, this breaks the idea of component based design. 


Another solution is to make a query based physic system. Where entities can query for collision information. You can then solve the problem with unused information by creating different types of queries. The query system can also be used for ray tracing etc.


Both solutions require update functions, so if you want a very strict implementation of ECS these solutions is not appropriate.

#5063466 Moddable but not open source?

Posted by on 21 May 2013 - 04:06 AM

There are two approaches you can choose between. Both of them needs to be done in the design phase of your project.


The first one is to make a plugin based system, using DLL's(dynamic link library) on Windows, or shared libraries on UNIX platform.
You start of creating an executable core, with the engine and other things you don't want a modder to have access to.

However keep in mind what you want the modder to have access to, and export these functions.
Now you can choose to make the game as a mod, or create the game explicit in the executable.
The first approach is the easiest way, and used by Valve for example.


The second approach is to give the modder a scripting language, which is linked with some of the functions in your game.

This can easily be done with Python, or Lua. Most games made to day have scripting support, used during the development cycle.
Unreal Tournament uses this approach for example.

When it comes to assets, you need to have a copyright license. The copyright license must be there even if you don't make the game moddable.
However most games use compiled assets, which often are hard to edit or use. This way the modders can only use your assets in your game, but can't edit them.
You should also ship the tools used for compiling assets, so that the modders can create their own assets.

#5062349 Visual Studio 2010 C++ | Folder structures

Posted by on 16 May 2013 - 12:58 PM

I will show you how i do it.

  • First create a project, where "Create directory for solution" is ticked. 
  • Then remove the project from the solution, by right clicking in the Solution Explorer on the project.(It will not delete the project, only lose reference to it)
  • Now create a new directory for the project file, and call it what you want.
  • Copy the project files from the directory storing the project into your new folder.
  • Back in Visual Studio, add the existing project to the solution file, by File->Add->Existing Project.
  • Now you probably want to define the binary output folder.
  • This is done in the project properties. General tab->Output Directory. Remember to set both Debug and Release.
  • Also, set the path for your source folder, when you create new source files. This is done with the Location property in the add file window.
  • Git got a handy setting file called .gitignore. Use this file to ignore binaries, objects, and other temporary data generated by visual studio.
  • I often use the following ignore file, with some customization for Visual Studio: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore


That's it, customize as much as you want :)

#5039633 Level data organisation advice?

Posted by on 05 March 2013 - 12:51 PM

Why not make an event structure like following:

public struct Event_t
char eventType;//Byte alignment error, for more memory efficient storage, should this byte be the last element of the struct!
int eventDuration;
short eventSize;

Event duration is the time from this event to next event. The event duration can be zero, which means that the event would be processed simultaneously with the next event. The event size are the number of bytes of the event(can be discarded if the size of the types of events are static). From here you can make different Event types. For example:

public struct EventMessage_t
int nMessageLength;
string szMessage;

After you have loaded the level. You can derive the event structs from a base event struct. Then you can make an array of base event structs, which you can easily process, by casting the base struct to the right event type structure.

#4980446 Marching Cube Holes

Posted by on 15 September 2012 - 12:19 PM

Now i have implemented the information i got from: http://gamedev.stack...le-perlin-noise
Even if i did that, i still had cracks, but then after a lot of testing of different sample types, i finally got it work. It worked when i used this sampler:

float4 realGridPos;
realGridPos.x = gridPos.x * voxelSize;
realGridPos.y = gridPos.y * voxelSize;
realGridPos.z = gridPos.z * voxelSize;

density.x += read_imagef(randomVolume, randomVolumeSampler, realGridPos).x * 0.8f;

realGridPos is the same as the local position in terms of a block, which in this case range from [0.0, 2.0] for each block.
Here is the result:

Thanks for the help! Posted Image

#4851528 Bounding Sphere from OBB

Posted by on 20 August 2011 - 02:26 AM

Problem with a sphere is that it only haves 1 degree of freedom. You can only xhoose one size for every axe. So if one bounding box axe is slightly bigger the sphere would have more free space because the biggest axe would make the sphere. But on the picture is looks like the box is not in centre of sphere, that's maybe the problem.
And also you are generatimg the sphere wrong. The radius is the biggest axe extent.