Skip to content

Instantly share code, notes, and snippets.

@3lvis
Created September 30, 2013 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3lvis/6762167 to your computer and use it in GitHub Desktop.
Save 3lvis/6762167 to your computer and use it in GitHub Desktop.
//
// SKShapeNode+Utility.m
// Dynamics
//
// Created by Jonathan Wight on 6/14/13.
// Copyright (c) 2013 toxicsoftware. All rights reserved.
//
#import "SKShapeNode+Utility.h"
@implementation SKShapeNode (Utility)
+ (instancetype)shapeNodeWithCircleOfRadius:(CGFloat)r
{
// happy face smiley face
SKShapeNode *theShapeNode = [SKShapeNode node];
CGPathRef thePath = CGPathCreateWithEllipseInRect((CGRect){ { -r, -r }, { r * 2, r * 2 } }, NULL);
theShapeNode.path = thePath;
CGPathRelease(thePath);
return(theShapeNode);
}
- (void)inferPhysicsBody
{
SKPhysicsBody *theBody = NULL;
NSMutableArray *theArray = [NSMutableArray array];
CGPathApply(self.path, (__bridge void *)theArray, &MyCGPathApplierFunction);
if (theArray.count == 6)
{
NSString *s = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:theArray options:0 error:NULL] encoding:NSASCIIStringEncoding];
static NSRegularExpression *regex = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// The un-escaped version follows:
// \[\{"type":0,"points":\[\[(.+),0\]\]\},\{"type":3,"points":\[\[\1,(.+)\],\[\2,\1\],\[0,\1\]\]\},\{"type":3,"points":\[\[-\2,\1\],\[-\1,\2\],\[-\1,0\]\]\},\{"type":3,"points":\[\[-\1,-\2\],\[-\2,-\1\],\[0,-\1\]\]\},\{"type":3,"points":\[\[\2,-\1\],\[\1,-\2\],\[\1,0\]\]\},\{"type":4\}\]
regex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\{\"type\":0,\"points\":\\[\\[(.+),0\\]\\]\\},\\{\"type\":3,\"points\":\\[\\[\\1,(.+)\\],\\[\\2,\\1\\],\\[0,\\1\\]\\]\\},\\{\"type\":3,\"points\":\\[\\[-\\2,\\1\\],\\[-\\1,\\2\\],\\[-\\1,0\\]\\]\\},\\{\"type\":3,\"points\":\\[\\[-\\1,-\\2\\],\\[-\\2,-\\1\\],\\[0,-\\1\\]\\]\\},\\{\"type\":3,\"points\":\\[\\[\\2,-\\1\\],\\[\\1,-\\2\\],\\[\\1,0\\]\\]\\},\\{\"type\":4\\}\\]" options:NSRegularExpressionCaseInsensitive | NSRegularExpressionAnchorsMatchLines error:NULL];
});
NSTextCheckingResult *theMatch = [regex firstMatchInString:s options:0 range:(NSRange){ 0, s.length }];
if (theMatch != NULL)
{
CGFloat theRadius = [theArray[0][@"points"][0][0] floatValue];
theBody = [SKPhysicsBody bodyWithCircleOfRadius:theRadius];
}
}
if (theBody == NULL)
{
NSParameterAssert(self.path != NULL);
theBody = [SKPhysicsBody bodyWithPolygonFromPath:self.path];
}
self.physicsBody = theBody;
}
#pragma mark -
static void MyCGPathApplierFunction(void *info, const CGPathElement *element)
{
NSMutableArray *theArray = (__bridge NSMutableArray *)info;
NSArray *thePoints = NULL;
switch (element->type)
{
case kCGPathElementMoveToPoint:
thePoints = @[ @[ @(element->points[0].x), @(element->points[0].y) ] ];
break;
case kCGPathElementAddLineToPoint:
thePoints = @[ @[ @(element->points[0].x), @(element->points[0].y) ] ];
break;
case kCGPathElementAddQuadCurveToPoint:
thePoints = @[
@[ @(element->points[0].x), @(element->points[0].y) ],
@[ @(element->points[1].x), @(element->points[1].y) ],
@[ @(element->points[2].x), @(element->points[2].y) ],
@[ @(element->points[3].x), @(element->points[3].y) ],
];
break;
case kCGPathElementAddCurveToPoint:
thePoints = @[
@[ @(element->points[0].x), @(element->points[0].y) ],
@[ @(element->points[1].x), @(element->points[1].y) ],
@[ @(element->points[2].x), @(element->points[2].y) ],
];
break;
case kCGPathElementCloseSubpath:
break;
}
if (thePoints)
{
[theArray addObject:@{ @"type": @(element->type), @"points": thePoints }];
}
else
{
[theArray addObject:@{ @"type": @(element->type) }];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment