SpriteKit's SKPhysicsBody with polygon helper tool

前端 未结 8 2207
面向向阳花
面向向阳花 2020-11-28 21:45

I wonder if there is a tool that could be used for easy generation of complex physics bodies in SpriteKit. I would like to have a volume based physical bodies with polygon-t

8条回答
  •  爱一瞬间的悲伤
    2020-11-28 22:23

    This is an adaptation of Xelt's answer in Swift 3.

    
    
    
        
        SpriteKit Tools - SKPhysicsBody Path Generator (Swift version 
        
    
        
    
        
        
            

    SKPhysicsBody Path Generator

    Want to use SKPhysicsBody(polygonFromPath: path) easier way like me? Here with a small helper for easier path drawing, hope it help others too.

    Basic Instruction
    1. Drag and drop the sprite image into drop zone.
    2. Start drawing path by clicking on coordinates.
    Some Rules / Known Issue
    • Path need to be as a convex polygonal path with counterclockwise winding and no self intersections. The points are specified relative to the owning node’s origin. (documentation link)
    • Please use Chrome for best compatibility as I have not tested on other browsers.

    Retina? (please check before declaring path)

    X:0 Y:0


    Output
    let sprite = SKSpriteNode(imageNamed: "codeImgName")
    
    let offsetX = sprite.size.width * sprite.anchorPoint.x
    let offsetY = sprite.size.height * sprite.anchorPoint.y
    
    let path = CGMutablePath()
    
    
    path.closeSubpath()
    
    sprite.physicsBody = SKPhysicsBody(polygonFromPath: path)
    

提交回复
热议问题