In this example, the usage of gravity and object colliders in DeepAR Studio will be showcased by creating a simple ping pong mini game. In the game, the user controls a ping pong racket with their mouth and the goal is to hit the ball as many times as possible while keeping it in the air.
The steps to recreate this concrete example are given in the rest of the article. The given property values are all exemplary and can be adjusted if some value change doesn't result in the expected behaviour.
Open the pingPong.fbx file given in the zip that is located at the bottom of the article by selecting File→Import FBX or by pressing Cmd+I keyboard shortcut. The imported objects are located on the left in the hierarchy as nodes, under the root node RootNode.
To enable collider visualisation go to Physics→Debug→Show Colliders. The visible colliders allow for more precise proper alignment of the added colliders with the objects present in the respective physics world.
All descendants of the RootNode that contain one of the collider components can be made static for the effect creation process so that the made changes can be visible the process of making. The only non-static object will be Ball and after setting up the transformation and other parameters, the static property should be disabled. By making a certain node static, all of its descendants become static as well, locking their positions in the previously defined physics world.
In order to enable physics mechanics in the effect, a Physics World component needs to be added to RootNode by selecting the aforementioned node and then choosing Physics→Physics World. It is added in order to set core physics characteristics that will apply to the children nodes of the selected node recursively - in this case, all the descendants of RootNode in the hierarchy.
The changed properties in the added component, for this example, are:
Gravity: 0 150 0
stronger gravity in the positive y direction results in faster ball movement
Kinematic Knockback Factor: 1
the multiplier of the impulse applied by the racket (made out of static objects) to the non-static ping pong ball after their collision occurs
The RootNode node is also translated in the space in its Transformations component so that the imported objects correspond with the wished-for alignment - the user has the racket in their mouth with the ball positioned above the paddle so that it falls on the paddle:
Position: 0 -4 18
The ping pong ball is modelled as a sphere, located in the Ball node, and therefore a Physics Sphere Body collider is added with the value of the radius parameter being the same as the scaling of the Ball node itself. I.e., the Ball node is scaled in all axes by 1.75 (changed in the Transformations component), and the radius of the collider added through Physics→Physics Body→Physics Sphere Body should be set to the same value of 1.75. So the changed property values are:
Scale: 1.75 1.75 1.75
Physics Sphere Body component:
After finishing the ball's set up, it should be made non-static by disabling the Static Object property within the Physics Sphere Body component.
When colliding with the racket or the head, the ball's knockback speed can be tempered with by changing its mass - lighter mass amounts to faster speed, and vv.
To restrict the ball's movement in the space to only the x and y axes (which results in easier control of the ball and a lower chance of losing the ball in the 3D space since it can now only move in a 2D space - up-down and left-right), the z axis can be determined as fixed by ticking the checkbox next to the Z option in the Fixed Position property of the Physics Sphere Body component.
The ball's colour can be changed in its Material properties component. One of the available shaders should be chosen, and for this example the changed properties were:
Select Shader: Unlit Color
Color set to a warm shade of white
The Racket object/node is modelled as two different objects given as its two immediate children, Paddle and Handle.
Paddle is represented as a more complex object than the available colliders - the closest collider available is the Physics Box Body collider which is then changed to:
Static Object: enabled
changed from the minimum value of 0 to the maximum 1 so that the ball can bounce off the paddle (increases the bounciness percentage)
Extent: 8.5 9 0.9
proportions of the collider in order to match the true shape of the paddle as close as possible
The paddle's colour can be changed in its Material properties component. One of the available shaders should be chosen, and for this example the changed properties were:
Select Shader: Standard shader full
Diffuse color set to a shade of red.
Ambient color set to a slightly cooler shade of red than the previous one.
Directional light color/Point light 1 color/Point light 2 color all set to white.
Lights intensity: 1 0 0
Directional light: 1 1 1
Point light 1 position/Point light 2 position: 0 0 0
The Handle object closely resembles a capsule and the Physics Capsule Body collider is then used to create its collider. The default colliders in the Studio have fixed rotation values (if added to the object itself, they inherit the position, but not the other properties) so, in order to match the available handle object, a new empty node needs to be added as the child of the Handle node by selecting the Handle node and choosing Node→Add Empty. That empty node can then be arbitrarily renamed (in this example, PaddleCollider), the Transformation component can be adapted to be properly aligned with the Handle object and the Physics Capsule Body collider can be then added to this new node.
Positon 0 0 0.4
Rotation: 90 0 0
Physics Capsule Body component:
no need to explicitly enable since the parent node is also static - it inherits the property value
the length of the collider between the two sphere hemispheres on each end
The handle's colour can be changed in its Material properties component. One of the available shaders should be chosen, and for this example the changed properties were:
Select Shader: Standard shader
Diffuse color/Specular color set to a shade of brown
Ambient color set to black
Directional light color/Point light 1 color/Point light 2 color all set to white
Lights intensity: 1 0 0
Directional light: 0 0 1
Point light 1 position: 0.5 0 0.5
Point light 2 position: -0.5 0 0.5
An optional step is the addition of a collider for the user's head. The collider is added in the form of a simple capsule collider that mimics the head shape.
Select RootNode (making it the parent of the new node) and create a new node using Node→Add Empty
Give the node an arbitrary name, in this example HeadCollider was chosen
Add the capsule collider component using Physics→Physics Body→Physics Capsule Body
Set these values within the newly added Physics Capsule Body component:
Change the Transformations component so that it properly aligns with the user's head
Position: 0 5 -24
The hierarchy should look like this:
Since the ball is the only non-static object in the world, only its position will be reset every time the effect is reopened, by pressing the Reload button located on the upper right of the window or every time some value of any object in the effect is changed. Other (static) objects stay in the same position relative to the root node's origin point.
The final effect can also be obtained by opening the physics_pingPong_finished file as an already existing effect using File→Open effect or Cmd+O keyboard shortcut in the Studio.
After trying out the example, freely play around with the recommended settings to explore the possibilities of the represented physics mechanics.
Download the required .fbx file and the final effect