Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spritekit | Swift3 | Applying SKWarpGeometry to nodes

I've searched the depths of the internet to find some tutorials on implementing SKWarpGeometry on SKSpritenodes to no avail.

I have a Trampoline SKSpritenode and a ball SKSpritenode.

 ballNode = childNode(withName: "ballNode") as? SKSpriteNode
    ballNode?.physicsBody = SKPhysicsBody(circleOfRadius: (ballNode?.frame.size.width)! / 2)
    ballNode?.physicsBody?.isDynamic = true
    ballNode?.physicsBody?.usesPreciseCollisionDetection = true
    ballNode?.zPosition = 3
    ballNode?.physicsBody!.categoryBitMask = PhysicsCategory.ballCategory
    ballNode?.physicsBody!.contactTestBitMask = PhysicsCategory.trampolineCategory
    ballNode?.physicsBody!.collisionBitMask = PhysicsCategory.platformCategory
    ballNode?.physicsBody?.velocity = CGVector(dx: 0.0, dy: 0.0)
    ballNode?.move(toParent: _gameNode)

enumerateChildNodes(withName: "trampoline") {trampolineNode,_ in
    trampoline = trampolineNode as? SKSpriteNode
    trampolineNode.physicsBody = SKPhysicsBody.init(rectangleOf: CGSize(width: (trampoline?.size.width)!, height: (trampoline?.size.height)!))
    trampolineNode.physicsBody?.categoryBitMask = PhysicsCategory.trampolineCategory
    trampolineNode.physicsBody?.contactTestBitMask = PhysicsCategory.ballCategory
    trampolineNode.physicsBody?.collisionBitMask = PhysicsCategory.ballCategory
    trampolineNode.physicsBody?.affectedByGravity = false
    trampolineNode.physicsBody?.isDynamic = false
    trampolineNode.physicsBody?.usesPreciseCollisionDetection = true
    trampolineNode.physicsBody?.restitution = 1
    trampolineNode.move(toParent: _gameNode)

When the ballNode hits the trampoline. I want the top of trampoline node to bend inwards. However by running the following code, the trampoline image disappears.

if collision == PhysicsCategory.ballCategory | PhysicsCategory.trampolineCategory {
        let src = [float2(0.0), float2(1.0), float2(2.0),
                   float2(3.0), float2(4.0), float2(5.0),
                   float2(6.0), float2(7.0), float2(8.0)]

        let dst = [float2(0.0), float2(0.0), float2(2.0),
                   float2(3.0), float2(1.0), float2(5.0),
                   float2(6.0), float2(7.0), float2(8.0)]

        warpGrid = SKWarpGeometryGrid(columns: 2, rows: 2,
                                      sourcePositions: src,
                                      destinationPositions: dst)
        //
        trampoline?.warpGeometry = warpGrid     <---This line removes the trampoline image, so I could only assume its the way I set up the source and destination points.

        let transform = SKAction.warp(to: warpGrid!, duration: 1)
        let transformAction = SKAction.repeat(transform!, count: 1)
        trampoline?.run(transformAction)            
    }

I could be setting the grid nodes wrong but I have no clue as there are no tutorials to guide me. Has anyone used SKGeometry yet?

like image 380
user3482617 Avatar asked Oct 25 '16 22:10

user3482617


Video Answer


1 Answers

Solved it. Hope this helps other people!!!

//Create your SKSpritenodes
ballNode = childNode(withName: "ballNode") as? SKSpriteNode
ballNode?.physicsBody = SKPhysicsBody(circleOfRadius: (ballNode?.frame.size.width)! / 2)
ballNode?.physicsBody?.isDynamic = true
ballNode?.physicsBody?.usesPreciseCollisionDetection = true
ballNode?.zPosition = 3
ballNode?.physicsBody!.categoryBitMask = PhysicsCategory.ballCategory
ballNode?.physicsBody!.contactTestBitMask = PhysicsCategory.trampolineCategory
ballNode?.physicsBody!.collisionBitMask = PhysicsCategory.platformCategory
ballNode?.physicsBody?.velocity = CGVector(dx: 0.0, dy: 0.0)
ballNode?.move(toParent: _gameNode)

enumerateChildNodes(withName: "trampoline") {trampolineNode,_ in
trampoline = trampolineNode as? SKSpriteNode
trampolineNode.physicsBody = SKPhysicsBody.init(rectangleOf: CGSize(width: (trampoline?.size.width)!, height: (trampoline?.size.height)!))
trampolineNode.physicsBody?.categoryBitMask = PhysicsCategory.trampolineCategory
trampolineNode.physicsBody?.contactTestBitMask = PhysicsCategory.ballCategory
trampolineNode.physicsBody?.collisionBitMask = PhysicsCategory.ballCategory
trampolineNode.physicsBody?.affectedByGravity = false
trampolineNode.physicsBody?.isDynamic = false
trampolineNode.physicsBody?.usesPreciseCollisionDetection = true
trampolineNode.physicsBody?.restitution = 1
trampolineNode.move(toParent: _gameNode)

   // Declare the following SKWarpGeometryGrids
   var trampolineWarpGrid: SKWarpGeometryGrid?
   var trampolineNoWarpGrid: SKWarpGeometryGrid?

   // For 2x2 grid - State the source positions
    let sourcePositions: [vector_float2] = [
        vector_float2(0, 0),   vector_float2(0.5, 0),   vector_float2(1, 0),
        vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5),
        vector_float2(0, 1),   vector_float2(0.5, 1),   vector_float2(1, 1)
    ]

    // For 2x2 grid - State the destination positions
    // To make the dent, I changed row 3 column 2 value from (0.5, 1) to (0.5, 0.8)
    let destinationPositions: [vector_float2] = [
        vector_float2(0, 0),   vector_float2(0.5, 0),   vector_float2(1, 0),
        vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5),
        vector_float2(0, 1),   vector_float2(0.5, 0.8),   vector_float2(1, 1)
    ]

    //Create the 2x2 warp grid based on the source and destination positions
    trampolineWarpGrid = SKWarpGeometryGrid(columns: 2, rows: 2,
                                  sourcePositions: sourcePositions,
                                  destinationPositions: destinationPositions)

    //Create and assign a Grid to the SKSpritenode that has no warp effects
    //Will be used to convert the trampoline back to original view  
    trampolineNoWarpGrid = SKWarpGeometryGrid(columns: 2, rows: 2)
    trampoline?.warpGeometry = trampolineNoWarpGrid

 func didBegin(_ contact: SKPhysicsContact) {
    let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    if collision == PhysicsCategory.ballCategory | PhysicsCategory.trampolineCategory {
    //Transform the trampoline grid to show dent
    let transform = SKAction.warp(to: trampolineWarpGrid!, duration: 0.3)
    //Warp the trampoline gird back to normal
    let transformBack = SKAction.warp(to: trampolineNoWarpGrid!, duration: 0.3)
    //Run Action
    let transformAction = SKAction.sequence([transform!, transformBack!])
    trampoline?.run(SKAction.repeat(transformAction, count: 1))
}
like image 105
user3482617 Avatar answered Oct 30 '22 11:10

user3482617