Make SCNNode fall using gravity?
I have a SceneKit setup and have one sphere that is configured as a dynamic body.
I can run the application and see a sphere fall on the static floor of the body.
What I'm trying to do is set up the scene, so sfere doesn't crash at first.
Then, when the function runs, I want the sfere to be discarded.
What is the correct logic / steps to suddenly make the scene (maybe when a button or something is pressed) lower the sphere?
I have also tried setting the sphere to mass 0 and then setting mass to 100, but it doesn't cause it to fall ...
source to share
Mass has no control over how quickly something falls. This is true in the real world, but even more so in simulations that instead use shortcuts to simulate every detail of real world physics. (Unfortunately, iOS devices still lack the processor power to account for the Earth's rotating frame of reference, the van der Waals attraction between your sphere and a body especially close to it, the strong force that holds the trianglesetc.). In SceneKit, gravity is simply constant acceleration in a specific direction.
Setting the 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 / delete the physical body from its scope, if you want to be fully affected or not affected by physics.
But what if you want to keep the physical body of the sphere for other reasons, such as allowing other bodies to collide with it even before you start falling? There are several approaches you could use to do this:
- Set the body of the sphere
damping
to 1.0. - Set the body of the sphere
velocityFactor
to zero (at least in the direction of gravity).
Both of these will keep the sphere from moving when something else hits it. If you want the ball to be knocked over without being affected by gravity, your best bet would be to turn off scene gravity for physical fields:
- Install
scene.physicsWorld.gravity
inSCNVector3Zero
. - Add the
SCNPhysicsField
one created with the constructorlinearGravityField
and set itdirection
andstrength
in get the kind of gravity you want. - Place the
categoryBitMask
spheres on both your body and the gravitational field so that the field acts on other bodies, but not on the sphere.
Whichever of these methods you use, you can change them if you want to "rotate gravity on" for the sphere: decrease damping
, reset the, velocityFactor
or change the sphere or gravitational field categoryBitMask
.
source to share
As of iOS 9, you can set the property isAffectedByGravity
to false
and then flip the value to true
to make the sphere fall: https://developer.apple.com/reference/scenekit/scnphysicsbody/1514738-isaffectedbygravity
source to share