vendredi 20 avril 2018

Traffic simulation collision detecttion

The following code I've written takes in a vector of type int and finds a specific integer, in this case a 1. It will then move this 1 to a new position depending on the value of R. The idea of this is to be a simple representation of a car on a road that's moving at a given speed.

#ifndef swap_H
#define swap_h

#include <vector>
#include <iostream>
using namespace std;

vector<int>swap_elements(vector<int>(n))
{
    vector<int> newvector(n);
    vector<int> nextvector(n);
    int holder;
    int R = 3;
    int P = 8;
    int B;

    cout << newvector.size() << endl;
    bool isfound = false;
    int count = 0;

    for (int j = 0; j < newvector.size(); j++) {

        if (count == 2){
            isfound = false;
            count = 0;
        }
        if (newvector[j] == 1) {
            count = count + 1;
                if (isfound != true){

                holder = newvector[j];
                if (j + R < newvector.size()){
                    newvector[j] = newvector[j + R]; // R will be taken from a vehicle class, R being the speed
                    newvector[j + R] = holder;
                    isfound = true;
                }
                else{
                    newvector[j] = newvector[0]; // R will be taken from a vehicle class, R being the speed
                    newvector[0] = holder;
                    isfound = true;
                  }
                }                   
            }           

            //cout << "Count: " << count << endl;
            cout << newvector[j] << endl;
            //system("pause");
        }

    return newvector;
}
#endif

The problem I'm having is to be able to have more that one car on the road and be able to move them without them colliding as no overtaking will be permitted. I need to be able to check for the car ahead and leave a desired spacing but don't know how to achieve it.

I tried adding a smaller loop that would check from that current index and check dependent on the car speed +1 with no success.

Aucun commentaire:

Enregistrer un commentaire