iOS - bodyWithPolygonFromPath:Body与路径相同但碰撞无法正常工作 [英] iOS - bodyWithPolygonFromPath : Body is the same as the path but collisions are not working properly

查看:190
本文介绍了iOS - bodyWithPolygonFromPath:Body与路径相同但碰撞无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要感谢本网站上的每一位用户,因为我总是在这里寻找解决方案而且非常有帮助!

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 !

我正在尝试用SpriteKit制作像Xonix,Bix或Jezzball这样的游戏。无论如何,我有一个球在墙壁上弹跳,我试图在不能去的地方制造障碍,这些障碍是由CGPathref制造的(用户通过它的动作制作)

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)

我正在使用bodyWithPolygonFromPath来创建skspritenode的物理实体,它正在工作,但并非总是如此。我已经下载了YMCPhysicsDebugger以查看身体的内容,并且在每种情况下,它都很好,但是我已经看到当我在CGPath上有超过4个点时,球不会碰到整个身体,它只碰到一个较小的区域..我已经尝试和搜索了很多没有任何结果..我试图逆时针方向使CGPath的点,它不起作用,即使在另一侧..

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..

我正在复制你创建SKSpriteNode的代码,但我怀疑它会有用,我认为问题在于我的观点,也许我需要更多的解释物理体如何使用多边形

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 *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/e16d18e

我还尝试检测球何时进入更新功能中的障碍物手动让它反弹,但当然,它并没有真正起作用。加上我的数学学习不是很好lol

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

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++;

}

我还想过每次CGPath都要制作几个SKSpriteNode有超过4分,但我真的不知道如何在数学中做到这一点..

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..

无论如何,这是一个很长的帖子,我希望你们中的一些人会读到一切都明白了(对不起我的英语,我是法国人!)最重要的是,能够帮助我!!

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 !!

非常感谢你

推荐答案

第一个屏幕截图中的形状是凸的,但第二个屏幕截图中的形状是凹的。

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:


  • 创建两个节点和两个实体,其各自的形状为凸形,但它们一起形成凹形。这些机构可以是动态的。您可能希望将它们设置为静态或使用刚性距离关节将它们连接在一起,或者确保它们不会分开。

  • 使用 bodyWithEdgeLoopF​​romPath: bodyWithEdgeChainFromPath:能够创建凹面碰撞形状。这些物体不会是动态的,也不会自行移动,但它们是任意碰撞形状与静态游戏场的完美解决方案。

  • 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.

这篇关于iOS - bodyWithPolygonFromPath:Body与路径相同但碰撞无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆