bodyWithPolygonFromPath: Body与路径是相同的,但是碰撞不能正常工作

时间:2023-01-23 13:12:25

First i'd like to say thanks to every users on this website because i'm always finding solutions here and it's sooo helpful !

首先,我想对这个网站上的所有用户说声谢谢,因为我总是在这里找到解决方案,它非常有用!

I'm trying to make a game like Xonix, Bix or Jezzball with SpriteKit. Anyway, i have a ball bouncing against walls, and i'm trying to make obstacles where it can't go, those obstacles are made from CGPathref (the user makes it with its movements)

我试着用SpriteKit做一个像Xonix, Bix或者Jezzball的游戏。不管怎样,我有一个球撞在墙上,我试着在它不能去的地方设置障碍,这些障碍是由CGPathref(用户用它的动作来制造的)制造的

I'm using bodyWithPolygonFromPath to create the physicsbody of the skspritenode, it's working, but not always. I've downloaded YMCPhysicsDebugger to see the content of the body, and in every case, it's good, but i've seen that when i have more than 4 points on the CGPath, the ball does not collide against the whole of the body, it only collides against a smaller zone.. I've tried and searched a lot without any results.. I've tried to make the points of CGPath counterclockwise, it doesn't work as well, even in the other side..

我用bodyWithPolygonFromPath创建了skspritenode的物理体,它是有效的,但并不总是有效的。我下载了YMCPhysicsDebugger来查看身体的内容,在每种情况下都很好,但是我已经看到当我在CGPath上有超过4个点的时候,球不会撞到整个身体,它只会碰撞一个更小的区域。我已经试了很多次了,但是没有结果。我试着让CGPath的点是逆时针的,即使在另一边也不行。

I'm copying you the code for the creation of SKSpriteNode, but i doubt it will be useful, i think the problem is with my points, maybe i need more explanations on how the physicsbody works with a polygon

我复制了创建SKSpriteNode的代码,但我怀疑它是否有用,我认为问题在于我的观点,也许我需要更多关于物理体如何与多边形结合的解释

SKSpriteNode *zone = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)];
zone.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath]; // 1
zone.physicsBody.friction = 0.0f;
zone.physicsBody.restitution = 0.0f;
zone.physicsBody.linearDamping = 0.0f;
zone.physicsBody.allowsRotation = NO;
zone.physicsBody.categoryBitMask = wallsCategory;
zone.physicsBody.dynamic = NO; // 2
[self addChild:zone];

Here are two pictures of my "game", the first one works, the ball bounces right back against it. The second one is not working like it should, only the upper part is detected for collision.

这是我的“游戏”的两张照片,第一张成功了,球正好反弹回来。第二种方法不像它应该那样工作,只有上部被检测到碰撞。

http://hpics.li/86a69e2

http://hpics.li/86a69e2

http://hpics.li/e16d18e

http://hpics.li/e16d18e

I've also tried to detect when the ball enters an obstacle in the update function to manually make it bounce against it, but of course, it's not really working. Plus my math studies are not that good lol

我还尝试检测球何时进入更新函数中的障碍,以手动使它与它反弹,但当然,它并没有真正工作。再加上我的数学学习不是很好,lol

for(SKSpriteNode *zone in zonesTab) 
{
    UIBezierPath *path = [zoneesTab objectAtIndex:i]; // Array of UIBezierPath for the obstacles
    if(CGPathContainsPoint(path.CGPath, NULL, ball.frame.origin, FALSE)) 
    {
        ball.physicsBody.velocity=CGVectorMake(ball.physicsBody.velocity.dx*-1, ball.physicsBody.velocity.dy*-1);
        NSLog(@"Touches iT");
    }
    i++;

}

I've also thought about making several SKSpriteNode everytime a CGPath has more than 4 points, but i don't really know how to do that in mathematics..

每次CGPath的分数超过4分的时候,我也想做几个SKSpriteNode,但是我真的不知道如何在数学中做到这一点。

Anyway, that's a long post, I hope some of you will read it all and understand it all (sorry for my english, i'm french!) and most of all, will be able to help me !!

无论如何,这是一篇很长的文章,我希望你们中的一些人能读懂所有的内容(为我的英文感到抱歉,我是法国人!)而且最重要的是,我将能够帮助我!!

Thank you very much

非常感谢你

1 个解决方案

#1


10  

The shape in the first screenshot is convex but the shape in the second screenshot is concave.

第一个截图的形状是凸的,而第二个截图的形状是凹的。

The physics polygon shapes need to be convex for them to work correctly. You can do one of two things:

物理多边形的形状需要是凸的,以便它们正确地工作。你可以做以下两件事之一:

  • Create two nodes and two bodies whose individual shapes are convex but together they form the concave shape. These bodies can be dynamic. You may want to set them to static or connect them together with a rigid distance joint, or otherwise ensure they are not going to move apart.
  • 创建两个节点和两个实体,它们各自的形状是凸起的,但它们一起形成凹形。这些身体可以是动态的。你可能想把它们设置为静态的,或者用一个刚性的距离连接在一起,或者确保它们不会分开。
  • Use bodyWithEdgeLoopFromPath: or bodyWithEdgeChainFromPath: to be able to create concave collision shapes. These bodies will not be dynamic and won't be able to move by themselves but they are the perfect solution for arbitrary collision shapes with a static playfield.
  • 使用bodyWithEdgeLoopFromPath:或bodyWithEdgeChainFromPath:能够创建凹的碰撞形状。这些物体将不会是动态的,也不能自己移动,但是它们是一个静态游戏场任意碰撞形状的完美解决方案。

#1


10  

The shape in the first screenshot is convex but the shape in the second screenshot is concave.

第一个截图的形状是凸的,而第二个截图的形状是凹的。

The physics polygon shapes need to be convex for them to work correctly. You can do one of two things:

物理多边形的形状需要是凸的,以便它们正确地工作。你可以做以下两件事之一:

  • Create two nodes and two bodies whose individual shapes are convex but together they form the concave shape. These bodies can be dynamic. You may want to set them to static or connect them together with a rigid distance joint, or otherwise ensure they are not going to move apart.
  • 创建两个节点和两个实体,它们各自的形状是凸起的,但它们一起形成凹形。这些身体可以是动态的。你可能想把它们设置为静态的,或者用一个刚性的距离连接在一起,或者确保它们不会分开。
  • Use bodyWithEdgeLoopFromPath: or bodyWithEdgeChainFromPath: to be able to create concave collision shapes. These bodies will not be dynamic and won't be able to move by themselves but they are the perfect solution for arbitrary collision shapes with a static playfield.
  • 使用bodyWithEdgeLoopFromPath:或bodyWithEdgeChainFromPath:能够创建凹的碰撞形状。这些物体将不会是动态的,也不能自己移动,但是它们是一个静态游戏场任意碰撞形状的完美解决方案。