Biokuh's picture

Traffic Simulation

Hello, I am new to OpenTK and I want to create a traffic simulation. What is a good option to create a map with some crossroads for this issue? I thought about a grid(cellular automaton) or vector graphics, so the cars know the position of the street; but i don´t know how to do this with OpenGL.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

The way you simulate traffic is (or should be) independent of the way you display that on the screen. I.e. you do not use OpenGL to simulate traffic, you use OpenGL to display the *results* of your traffic simulation on screen.

For a cellular automata approach, one simple map representation might be a 2d-grid (quick example):

enum CellType { Road, Pavement, /* anything else you need */ }
 
struct Cell
{
    public CellType Type { get; set; }
}
 
class Grid
{
    Cell[,] cells = new Cell[100, 100];
    public Cell[,] Cells { get; }
 
    // You could load the contents from a file
}
 
class Car
{
    public int X { get; set; } // x-coordinate on the grid
    public int Y { get; set; } // y-coordinate on the grid
}
 
class TrafficSimulator
{
    public void UpdateGrid(Grid grid, List<Car> cars, double time)
    {
        /* simulation code */
        /* there are no OpenGL commands here! */
        /* there is lots of literature on how to simulate traffic,
         * check google scholar or something similar */
    }
}
 
class TrafficRenderer
{
    public void RenderGrid(Grid grid, List<Car> cars, double time)
    {
        /* rendering code */
        /* all OpenGL commands go here */
        /* you could go as simple or as fancy as you require.
         * For a project I worked on, we had a basic 2d renderer
         * (just vectors and rectangles) to verify the results
         * of the simulation, and a fancy 3d renderer to show off. */
    }
}

This is just an example to help you get started. You'll need to adapt this to your needs. You'll also need to do some reading on the topics of traffic simulation and OpenGL / graphics.

(OpenTK gives you the means to use OpenGL from C# in a very simple and intuitive way, but you still need to understand how OpenGL works before you can use it effectively.)

flopoloco's picture

Hi Fiddler! I am glad you are back again :-D

Biokuh, here's how to setup a 2D project quickly.

using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
 
namespace OpenTK2DProject
{
	class Program : GameWindow
	{
		int gridX = 20, gridY = 20;
		int gridX2, gridY2;
 
		public Program(int width, int height) : base(width, height)
		{}
 
		protected override void OnLoad(EventArgs e)
		{
			gridX2 = Width / gridX;
			gridY2 = Height / gridY;
		}
 
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			GL.Begin(BeginMode.Lines);
			for (int x = 0; x < Width; x += gridX2)
			{
				GL.Vertex2(x, 0);
				GL.Vertex2(x, Height);
 
				for (int y = 0; y < Height; y += gridY2)
				{
					GL.Vertex2(0, y);
					GL.Vertex2(Width, y);
				}	
			}
			GL.End();
 
			SwapBuffers();
		}
 
		protected override void OnResize(EventArgs e)
		{
			GL.Viewport(0, 0, Width, Height);
			GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity();
			GL.Ortho(0, Width, Height, 0, -1, 1);
			GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity();
		}
 
		public static void Main(string[] args)
		{
			using (Program p = new Program(800, 800))
			{
				p.Run(60.0);
			}
		}
	}
}

Things might be tough I guess but it needs lots of practice to get going. Don't give up.