Skip to content

Commit 45bcdd4

Browse files
committed
Just a small test case change when it comes to concave physical shapes.
Still not possible in SpriteKit. Contacted apple for feedback on this.
1 parent 941c935 commit 45bcdd4

File tree

3 files changed

+82
-66
lines changed

3 files changed

+82
-66
lines changed

GameDevHelperAPI/GHSprite.m

Lines changed: 82 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -179,73 +179,89 @@ -(void)createBody{
179179
}
180180
}
181181
else{//create using points
182-
183-
NSLog(@"SpriteKit does not currently support complex polygons");
184-
185-
// CGMutablePathRef bodyPath = CGPathCreateMutable();
186-
// CGPathMoveToPoint(bodyPath, nil, 0, 0);
187-
////
188-
// NSArray* fixtures = [shInfo objectForKey:@"fixtures"];
189-
// for(NSArray* fixPoints in fixtures)
190-
// {
191-
// int count = (int)[fixPoints count];
192-
//
193-
//// NSMutableArray* verts = [NSMutableArray arrayWithCapacity:count];
194-
//
195-
// int i = count - 1;
196-
// for(int j = 0; j< count; ++j)
197-
// {
198-
// NSString* pointStr = [fixPoints objectAtIndex:(NSUInteger)j];
199-
// CGPoint point = CGPointFromString(pointStr);
200-
//
201-
// //flip y for sprite kit coordinate system
202-
// point.y = self.size.height - point.y;
203-
// point.y = point.y - self.size.height;
204-
//
205-
//// [verts replaceObjectAtIndex:i withObject:[NSValue valueWithCGPoint:point]];
206-
// i = i-1;
207-
// }
208-
//
209-
// CGMutablePathRef fixPath = CGPathCreateMutable();
210-
//
211-
// bool first = true;
212-
// for(NSValue* val in verts)
213-
// {
214-
// CGPoint point = [val CGPointValue];
215-
//
216-
// if(first){
217-
// CGPathMoveToPoint(fixPath, nil, point.x, point.y);
218-
// }
219-
// else{
220-
// CGPathAddLineToPoint(fixPath, nil, point.x, point.y);
221-
// }
222-
// first = false;
223-
// }
224-
// if(self.physicsBody == nil){
225-
// self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:fixPath];
226-
// }
227-
// CGPathAddPath(bodyPath,
228-
// nil, fixPath);
229-
//
230-
// }
231-
//
232-
// //self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:bodyPath];
233-
//
234-
//
235-
// self.physicsBody.friction = friction;
236-
// self.physicsBody.restitution = restitution;
237-
// self.physicsBody.density = density;
238-
//
239-
// NSNumber* cat = [shInfo objectForKey:@"category"];
240-
// NSNumber* mask = [shInfo objectForKey:@"mask"];
241-
//
242-
// if(cat && mask)
243-
// {
244-
// self.physicsBody.categoryBitMask = [cat intValue];
245-
// self.physicsBody.collisionBitMask = [mask intValue];
246-
// }
247-
//
248182

183+
NSArray* fixtures = [shInfo objectForKey:@"fixtures"];
184+
bool firstFixtureNode = true;
185+
for(NSArray* fixPoints in fixtures)
186+
{
187+
int count = (int)[fixPoints count];
188+
CGPoint points[count];
189+
190+
int i = count - 1;
191+
for(int j = 0; j< count; ++j)
192+
{
193+
NSString* pointStr = [fixPoints objectAtIndex:(NSUInteger)j];
194+
CGPoint point = CGPointFromString(pointStr);
195+
196+
point.x *=2;
197+
point.y *=2;
198+
199+
//flip y for sprite kit coordinate system
200+
point.y = self.size.height - point.y;
201+
point.y = point.y - self.size.height;
202+
203+
points[j] = point;
204+
i = i-1;
205+
}
206+
207+
208+
CGMutablePathRef fixPath = CGPathCreateMutable();
209+
210+
bool first = true;
211+
for(int k = 0; k < count; ++k)
212+
{
213+
CGPoint point = points[k];
214+
if(first){
215+
CGPathMoveToPoint(fixPath, nil, point.x, point.y);
216+
}
217+
else{
218+
CGPathAddLineToPoint(fixPath, nil, point.x, point.y);
219+
}
220+
first = false;
221+
}
222+
223+
224+
SKNode* fixtureNode = self;
225+
if(!firstFixtureNode){
226+
// fixtureNode = [SKNode node];
227+
fixtureNode = [SKShapeNode node];
228+
[(SKShapeNode*)fixtureNode setPath:fixPath];
229+
}
230+
firstFixtureNode = false;
231+
232+
233+
234+
fixtureNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:fixPath];
235+
236+
fixtureNode.physicsBody.friction = friction;
237+
fixtureNode.physicsBody.restitution = restitution;
238+
fixtureNode.physicsBody.density = density;
239+
240+
NSNumber* cat = [shInfo objectForKey:@"category"];
241+
NSNumber* mask = [shInfo objectForKey:@"mask"];
242+
243+
if(cat && mask)
244+
{
245+
fixtureNode.physicsBody.categoryBitMask = [cat intValue];
246+
fixtureNode.physicsBody.collisionBitMask = [mask intValue];
247+
}
248+
249+
int type = [[physicsInfo objectForKey:@"type"] intValue];
250+
if(type == 3)//NO PHYSICS
251+
return;
252+
253+
fixtureNode.physicsBody.dynamic = NO;
254+
if(type == 2)//dynamic
255+
// fixtureNode.physicsBody.dynamic = YES;
256+
257+
fixtureNode.physicsBody.allowsRotation = ![[physicsInfo objectForKey:@"fixed"] boolValue];
258+
259+
if(self != fixtureNode)
260+
[self addChild:fixtureNode];
261+
262+
NSLog(@"DID ADD FIXTURE NODE body %p %d", fixtureNode.physicsBody, fixtureNode.physicsBody.dynamic);
263+
264+
}
249265
}
250266
}
251267

165 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)