Calculate travel time on road map with semaphores

Posted by Ivansek on Game Development See other posts from Game Development or by Ivansek
Published on 2012-07-02T06:27:06Z Indexed on 2012/07/02 9:24 UTC
Read the original article Hit count: 258

Filed under:
|
|

I have a road map with intersections. At intersections there are semaphores. For each semaphore I generate a red light time and green light time which are represented with syntax [R:T1, G:T2], for example:

    119                   185                 250
A ------- B: [R:6, G:4] ------ C: [R:5, G:5] ------ D

I want to calculate a car travel time from A - D. Now I do this with this pseudo code:

function get_travel_time(semaphores_configuration)
{
    time = 0;
    for( i=1; i<path.length;i++)
    {
        prev_node = path[i-1];
        next_node = path[i]);
        cost = cost_between(prev_node, next_node)
        time += (cost/movement_speed) // movement_speed = 50px per second
        light_times = get_light_times(path[i], semaphore_configurations)
        lights_cycle = get_lights_cycle(light_times) // Eg: [R,R,R,G,G,G,G], where [R:3, G:4]
        lights_sum = light_times.green_time+light_times.red_light; // Lights cycle time
        light = lights_cycle[cost%lights_sum];

        if( light == "R" )
        {
            time += light_times.red_light;
        }
    }
    return time;
}

So for distance 119 between A and B travel time is, 119/50 = 2.38s ( exactly mesaured time is between 2.5s and 2.6s), then we add time if we came at a red light when at B. If we came at a red light is calculated with lines:

lights_cycle = get_lights_cycle(light_times) // Eg: [R,R,R,G,G,G,G], where [R:3, G:4]
lights_sum = light_times.green_time+light_times.red_light
light = lights_cycle[cost%lights_sum];
if( light == "R" )
{
    time += light_times.red_light;
}

This pseudo code doesn't calculate exactly the same times as they are mesaured, but the calculations are very close to them.

Any idea how I would calculate this?

© Game Development or respective owner

Related posts about JavaScript

Related posts about ai