Elsewhere, I was attempting to get working controls/movement system.
My plan, at the time:
The green cubes would be solid and you’d “bounce” off those, but you collect the blue spheres.
That seems like a reasonable and approachable goal.
I am unsure what the red torus will do, just sit there and look cool I suppose.
I'm always a big fan of setting reasonable and approachable goals.
This was a great opportunity to learn more about the physics system that SceneKit gives you access to.
You can assign a “Physics Body” to a SceneKit node, and then it can be subject to various forces including gravity. There are three properties relevant to collisions/contact.
The category mask represents what type of object it is. Since we’re dealing with bitwise operations, it means that each unique value has to be a power of 2.
Entity | Decimal | Power of Two | Binary |
---|---|---|---|
Level Geometry | 1 | 2^0 | 0001 |
Player / Ship | 2 | 2^1 | 0010 |
Enemies | 4 | 2^2 | 0100 |
“Power Ups” | 8 | 2^3 | 1000 |
So, for example my PlayerShip is setup this way:
Mask | Value (Decimal) |
---|---|
Category | 0x0010 (2) |
Collision | 0x0101 (5) |
Contact | 0x1000 (8) |
So the physics system will have my physics body collide (resulting in the node stopping or bouncing when encountering the body it's colliding with), or generate a contact event indicating that I have run into a collectible power up.
Things like, to be inspired by Descent again:
And so, with a couple of things in place, I was able to test things out:
That's what the scene looks like after I have "collected" the blue spheres. The model bounces off of the green cubes.