Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a SCNNode drop using gravity?

Tags:

ios

scenekit

I have a SceneKit setup and have one Sphere in it that is setup as a Dynamic body.

I am able to run the app and see the sphere drop on the static body floor.

What I am trying to do is setup the scene so the sfere initially does not drop.

Then when a function is run I want the sfere to drop.

what is the correct logic / steps to make the scene suddenly (maybe when a button is pressed or something) drop the sphere?

I have tried also to set the sphere to mass 0 and then set the mass to 100 but it does not cause the drop...

like image 320
zumzum Avatar asked Dec 09 '22 05:12

zumzum


1 Answers

Mass doesn't control how fast something falls. This is true in the real world, but more so in simulations that take shortcuts instead of simulating every detail of real-world physics. (Sadly, iOS devices still don't have the CPU power to account for the rotating reference frame of the Earth, the Van der Waals attraction between your sphere and any body especially close to it, the strong force that keeps its triangles atoms together, etc etc.) In SceneKit, gravity is just a constant acceleration in a specific direction.

Setting mass to zero and switching it to something else interferes with the distinction between static/kinematic and dynamic bodies... so don't do that.

As @mnuages notes, you can add/remove the physics body from your sphere when you want it to be affected or unaffected by physics entirely.

But what if you want to keep the sphere's physics body for other reasons—such as allowing other bodies to collide with it even before you make it start falling? There are a few approaches you could use for that:

  • Set the sphere body's damping to 1.0.
  • Set the sphere body's velocityFactor to zero (at least in the direction of gravity).

Both of those will keep the sphere from moving when something else hits it. If you want the ball to get knocked around, but not be affected by gravity, the best thing to do might be to switch out scene gravity for physics fields:

  1. Set scene.physicsWorld.gravity to SCNVector3Zero.
  2. Add a SCNPhysicsField created with the linearGravityField constructor to your scene, and set its direction and strength to get the kind of gravity behavior you want.
  3. Set the categoryBitMasks on both your sphere body and the gravity field such that the field affects other bodies but not the sphere.

Whichever of these methods you use, you can change them when you want to "turn gravity on" for the sphere: reduce the damping, reset the velocityFactor, or change the sphere's or the gravity field's categoryBitMask.

like image 181
rickster Avatar answered Dec 11 '22 10:12

rickster