« MBS Xojo Plugins, ver… | Home | FAQ for our Birthday … »

Physics example for SceneKit in Xojo

In this article I want to show you an example with the physics of SceneKit. The physics functions of SceneKit are available in 20.2 pre-release version of our MBS Xojo Plugins.

In this example two pyramids should hit each other. We will create pyramids that move in the direction of each other. We will not only create two pyramids but several with the help of a timer.

We start with a normal and empty SceneKit Scene. The SceneKit Control is located in the middle of our window. We add a timer to our window and open its action event where we want to insert our code.

First we create our pyramid geometry. Then we place the pyramid as a normal node in the scene with the position (5,0,0) on the right side. We repeat it with the second pyramid and place it at the point (-5,0,0) on the left side:

Dim PyramidGeometry As New SCNPyramidMBS(1, 1, 1)
PyramidGeometry.firstMaterial.diffuse.contents = NSColorMBS.redColor
Dim Pyramid1 As New SCNNodeMBS(PyramidGeometry)
Pyramid1.Position = New SCNVector3MBS(5, 0, 0)

Dim Pyramid2 As New SCNNodeMBS(PyramidGeometry)
Pyramid2.Position = New SCNVector3MBS(-5, 0, 0)

Then we create another pyramid geometry. We use this one to assign the nodes physical shape. This physical shape does not necessarily have to be the same geometry as the node. For example you can use the shape of a sphere. In this case the node would be displayed as a pyramid in the scene, but would physically behave like a sphere.

But in this example we want the pyramid to behave physically like a pyramid, too.

Dim phGeometry As New SCNPyramidMBS(1, 1, 1)

Then we connect in the property physic Body of the nodes to the physical shape.

Pyramid1.physicsBody = New SCNPhysicsBodyMBS(1, New SCNPhysicsShapeMBS(phGeometry))
Pyramid2.physicsBody = New SCNPhysicsBodyMBS(1, New SCNPhysicsShapeMBS(phGeometry))

Then we have to indicate how our pyramids should move. That means what is the start impulse the pyramids get. So we set the vector with the direction of motion. If we would only set values on the y-axis, our pyramid would first be moved upwards and then would fall down without an arc. If we additionally specify an x-vector we see that an arc movement is created. This is because the object moves forward with a certain force. During the flight the force decreases, so that the object falls down after reaching the peak.

With the method applyForce we set the previously defined vector.

Dim force1 As New SCNVector3MBS(-10, 10, 0)
Dim force2 As New SCNVector3MBS(10, 10, 0)
Pyramid1.physicsBody.applyForce(force1, True)
Pyramid2.physicsBody.applyForce(force2, True)

You can also specify other physical properties such as the mass of a node and see what effect this property has.

Pyramid1.physicsBody.mass = 3.2

Finally we insert the nodes into our scene and make settings for the timer.
If we now run our application we see that the pyramids influence each other' s fall

I wish you a lot of fun with the new physics functions in SceneKit. The example is included with 20.2 pre-release version of MBS Xojo Plugins.
by Stefanie Juchmes The biggest plugin in space...
07 04 20 - 12:27

OmegaBundle 2022 with some MBS products

Start Chat