0

i've created a code that creates a line of unlimited size by touch and i'm wondering how to restrict the size such that the beginning and end of the line can be a small distance to a maximum set distance apart?

The code i used was:

pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode = [SKShapeNode node];
lineNode.path = pathToDraw;
lineNode.zPosition = 1000;
lineNode.strokeColor = [SKColor blueColor];
lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:pathToDraw];
lineNode.physicsBody.categoryBitMask = ballCategory;
lineNode.physicsBody.contactTestBitMask = ballCategory;
[self addChild:lineNode];

in the touch Began Method and

CGPathAddLineToPoint(pathToDraw, NULL, location.x, location.y);
lineNode.path = pathToDraw;
lineNode.zPosition = 1000;
lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:pathToDraw];
lineNode.physicsBody.categoryBitMask = boundaryCategory;
lineNode.physicsBody.contactTestBitMask = ballCategory;
lineNode.name = @"boundary";
lineNode.physicsBody.restitution=1;

in the touchMoved method.

Thank you

bob
  • 1
  • 1
  • The code you have included does not draw a line. Where do u call CGPathAddLineToPoint? – ZeMoon Jul 21 '14 at 07:40
  • Sorry I forgot to include it in this question, the proper code is now shown in the question – bob Jul 21 '14 at 21:18

1 Answers1

0

First add this function...

// Computes the distance between two points
static inline CGFloat distance(CGPoint p1, CGPoint p2)
{
    CGFloat dx = p1.x - p2.x;
    CGFloat dy = p1.y - p2.y
    return sqrt(dx*dx+dy*dy);
}

then add this to your touches moved function...

CGFloat lineLength = distance(location, positionInScene);
if (distance < kMaxDistance) {
    // Draw line here

}

Also, I suggest you wait until you know the line is valid before creating the SKShapeNode. Perhaps you should create a placeholder node, such as a circle, and then create the shape node line after you verified that the line is valid.

0x141E
  • 12,613
  • 2
  • 41
  • 54