logo
down
shadow

Scenekit - physicsWorld setup to prevent kinematic nodes to intersect


Scenekit - physicsWorld setup to prevent kinematic nodes to intersect

By : Brendon U.
Date : November 24 2020, 03:01 PM
I hope this helps you . After a lot of days of headbanging and almost giving up, i re-re-reread the physicsWorld documentation and finally found what i was overlooking all the time - the contactTest method that can be manualy triggered at any time, independent of render loop. I use it in the renderer(_:willRenderScene:atTime:) in order to "fix" the overlap before the scene gets rendered.
My scene is bit more complicated than the example, but with few extra tweeks i almost got it working now. I'm not sure if this is the proper solution and how costy it will become performance wise, but for now i will settle down with this so i can move on with the development.
code :
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {

    // make sure we have active node and pan direction
    if(selectedBrickNode != nil && self.panDirection != nil){

        // contactTest
        let pw = scnScene.physicsWorld
        let node = selectedBrickNode.node!
        let contacts = pw.contactTest(with: node.physicsBody!, options: nil)

        var axisVector:SCNVector3
        // specify which axis we want to correct
        switch self.panDirection!{
            case "right","left": axisVector = SCNVector3Make(1,0,0)
            default: axisVector = SCNVector3Make(0,1,0);
        }

        for contact in contacts {
            // round contact normal to get a unit vector
            let cn = SCNVector3( round(contact.contactNormal.x),
                                 round(contact.contactNormal.y),
                                 round(contact.contactNormal.z))

            // fix only for pan direction axis
            if abs(cn.x) == axisVector.x && abs(cn.y)==axisVector.y  {
                let normal = contact.contactNormal
                let transform = SCNMatrix4MakeTranslation( round(normal.x) * -Float(contact.penetrationDistance),
                                            round(normal.y) * -Float(contact.penetrationDistance),
                                            round(normal.z) * -Float(contact.penetrationDistance))
                node.transform = SCNMatrix4Mult(node.transform, transform)
                // break to prevent repeated contacts 
                break;
            }

        }

    }
}


Share : facebook icon twitter icon
How can I remove all nodes from a scenekit scene?

How can I remove all nodes from a scenekit scene?


By : user3240831
Date : March 29 2020, 07:55 AM
Any of those help Hi I am trying to remove all nodes from my Scenekit scene but I cannot for the life of me figure out a way. , Try this (assuming you are using Swift):
code :
rootNode.enumerateChildNodes { (node, stop) in
        node.removeFromParentNode()
    }
SceneKit - Instant contact detection using physicsWorld.updateCollisionPairs()

SceneKit - Instant contact detection using physicsWorld.updateCollisionPairs()


By : Kaushik
Date : March 29 2020, 07:55 AM
it fixes the issue Looks like i just missplaced the code, the above code works fine if it's placed within the renderer(:willRenderScene) - i initialy tried to perform it directly within the method triggered by gestureRecogniser. Instead i just set the flag now and perform the check before next frame is rendered:
code :
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
    ... check for flag and perform contact test
}
Run SCNActions sequence with different nodes in SceneKit

Run SCNActions sequence with different nodes in SceneKit


By : Chirag Joshi
Date : March 29 2020, 07:55 AM
To fix this issue I know how to create a sequence of SCNActions in a single node, one by one, in SceneKit. But I would like to know, how can I make a sequence of SCNActions with different nodes? For example , you can use customAction.
code :
let actionA = SCNAction.customAction(duration: 1.5) { (node, elapsedTime) in
    nodeA.position.z -= 1.0
}

let actionB = SCNAction.customAction(duration: 1.5) { (node, elapsedTime) in
    nodeB.position.z -= 1.0
}

let sequence = SCNAction.sequence([actionA,actionB])
anyNode.runAction(sequence)
SceneKit two nodes are not colliding

SceneKit two nodes are not colliding


By : damanpreet singh Mat
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , After reading (SceneKit: How to detect contact without collision), I set both nodes to have static physic bodies and it works :)
Joining nodes in SceneKit

Joining nodes in SceneKit


By : Arun Kushwaha
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , you can group all your atom nodes under a common molecule node (that node doe snot have any geometry, just child nodes). When you move or scale the molecule, the effect will be applied on every atom.
shadow
Privacy Policy - Terms - Contact Us © voile276.org