How can I detect when a 2D moving object has crossed its own path?
I store the path as an array of points based on the plane's previous positions.
Pseudo-code or any programming language can be used to describe a solution.
Here's my code I've tried already - it detects a full 360 loop. I think I need a different approach.
CGFloat angleDiff = angleCurr - lastAngleRecorded;
lastAngleRecorded = angleCurr;
// Ensure -180 < angleDiff < 180
angleDiff = angleDiff > M_PI ? angleDiff - (M_PI*2) : angleDiff;
angleDiff = angleDiff < -M_PI ? angleDiff + (M_PI*2) : angleDiff;
// Reset tracking of the loop of the plane's angle exceeds (turns too sharply) or falls below the limits
if(fabsf(angleDiff) < angleDiffMinAllowed || fabsf(angleDiff) > angleDiffMaxAllowed) {
if(++ringFaultCount >= ringFaultCountMax) {
[self resetTracking];
return;
}
}
ringFaultCount = 0;
// Add plane position to ring polygon
[ringPoints addObject:[NSValue valueWithCGPoint: ccp(targetPlane.position.x, targetPlane.position.y)]];
// Add angleDiff to angleTotal
angleTotal += angleDiff;
// Completed loop?
if(fabsf(angleTotal) >= M_PI * 2.f) {
[self resetTracking];
if(isRingJoined){
CCLOG(@"%@ RING COMPLETE", NSStringFromSelector(_cmd));
}
return;
}