For those smart coders out there I have a problem which is pushing me to the extent of my knowledge and I need some guidance.

I Am trying to code a multi-sprite object for a game which is loosely inspired by snake. This snake, lets call it a worm, contains a deque which holds the objects for the tail.

To make the worm move I created a system where when the snake rotates, it creates a struct including the position and the direction to rotate and sends it to the deque in the first tail object. Every time it moves, it checks whether it has moved passed it, if so it moves the tail back to the position of the message, works out the time to get there and moves again with the remaining time and checks for another message. If a tail object hits a message, it sends it to the next tail and so on until it is deleted after being used by the last tail object.

This allows for the tail object to exactly follow the path of the worm's head and stop the tail objects from creeping up around corners.

Now this algorithm, which I hope is some what clear, works fine most of the time. However what seems to be quite random, this system fails and the tails fails to turn and travels in the direction it was facing.

Here lies the problem, I have spent a fortnight trying to solve this bug. First I had the tail in a vector which was fine until I tested the grow function. This have a few problems so I changed it to a deque which reduced the frequencies of the breaking. Now im not sure how to solve this problem and I need guidance.

What I think is happening if that the check for passing the message is failing as the messages still exist when the tail breaks apart but I can't see how it is happened and I don't know how to use the debugger for random events like this!!!

Can someone smarter than me see anything that could go wrong in this function that checks if a point is in a line segment.

Code:`bool AAH::math::isPointOnLineSeg(sf::Vector2f lineStart, sf::Vector2f lineEnd, sf::Vector2f point)`

{

//work out the line segment as a vector

sf::Vector2f lineVec = lineEnd - lineStart;

//work out left hand size of the equation point-lineStart = t*lineVec

sf::Vector2f leftSide = point - lineStart;

//is the line not vertical or horizontal

if (lineVec.x !=0.0f && lineVec.y !=0.0f)

{

//if t for y and x components are the same the point is on the full line

if ( AreSame((leftSide.x)/(lineVec.x), (leftSide.y)/(lineVec.y) ) )

{

//is the point within the line segment

if ( ( AreSame((leftSide.x)/(lineVec.x), 0.0f) || (leftSide.x)/(lineVec.x) > 0.0f ) &&

( AreSame((leftSide.x)/(lineVec.x), 1.0f) || (leftSide.x)/(lineVec.x) < 1.0f ))

{

return true;

}

else

return false;

}

}

else if (AreSame(lineVec.x, 0.0f) && lineVec.y != 0.0f)

{

//if t for y and x components are the same the point is on the full line

if ( AreSame(point.x, lineStart.x) )

{

//is the point within the line segment

if ( ( AreSame((leftSide.y)/(lineVec.y), 0.0f) || (leftSide.y)/(lineVec.y) > 0.0f ) &&

( AreSame((leftSide.y)/(lineVec.y), 1.0f) || (leftSide.y)/(lineVec.y) < 1.0f ))

{

return true;

}

else

return false;

}

}

else if ( AreSame(lineVec.y ,0.0f) && lineVec.x != 0.0f)

{

//if t for y and x components are the same the point is on the full line

if ( AreSame(point.y, lineStart.y) )

{

//is the point within the line segment

if ( ( AreSame((leftSide.x)/(lineVec.x), 0.0f) || (leftSide.x)/(lineVec.x) > 0.0f ) &&

( AreSame((leftSide.x)/(lineVec.x), 1.0f) || (leftSide.x)/(lineVec.x) < 1.0f ))

{

return true;

}

else

return false;

}

}

return false;

}

bool AAH::math::AreSame(float a, float b)

{

return std::fabs(a - b) < std::numeric_limits<float>::epsilon()*100;

}

Sorry for the long post, I decided to not show all of the code described just of yet.