I started creating a small abstract simulation framework and would be happy if someone could take a look at the code and give me feedback on it.
The code can be found here, and it is currently not in a 100% finished and tested state.
It should be (almost) multi threading ready.
This is some sample code on how you can use the framework:
class Program
{
static void Main(string[] args)
{
Debug.Listeners.Add(new ConsoleTraceListener());
var system = new SimulationSystem();
// Add a thread and a simulation engine to the simulation framework
system.AddSimulationThread(new SimulationThread(new SimulationEngine[] {new TestSimulationEngine() }.ToList()));
// Add a new simulation group
system.World.Objects.Add(new SimulationGroup());
// Add a new object to the group
((SimulationGroup)system.World.Objects[0]).Objects.Add(new SimulationObject());
system.World.Objects.Add(new SimulationObject());
DateTime loopStart = DateTime.Now;
DateTime loopEnd = DateTime.Now;
// Simulates the update loop
while (true)
{
loopStart = DateTime.Now;
system.Update(loopStart - loopEnd);
loopEnd = DateTime.Now;
// Wait for user to press enter to start the next loop
//Console.ReadLine();
}
}
}
with this TestSimulationEngine
class:
public class TestSimulationEngine : SimulationEngine
{
public override void UpdateWorld(SimulationContext context, TimeSpan step)
{
// Do some stuff to the simulation world
Console.WriteLine("UpdatingWorld with step {0}", step);
// Add a new object each loop
context.World.Objects.Add(new SimulationObject());
}
}
A short introduction to the architecture behind the framework:
There is the class SimulationSystem
which you can use to initialise a new simulated world. You can assign SimulationEngines
to defined threads. For example a physicsengine to thread 1, a heatsystem to thread 2, ...
Also it holds the SimulationWorld
which is a hierarchical system. Inside the World you can add SimulationObjects
and SimulationGroups
. A SimulationGroup
is the same as a SimulationObject
but can also have sub-objects.
For example, inside the world you can add a car as a simulation group. Inside the car you have the engine, driver, etc as simulation sub-objects.
You can also set any property you want on the objects using the code object["Property"]
.