本文介绍了SKshapenode没有回复Physicsbody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个SKShapeNode,并为其分配了一个physicsBody.但是,没有联系时不会触发它.

I have created a SKShapeNode and I have assigned a physicsBody to it. However, it is not being triggered when there is contact.

创建SKShapeNode代码:

-(SKShapeNode*)gravityline{
    //SKSpriteNode *lolo=[[SKSpriteNode alloc]init];
    SKShapeNode *lolo = [[SKShapeNode alloc] init];
    CGPoint fff=CGPointMake(ray1.position.x, ray1.position.y);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, fff.x, fff.y);
    CGPathAddLineToPoint(path, 0,rayoriginpoint.x,rayoriginpoint.y );
    CGPathCloseSubpath(path);
    lolo.path = path;
    lolo.name=@"gravityline";
    lolo.strokeColor=[SKColor greenColor];
    lolo.glowWidth=.1;
    CGPathRelease(path);
    lolo.physicsBody=[SKPhysicsBody bodyWithEdgeFromPoint:fff toPoint:rayoriginpoint];
    //lolo.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromPath:path];
    //lolo.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:path];
    lolo.physicsBody.categoryBitMask=raylightCategory;
    lolo.physicsBody.collisionBitMask=batCategory;
    lolo.physicsBody.contactTestBitMask=batCategory;
    lolo.physicsBody.usesPreciseCollisionDetection=YES;
    lolo.physicsBody.linearDamping=0;
    lolo.physicsBody.restitution=1.0;
    lolo.physicsBody.dynamic=NO;

    return lolo;
}

以下是触发代码:

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }
    if (firstBody.categoryBitMask == raylightCategory && secondBody.categoryBitMask==batCategory)
    {
        NSLog(@"Contact with bat have been made");
        [secondBody.node removeFromParent];
    }
}

如果有人知道我做错了什么,为什么SKShapeNode没有激活PhysicalBody,请告诉我.

If anybody has a clue what I did wrong, why the SKShapeNode is not activating the physicsBody, please let me know.

推荐答案

您的代码并未完全显示出您的"gravityLine"正在与什么碰撞.我只能从它的描述中推测它没有检测到与圆形物体(球和蝙蝠)的碰撞.

Your code doesn't quite show what your "gravityLine" is colliding with. I can only speculate from its description that it's not detecting a collision with a round shaped object (a ball and a bat).

"gravityLine"方法似乎正在返回由基于边缘的形状定义的SKShapeNode:

The "gravityLine" method seems to be returning a SKShapeNode that is defined by an edge-based shape:

lolo.physicsBody=[SKPhysicsBody bodyWithEdgeFromPoint:fff toPoint:rayoriginpoint];

lolo.physicsBody=[SKPhysicsBody bodyWithEdgeFromPoint:fff toPoint:rayoriginpoint];

涉及碰撞时,务必阅读Apple的Sprite Kit编程指南,特别是它对三种类型的形状的说明.

When it comes to collisions, it is important to read Apple's Sprite Kit Programming Guide, specifically it's explanation of three types of shapes.

2)静态体积类似于动态体积,但是它的速度被忽略,并且不受力或碰撞的影响.但是,由于它仍然具有体积,因此其他对象可以从其弹起或与之交互.使用静态体积来表示占用场景中空间但不会被模拟移动的项目.例如,您可能使用静态体积来表示迷宫的墙. 虽然将静态和动态体积视为不同的实体很有用,但实际上,这是两种不同的模式,您可以将其应用于任何基于体积的物理物体.这很有用,因为您可以有选择地启用或禁用主体的效果.

2) A static volume is similar to a dynamic volume, but its velocity is ignored and it is unaffected by forces or collisions. However, because it still has volume, other objects can bounce off it or interact with it. Use static volumes to represent items that take up space in the scene, but that should not be moved by the simulation. For example, you might use static volumes to represent the walls of a maze. While it is useful to think of static and dynamic volumes as distinct entities, in practice these are two different modes you can apply to any volume-based physics body. This can be useful because you can selectively enable or disable effects for a body.

3)边缘是静态的无体积物体.边缘永远不会因模拟而移动,其质量也无关紧要.边缘用于表示场景中的负空间(例如另一个实体内部的空心点)或不可跨越的,看不见的细边界.例如,边缘通常用于表示场景的边界. 边缘与体积之间的主要区别在于,边缘允许在其自身边界内移动,而体积则被视为实体. 如果通过其他方式移动边缘,则它们只会与体积互动,而不会与其他边缘互动.

3) An edge is a static volume-less body. Edges are never moved by the simulation and their mass doesn’t matter. Edges are used to represent negative space within a scene (such as a hollow spot inside another entity) or an uncrossable, invisibly thin boundary. For example, edges are frequently used to represent the boundaries of your scene. The main difference between a edge and a volume is that an edge permits movement inside its own boundaries, while a volume is considered a solid object. If edges are moved through other means, they only interact with volumes, not with other edges.

根据上述信息,如果您阅读了使用 bodyWithEdgeFromPoint:toPoint:方法的文档,您将看到正在创建基于边缘"的物理物体.

Based on the above info, if you read the documentation for the method that you used bodyWithEdgeFromPoint:toPoint:, you will see that you are creating an "Edge-based" physics body.

一个新的基于边缘的物理体.

A new edge-based physics body.

讨论

边没有体积或质量,并且始终将其视为动态属性等于NO.边缘只能与基于体积的物理物体碰撞.

An edge has no volume or mass and is always treated as if the dynamic property is equal to NO. Edges may only collide with volume-based physics bodies.

要使碰撞起作用,必须确保边缘与基于体积的物理物体碰撞.每种物理学的身体形状创建方法都会记录要创建形状的类型.

To make your collision work, you have to make sure that your edge is colliding with a volume-based physics body. Every physics body shape creation method documents what type if shape it's creating.

如果正在使用与边缘碰撞的基于体积的物理物体,则另一种可能性可能是所涉及对象的大小或速度所致.同样,阅读Apple的文档可以清楚地说明这一点.

If you are using a volume-based physics body that is colliding with your edge, then another possibility may be due to the size or speed of the involved objects. Again, reading Apple's docs makes it clear.

如果必须碰撞的物理物体,则可以提示Sprite Kit使用更精确的碰撞模型来检查相互作用.该模型更昂贵,因此应谨慎使用.当任何一个身体进行精确碰撞时,都会接触并测试多个移动位置,以确保检测到所有接触

If you have physics bodies that must collide, you can hint to Sprite Kit to use a more precise collision model to check for interactions. This model is more expensive, so it should be used sparingly. When either body uses precise collisions, multiple movement positions are contacted and tested to ensure that all contacts are detected

ship.physicsBody.usesPreciseCollisionDetection = YES;

其他可能性也可能使碰撞混乱,例如您要分配给物理物体的路径的错误位置信息.重要的是要理解,当您设置节点的路径时,将使用节点的局部坐标系来设置形状.重要的是要记住,Sprite Kit中的原点位于左下角(而不是UIKit的左上角),并且当您为节点的物理主体分配路径时,该路径是相对于节点的锚点放置的

Other possibilities might mess up collisions as well, such as wrong position info for the path that you're assigning to the physics body. It's important to understand that when you set a path to a node, that the shape is being set using the local coordinate system of the node. It's important to remember that the origin in Sprite Kit is located bottom left corner (not UIKit's top left corner), and when you assign a path to a physics body of a node, that the path is placed relative to the anchor point of the node.

例如:

SKShapeNode *ball = [[SKShapeNode alloc] init];
CGRect ballFrame = CGRectMake(-25.0, -25.0, 50.0, 50.0);
[ball setPath:[UIBezierPath bezierPathWithOvalInRect:ballFrame].CGPath];
[ball setPosition:CGPointMake(100.0, 450.0)];
[ball setFillColor:[UIColor redColor]];
[ball setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:25.0]];

如果将"ballFrame"的原点设置为(0,0),则半径为25.0的物理体圆将与球的形状不一致,因为物理的左下角物体将放置在球的局部坐标系中的球的锚点,即x = 0和y = 0的点.

If I had set the origin of "ballFrame" at (0,0), then the circle of the physics body with radius of 25.0, would not coincide with the shape of the ball, as the bottom left corner of the physics body will be placed at the anchor point of the ball, which is at points x = 0 and y = 0, in the local coordinate system of the ball.

这篇关于SKshapenode没有回复Physicsbody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 06:37