Giter Site home page Giter Site logo

skdestructiblenode's Introduction

SKDestructibleNode

This is an extension of the SKSpriteNode class and allows for destructible image nodes. It allows for an image to be rendered and used as an SKSpriteNode with the added feature of being able to be destroyed on command.

The image is broken up into square pieces and torn apart. Physics bodies are created based on the alpha of the piece. Therefore, more intricate pieces will have more intricate physics bodies (and will therefore have a performance impact). All in all, the performance stays relatively good for most images.

Usage

The code for the SKDestructibleNode class can be found in SKDestructibleNode.swift.

To create a piece broken with squares:

let node = SKDestructibleNode(imageName: "SinisterClown.png", scene: self, pieceSize: 25)
node.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(node)

//The broken pieces are returned as an array of SKSpriteNodes
let pieces = node.destroy()

To create a piece broken with rectangles of a certain size:

let node = SKDestructibleNode(imageName: "SinisterClown.png", scene: self, rectSize: CGSize(width: 50, height: 20))
node.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(node)

//The broken pieces are returned as an array of SKSpriteNodes
let pieces = node.destroy()

Alternatively, you can destroy the node with a boom:

let pieces = node.destroyWithBoom(0.015, atPoint: node.position)

The SKDestructibleNode class takes care of matching the original nodes position, rotation, velocity, etc.

Examples

Mario Bacon Clown Boom

Caveats

  • Creating the physicsBodies this way is expensive. The simpler the image, the better the performance will be. Squares and circles work best.
  • You will need to mess with the number of pieces for the given image. The pieces are stored so that they can be instantly added to the scene upon destruction. This means that the more pieces required to compose the image, the more memory will be eaten.
  • Some images just don't work well with this. Images with lots of detail (with alpha) will slow performance to a crawl. I'm working on some optimizations.
  • There is apparently a bug where physics bodies don't actually touch. This is why there is a small amount of space between the pieces. I'm working on finding a fix for this.

What's next?

  • Optimizations
  • Random/different piece sizes
  • Options for how the image breaks (explosions, fadeouts, etc)

skdestructiblenode's People

Contributors

trentsartain avatar

Watchers

James Cloos avatar Alex Nagy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.