5

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)

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

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

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

}

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

Thank you very much

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user3198930
  • 138
  • 8

1 Answers1

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.
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • OH MY GOD !! Thank you so much !!! Why haven't i tried this before.. So many hours lost.. But I usually have trouble on simple things like this.. Anyway, thank you really much :) PS: My english is bad because i thought concave meant "having a hole inside", it misinformed me before! – user3198930 Jan 15 '14 at 17:40
  • I mix up convex and concave a lot too. ;) One way to remember is that one has an 'x' at the end whose two lines mark 4 corners which form a rectangle and a rectangle is a convex shape. – CodeSmile Jan 15 '14 at 17:50
  • 1
    Hows about "caves go in" as in concave? – fuzzygoat Jan 15 '14 at 19:28
  • or say it out loud as "convectangle" which will sound weird when used incorrectly as "concavtangle" ;) – CodeSmile Jan 15 '14 at 21:38
  • Well! Thanks for those ways to remember.. but i'm sure next time i'll see concave or convex, i'll mix them up ^^ – user3198930 Jan 16 '14 at 00:02
  • Thank you so much!!!! Really useful for me and developers who are facing same kind issue. :) :) :) – Sandip Patel - SM Mar 17 '16 at 13:52