sprite kit collisions: ignore transparency?
I am building a game with Xcode’s spritekit. Its a platform game and right now it functions well with flat ground pieces. I was wondering if it was possible to ignore transparency in the png for collisions. That is to say, If i have a ground piece with a curved floor and transparency filling the troughs, can i make the player walk the curves instead of a square bounding box covering the whole thing? The only example i can find is in the Gamemaker GML language, you can do “precise” collisions such that blank space in the images do not count as part of the sprite. I can supply code if necessary but this seems like more of a conceptual question. Thanks in advance
3 Solutions Collect From Internet About “sprite kit collisions: ignore transparency?”
Hey there’s a easy solution for this provided in the Apple Documentation.
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"]; sprite.physicsBody = [SKPhysicsBody bodyWithTexture:sprite.texture size:sprite.texture.size];
This creates a physics body around the physical paths of the texture.
Simulating Physics – SpriteKit Programming Guide
It’s all in how you instantiate the physicsBody of the node in question.
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node.size];
is probably the easiest and most common way of making a physicsBody, but will also create the issue you’ve identified above, because, for all collision purposes, the node is a rectangle.
take a look at the documentation for SKPhysicsBody to see the other options available to you. PolygonFromPath and BodyWithBodies are probably the best suited for what you’re doing.
SKPhysicsBody is the right move, just wanted to note that it does make sense to have a separate (simplified) mask-image for SKPhysicsBody to improve your performance, simplified from color and geometry standpoint, and here’s the code:
let birdMask: UInt32 = 0x1 << 0 let pipeMask: UInt32 = 0x1 << 1 //... pipeImage = SKSpriteNode(imageNamed: "realImage") //... size and position let maskTexture = SKSpriteNode(imageNamed: mask) maskTexture.size = pipeImage!.size // size of texture w/ real imageNamed pipeImage!.physicsBody?.usesPreciseCollisionDetection = true pipeImage!.physicsBody = SKPhysicsBody(texture: maskTexture.texture!, size: size) pipeImage!.physicsBody?.affectedByGravity = false // disable falling down... pipeImage!.physicsBody?.allowsRotation = false pipeImage!.physicsBody?.isDynamic = true pipeImage!.physicsBody?.friction = 0 pipeImage!.physicsBody?.categoryBitMask = pipeMask pipeImage!.physicsBody?.collisionBitMask = birdMask | pipeMask pipeImage!.physicsBody?.contactTestBitMask = birdMask | pipeMask
and more detailed example/guide.
- Why do I get the error “error: unknown type name 'virtual'” when trying to compile this code?
- Can xcodebuild manage automatic signing?
- Hide statusbar using info.plist in any app
- NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?
- How to implement GMUClusterRenderer in Swift
- App Store Review Button
- How do I loop through and get all the keys of the nested nodes in firebase?
- Finding an angle with 3 CGPoints
- gestureRecognizer: shouldReceiveTouch: not getting called
- How to programmatically send SMS on the iPhone?
- How to determine the content size of a UIWebView?
- Using Grand Central Dispatch in Swift to parallelize and speed up “for” loops?
- How to Implement Semaphores in iOS Application?
- Xcode and iOS5.1 on iPhone won't work
- how to throw SKSpriteNode?