SKShapeNode 检测两条线的交点
Posted
技术标签:
【中文标题】SKShapeNode 检测两条线的交点【英文标题】:SKShapeNode Detect Two Lines Intersection 【发布时间】:2015-05-30 09:48:15 【问题描述】:我正在开发一个应用程序,我正在根据用户手指的用户触摸画线。一旦收到触摸结束事件,该行将转换为最后一条路径。当接收到新的触摸开始事件时,将绘制一条名为“当前路径”节点的新线。我为具有相反接触位掩码的两条线添加了一个物理体,但我无法接收碰撞事件。 以下是我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
currentPath = CGPathCreateMutable();
currentPathNode = [self newLineNodeWithFillColor : CURRENT_LINE_COLOR];
CGPathMoveToPoint(currentPath, NULL, positionInScene.x, positionInScene.y);
currentPathNode.path = currentPath;
[self addChild:currentPathNode];
uint32_t contactBitMask = circleCategory | lastPathCategory;
[self addPhysicsBodyForLine:currentPathNode withCategoryBitMask:drawPathCategory withContactBitMask:contactBitMask];
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPathAddLineToPoint(currentPath, NULL, positionInScene.x, positionInScene.y);
currentPathNode.path = currentPath;
uint32_t contactBitMask = lastPathCategory;
[self addPhysicsBodyForLine:currentPathNode withCategoryBitMask:drawPathCategory withContactBitMask:contactBitMask];
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
if(lastPath == nil)
lastPath = CGPathCreateMutable();
CGPathAddPath(lastPath, nil, currentPath);
[lastPathNode removeFromParent];
if(currentPathNode != nil)
[currentPathNode removeFromParent];
currentPathNode = nil;
lastPathNode = [self newLineNodeWithFillColor : LAST_LINE_COLOR];
lastPathNode.path = lastPath;
[self addChild:lastPathNode];
[self addPhysicsBodyForLine:lastPathNode withCategoryBitMask:lastPathCategory withContactBitMask:drawPathCategory];
CGPathRelease(currentPath);
- (void) addPhysicsBodyForLine:(SKShapeNode*)node withCategoryBitMask:(uint32_t)category withContactBitMask:(uint32_t)contactBitMask
node.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:node.path];
node.physicsBody.categoryBitMask = category;
node.physicsBody.contactTestBitMask = contactBitMask;
node.physicsBody.collisionBitMask = contactBitMask;
node.physicsBody.dynamic = YES;
node.physicsBody.usesPreciseCollisionDetection = YES;
但是没有检测到碰撞?任何解决方案。
【问题讨论】:
【参考方案1】:碰撞不能这样工作。如果您使用物理移动节点的位置,您只会记录碰撞。在已经存在的物理体上方(或跨过)创建新的物理体不会记录碰撞。
您可以在每次绘制新路径时使用-(BOOL)intersectsNode:(SKNode *)node
来检查新节点是否与任何其他节点相交。
【讨论】:
以上是关于SKShapeNode 检测两条线的交点的主要内容,如果未能解决你的问题,请参考以下文章