5

I'm drawing with Sprite Kit. I would like to detect when user's drawings are intersecting.
enter image description here

I tried to following code but it doesn't work. It seems sprite kit is not saving all the points:

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    touch = touches.anyObject() as UITouch!        
    for drawingPoint in drawingPoints{
        if(touch.locationInNode(self) == drawingPoint){println(true)}
    }
    drawingPoints.append(touch.locationInNode(self))
}
user3673836
  • 591
  • 1
  • 9
  • 23
  • 1
    I am not very familiar with SpriteKit, but I assume that the red path connects each touch point with the next using a line segment or bezier curve. In that case you have to check all segments for possible intersection. [Here](http://stackoverflow.com/questions/13999249/uibezierpath-intersect) is a similar problem in Objective-C, perhaps that helps you to get started. – Martin R Nov 12 '14 at 19:34
  • 1
    A more sophisticated algorithm is described here: http://softsurfer.com/Archive/algorithm_0108/algorithm_0108.htm. – Martin R Nov 12 '14 at 19:56
  • @MartinR By the way, I gave you credit in [my answer to his other question](http://stackoverflow.com/a/26945903/1271826). (I only just noticed that it was a duplicate.). FYI, I modified your code snippet to identify not only identify that there was intersection, but where precisely that intersection was. I implemented a fairly simple-minded routine for iterating through the line segments, though... – Rob Nov 16 '14 at 22:05
  • Where do you think the problem is...I copy-pasted your algorithm but it's detecting intersection even when the line is just closing in on it self but not really intersecting yet. – user3673836 Nov 17 '14 at 21:22
  • You should create question showing what you tried and show example of this "false positive". When I used Martin's routine, it looked pretty good to me... – Rob Nov 19 '14 at 00:21
  • Ok Will do that. I'll create it in the form of a new question. – user3673836 Nov 21 '14 at 16:06

1 Answers1

0

Here is a function for segments intersection in swift.

func linesIntersect(line1 : CGPointInterval, line2 : CGPointInterval) -> (intersects: Bool, point : CGPoint?)
    {
        //The algorithm is taken from http://www.amazon.com/dp/0672323699/?tag=stackoverfl08-20
        // http://portal.aauj.edu/portal_resources/downloads/programming/windows_game_programming_guru.pdf

        let p0_x = line1.start.x
        let p1_x = line1.end.x
        let p2_x = line2.start.x
        let p3_x = line2.end.x

        let p0_y = line1.start.y
        let p1_y = line1.end.y
        let p2_y = line2.start.y
        let p3_y = line2.end.y

        let s1_x = p1_x - p0_x
        let s1_y = p1_y - p0_y
        let s2_x = p3_x - p2_x
        let s2_y = p3_y - p2_y

        let s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y)
        let t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y)

        if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
        {
            // Collision detected
            let finalX = p0_x + (t * s1_x)
            let finalY = p0_y + (t * s1_y)
            return (true, CGPointMake(finalX, finalY))
        }

        return (false, nil) // No collision
    }
Naloiko Eugene
  • 2,453
  • 1
  • 28
  • 18