user6452266
user6452266

Reputation:

Applying simple Physics to a .scn object in SceneKit XCODE SWIFT

Hey Below I have a normal sphere that I created just to test if my in game scene/world has physics. So I simply put the ball in the scene/world and it is perfect. It is affected by the gravity. So then I try to do the exact same thing to the .scn file. I give it physics the same as the test sphere that falls down do to gravity. but the man doesn't move. The gravity is set to -9.8 to simulate regular gravity Code:

  //----Test-Circle-here--------------------

    var sphere1: SCNNode!
    let sphereGeometry = SCNSphere(radius: 10.5)
    let sphereMaterial = SCNMaterial()
    let collisionCapsuleRadius = CGFloat(0.4 - 0.4) * 0.4
    let collisionCapsuleHeight = CGFloat(0.4 - 0.4)
    sphereMaterial.diffuse.contents = UIColor.greenColor()
    sphereGeometry.materials = [sphereMaterial]
    sphere1 = SCNNode(geometry: sphereGeometry)
    sphere1.position = SCNVector3(x: 1.0, y: 0.05, z: 0.05)

    //----Giving it a physics---------

    sphere1.physicsBody?.affectedByGravity = true
    sphere1.physicsBody?.friction = 0
    sphere1.physicsBody?.restitution = 1 //bounceness of the object
    sphere1.physicsBody?.angularDamping = 1 // rotationess
    sphere1.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius, height: collisionCapsuleHeight), options:nil))
    scnView.scene!.rootNode.addChildNode(sphere1)

Below the man Stays in same- spot no matter what some how

     class Character {

let node = SCNNode()
init() {

    let GuyScene = SCNScene(named: "art.scnassets/FoxMan2.scn")
    let characterTopLevelNode: SCNNode = GuyScene!.rootNode.childNodeWithName("Guy", recursively: true)!
    let collisionCapsuleRadius = CGFloat(0.4 - 0.4) * 0.4
    let collisionCapsuleHeight = CGFloat(0.4 - 0.4)
    characterTopLevelNode.position = SCNVector3(x: 10.0, y: 0.0, z: 0.0)

    //----Giveing it a physics---------

    characterTopLevelNode.physicsBody?.affectedByGravity = true
    characterTopLevelNode.physicsBody?.friction = 0
    characterTopLevelNode.physicsBody?.restitution = 1 //bounceness of the object
    characterTopLevelNode.physicsBody?.angularDamping = 1 // rotationess
    characterTopLevelNode.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius, height: collisionCapsuleHeight), options:nil))
    node.addChildNode(characterTopLevelNode)

 }
}

enter image description here

enter image description here

Upvotes: 1

Views: 2335

Answers (1)

Joshua Smith
Joshua Smith

Reputation: 6621

Check to make sure that your characterTopLevelNode is actually the node you think it is (name mismatches are pretty common). That's often the problem in these kinds of situations. You seem to be adding the characterTopLevelNode as a child of node but never adding node as a child of the scene being displayed.

One other thing, don't set the options on the physics body before you create the physics body.

For example: sphere1.physicsBody = SCNPhysicsBody(type: .Dynamic, ... sphere1.physicsBody.affectedByGravity = true sphere1.physicsBody.friction = 0 sphere1.physicsBody.restitution = 1 sphere1.physicsBody.angularDamping = 1

Upvotes: 2

Related Questions