0

I am drawing a ShapeNode using UITouch as below:

The line draws correctly. However, my problem is with adding the physicsbody to the shapenode(I want a SKSpriteNode to collide and bounce). But it's as if the physicsbody never attaches to the shapenode. I've used YMCPhysicsDebugger to see where the physicsbody is, relative to the shapenode, but it doesn't detect anything.

Has anyone encountered this before? How do you successfully add a physicsbody to a dynamic shapenode?

`-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

    selectorLine = [SKShapeNode node];
    selectorLine.path = pathToDraw;
    selectorLine.strokeColor = [SKColor greenColor];
    selectorLine.lineWidth = 10;
    selectorLine.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:pathToDraw];
    selectorLine.physicsBody.restitution=1.0f;
    selectorLine.physicsBody.dynamic=YES;
    selectorLine.physicsBody.usesPreciseCollisionDetection=YES;
    selectorLine.physicsBody.mass=0.2;
    selectorLine.zPosition=1.5;

    [self addChild:selectorLine];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

 touch = [touches anyObject];
 CGPoint positionInScene = [touch locationInNode:self];
 CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
 selectorLine.path = pathToDraw;

}

Thanks, Doug

Doug
  • 169
  • 1
  • 2
  • 12
  • is the path convex and are the points in counter-clockwise order? – CodeSmile Mar 06 '14 at 16:21
  • @LearnCocos2D Could you elaborate? I read similar advice from you on other SO questions but don't understand. The code is above. The path is whatever the user sets. – Doug Mar 06 '14 at 16:47
  • Convex as defined here: http://en.wikipedia.org/wiki/Convex_and_concave_polygons Counter-clockwise means if you look at the pentagon shape on the wikipedia page and you start with the topmost point, the next point must be the one on the right side, then the one on the lower right, then lower left and left side. That is the orientation of the polygon. Complicated definition: http://en.wikipedia.org/wiki/Curve_orientation#Orientation_of_a_simple_polygon – CodeSmile Mar 06 '14 at 17:45
  • @LearnCocos2D You assisted a very similar SO reply: http://stackoverflow.com/questions/20882339/skshapenode-is-not-responding-to-physicsbody but it didn't seem to work for the SO member either. Using code, can you give me an example of setting the path convex and points in counter-clockwise order? – Doug Mar 06 '14 at 20:23

2 Answers2

1

I recall reading that you need to add the SKNode to it's parent and set it's position before adding the physics body...

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
  • Thanks. I am adding the shapenode prior to setting the physicsbody as shown in my code above. – Doug Mar 06 '14 at 16:48
  • Where are you doing that? – Mike Pollard Mar 06 '14 at 17:04
  • Yeah, so like I said, try adding the new SKNode to it's parent and setting it's position before you add it's physicsBody. – Mike Pollard Mar 06 '14 at 17:15
  • Thanks Mike. I'm new to Spritekit and I'm missing the concept that you're describing. Could you give me an example? Add the shapenode in touchesBegan, but then add the physicsbody in touchesEnded? – Doug Mar 06 '14 at 17:23
  • Also, to answer the question directly, I am setting the position of the node in touchesMoved. – Doug Mar 06 '14 at 18:37
  • @Doug What he's saying is after you've added the sprite to it's parent; example `[self addChild:node]` then you create the physics body. – Daniel Sep 15 '15 at 10:54
1

I found the solution that solved it for me (in Swift). I had exactly the same problem as you're facing. Instead of setting the node path like

selectorLine.path = pathToDraw

try setting it in the constructor instead like

selectorLine = SKPathNode(path: pathToDraw)