1

I am trying to calculate difference in numbers between 2 Points, from a list of about 90 points. The code I have so far:

int positiveCounter = 0;
        int positiveResetCounter = currentWayPointID;
        int negativeCounter = 0;
        int negativeResetCounter = currentWayPointID;
        while ((currentWayPointID + positiveResetCounter) != pos)
        {
            positiveCounter++;
            positiveResetCounter++;
            if(positiveResetCounter > navigationTrack.AllWayPoints.Count)
            {
                positiveResetCounter = 0;
            }
        }
        while((currentWayPointID+negativeResetCounter) != pos)
        {
            negativeCounter++;
            negativeResetCounter--;
            if(negativeResetCounter < 0)
            {
                negativeResetCounter = navigationTrack.AllWayPoints.Count;
            }
        }
        if(positiveCounter <= negativeCounter)
        {
            MoveForward();
        }
        else if(negativeCounter < positiveCounter)
        {
          //  MoveBack();
        }

This works as intended but its too much for update to handle. How can I do this in less taxing way? To give bit more context I have list of waypoints and vehicle that moves on each the vehicle moves to the point that is closest to my mouses position. The path is circular so last waypoint connects first first(index 0). I am trying to determine shortest path to each waypoint in order to go forward or back and the code above is my attempt at calculating which way to go. I am not looking for a way to make it move as that already works.

snake555510
  • 81
  • 1
  • 1
  • 11
  • Something seems off in your code. Why do you initialize `positiveResetCounter` and `negativeResetCounter` to _currentWayPointID_? As far as i can tell, they represent an offset/relative position relative to _currentWayPointID_. Shouldn't they thus be initialized with 0 instead? –  Feb 24 '19 at 21:30
  • By the way, 90 points in the list should not cause performance problems. In the worst case, both while-loops together should only loop 90 times accumulatively (that should be lightning-fast); if it takes considerably more time, your logic is broken somewhere, somehow –  Feb 24 '19 at 21:33
  • They are just means of reseting the number on list when it reach 0 or list.count amount. They are just ints nothing to do with vectors – snake555510 Feb 24 '19 at 21:34
  • If that is so, can you explain the expressions in the `while` loops you use? I don't mean you should explain to me what you believe they do, but rather look and try to understand what they actually do. I suggest you use the debugger (especially its single-step feature) and step through your code logic step-by-step while at every step observing the content and changes of all involved variables/fields/etc. If you play this through a few times, it should hopefully become clearer of where the logic is possibly broken and why it is apparently consuming so much time –  Feb 24 '19 at 21:37

1 Answers1

1

I assume pos is the target index of the waypoint you want to reach.

instead of the while loops and index shifting you could simply compare the indexes directly:

Lets say you have the waypoint list like

[WP0, WP1, WP2, WP3, WP4, ... WPn]

so the available indexes are 0 to n, the list length n+1

Lets say currentWayPointID = n and pos = 2.

What you want to know is whether it is faster to go backwards or fowards. So you want to compare which difference is smaller:

going backwards

n - 2 // simply go steps backwards until reaching 2

or going forwards using a virtual extended list

(n+1) + 2 - n; // add the length of the list to the target index

or to visiualize it

                 [WP0,   WP1,   WP2,   WP3,   WP4, ... WPn]

index:              0,     1,     2,     3,     4, ...     n
extended index: n+1+0, n+1+1, n+1+2, n+1+3, n+1+4, ... n+n+1

So in order to generalize that you only have to check first whether the currentwaypointID is before or after pos something like

bool isForwards = true;
if(currentwaypointID >= pos)
{
    if(currentwaypointID  - pos < navigationTrack.AllWayPoints.Count + pos - currentwaypointID)
    {
        isForwards = false;
    }
}
else
{
    if(pos - currentwaypointID > navigationTrack.AllWayPoints.Count + currentwaypointID - pos)
    {
        isForwards = false;
    }
}

if(isForwards)
{
    MoveForward();
}
else
{
    MoveBack();
}
derHugo
  • 83,094
  • 9
  • 75
  • 115