0
votes

I need some help in understanding how forces are applied to physics bodies in SceneKit.

My SCNSphere is defined as follows:

    let startsphere = SCNSphere(radius: CGFloat(0.2))
    startsphere.firstMaterial?.diffuse.contents = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.3)
    let startsphereNode = SCNNode(geometry: startsphere)
    startsphereNode.position = SCNVector3(0.0, 1.0, 0.0) // center of playfield, 1m height
    startsphereNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
    startsphereNode.physicsBody?.isAffectedByGravity = false
    startsphereNode.physicsBody?.friction = 0.0
    startsphereNode.physicsBody?.rollingFriction = 0.0
    startsphereNode.physicsBody?.damping = 0.0
    startsphereNode.physicsBody?.restitution = 1.0

This sphere is enclosed in a box made up of 6 planes where the sphere shall bounce off from. The collision masks and are set so that the sphere bounces off these walls and I can observe that when I move the sphere with an SCNAction.

Now I apply a force like this:

  startsphereNode.physicsBody?.applyForce(SCNVector3(50000,0,0), at: startsphereNode.position, asImpulse: false)
  // or like this
  startsphereNode.physicsBody?.velocity = SCNVector3(200,0,0)

I observe the behavior of the sphere in the scene. When the force is applied (or the velocity is set) the sphere instantaneously jumps far outside the box to the right and then slowly moves back (in leftward direction ) to its previously set position (0,1,0). I can see it moving through the wall.

Now my questions:

  1. Why does it jump (und not move) to the right ? I have played around with the impulse and velocity magnitudes, but always the same.

  2. Why is it moving back to it's original position once the force is applied ?

  3. Why does it not bounce off the right wall ? I see it "shot through" the wall.

The behavior I would like to achieve is after application of the force the sphere bouncing inside my box according to the physics set up.

Any thoughts ? Thanks.

2

2 Answers

1
votes

Just in case this might help others:

The problem was that my sphere also had an active SCNAction for rotation. At the moment I removed all the SCNActions it started to work as expected.

Outcome: Don't use SCNActions and physics simulation at the same time !

Regards Chris

0
votes

Some quick answers to your questions....

Why does it jump (und not move) to the right ? I have played around with the impulse and velocity magnitudes, but always the same.

you probably aren’t using timers. If you put you actions in timers and hold down buttons, or joysticks etc... you can move them on a loop with a timer duration.

@objc func rapidMove(_ timer: Timer) {
       self.sceneView.box.runAction(SCNAction.move(by: SCNVector3(-0.1,0,0), duration: 0.001))

    }

Why is it moving back to it's original position once the force is applied ?

your probably not using the presentation node to update the position.

Presentation Node - developer site

Why does it not bounce off the right wall ? I see it "shot through" the wall.

You have to setup collisions.

need to setup at least two collision categories

 struct CollisionCategory: OptionSet {
let rawValue: Int

static let sphere  = CollisionCategory(rawValue: 1 << 0) // 00...01
static let box = CollisionCategory(rawValue: 1 << 1) // 00..10
  }



startsphereNode.physicsBody?.categoryBitmask = CollisionCategory.sphere.rawValue
startsphereNode.physicsBody?.contactTestBitMask = CollisionCategory.box.rawValue

You have to do the same for your box node but the reverse

boxNode.physicsBody?.categoryBitmask = CollisionCategory.box.rawValue
boxNode.physicsBody?.contactTestBitMask = CollisionCategory.sphere.rawValue