[英]SKShapeNode Detect Two Lines Intersection
I am working on a application and i am drawing line based on user touch of user finger. 我正在开发一个应用程序,并且正在根据用户手指的触摸来画线。 Once the touch end event received the line is converted to last path.
收到触摸结束事件后,该行将转换为最后一条路径。 A new line is draw with name "Current path" node when a new touch began event received.
当收到新的触摸开始事件时,将绘制一条名为“当前路径”节点的新行。 I added a physics body for both the line with opposite contact bit mask but i am not able to receive collision event.
我为两条带有相反接触位掩码的线添加了一个物理实体,但是我无法接收碰撞事件。 Following is my code:
以下是我的代码:
- (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;
}
But collision is not Detected? 但是没有检测到碰撞? Any Solution.
任何解决方案。
Collisions do not work that way. 碰撞不是那样的。 You will only register a collision if you use physics to move a node's position.
如果您使用物理学来移动节点的位置,则仅会记录碰撞。 Creating a new physics body over (or across) an already existing physics body will not register a collision.
在(或横跨)已经存在的物理物体上创建新的物理物体将不会记录碰撞。
You can use -(BOOL)intersectsNode:(SKNode *)node
every time a new path is drawn to check if the new node intersects any other node. 每次绘制新路径时,都可以使用
-(BOOL)intersectsNode:(SKNode *)node
来检查新节点是否与任何其他节点相交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.