Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw in color the shape of your SKPhysicsBody

Tags:

ios

sprite-kit

Creating a game with SpriteKit and its actually working out very well. I am using physics and having the ability to see actually where my bodies are because I might have some alpha inside of my sprites will really be helpful. This will also be helpful for creating more precise bodies.

In the documentation for SpriteKit they talk about a debugOverlay but the only example they give is to draw the direction of gravity. Here is the link: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html

Scroll to area of debugging overlay.

Has anybody written some code that can easily access the body of all objects in a scene and draw the exact size of them and place them where they should be in the correlation? I'm very surprised apple did not just add some type of DrawGizmos property to the physics body.

Thanks in advance.

like image 242
Krzemienski Avatar asked Nov 20 '13 00:11

Krzemienski


2 Answers

I'm sure you've long since answered your question, but as of iOS 7.1 it is now possible to use:

skView.showsPhysics = YES;

Which works much better than the YMCPhysicsDebugger. YMCPhysicsDebugger is also no longer supported.

like image 69
Rob Avatar answered Nov 18 '22 03:11

Rob


This is from RW tutorial book.

- (void)attachDebugRectWithSize:(CGSize)s {
    CGPathRef bodyPath = CGPathCreateWithRect( CGRectMake(-s.width/2, -s.height/2, s.width,   s.height),nil);
    [self attachDebugFrameFromPath:bodyPath];
    CGPathRelease(bodyPath); 
 }

 - (void)attachDebugFrameFromPath:(CGPathRef)bodyPath {
     //if (kDebugDraw==NO) return;
     SKShapeNode *shape = [SKShapeNode node];
     shape.path = bodyPath;
     shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5];
     shape.lineWidth = 1.0;
     [self addChild:shape]; 
  }

To use this on a node,

 [mySpriteToDebug attachDebugRectWithSize:contactSize];
like image 43
DogCoffee Avatar answered Nov 18 '22 02:11

DogCoffee