Physics
The Physics Template demonstrates how you can use Physics in Lens Studio to dynamically simulate real-world physical effects and define 3D geometry for raycast or collision detection.
Note: To get to know more about Physics in Lens Studio , please check out the Physics Guide to see more detailed explanations about the concepts and scripting.
Guide
Raycasting
Raycasting is a powerful feature that is enabled by the physics component. To demonstrate how raycasting works with custom script. We have included a Drag Script in this template. You can find it at the top of the hierarchy under Physics Intro Examples.
This script allows you to drag any dynamic object in this template with the power of raycasting. Try interacting with any examples provided below to see how it works!
Examples
Under the Physics Intro Examples
there is a list of objects, each of them containing one example. We encourage you to toggle them one by one to explore different aspects of the Physics feature. Each of these examples is designed to visualize one aspect of using the Physics component. Let’s dive into the examples now!
Dynamic Setting
The Dynamic
settings makes an object interact with gravity and other forces. A dynamic object can be tossed, will fall and be moved around.
You can turn an object to static by unchecking the Dynamic
checkbox. If the Dynamic
checkbox is unchecked, the object will still react to other physical objects just like how a solid wall would influence other physical objects.
Fit Visual Setting
The Fit Visual
checkbox allows you to adjust the Physics Body's bounding box according to the size of your mesh. If unchecked, you will have options to give a custom size to your mesh.
Mass Setting
Changing mass on the Physics Body setting would determine how heavy your item would be. As shown in this example, the heavier box would ‘win’ its place in a seesaw match at any given day!
Density Setting
Density of the Physics Body will determine how ‘dense’ your object is. When 2 objects collide, the object with higher density will more likely knock off the other object and keep on maneuvering.
Physics Body Types
Different Physics body types can be made with the physics collider presets. Currently the types of colliders we support are: Box
, Sphere
, Cone
,Cylinder
and Capsule
.
You can also create a compound physics body by nesting different physics bodies to create a complex shaped collider.
Constraint Types
Setting up a constraint on the Physics Body is a great way to add interactivity to your Physics Bodies. In this template, we have provided a few examples of Physics Body constraints.
- Fixed Constraint - The constraint point would completely freeze physical movement of a physics body, essentially turning it into a static object.
- Point Constraint - This type of constraint would freeze a single point of a physics body.
- Hinge Constraint - This constraint would freeze a ‘side’ of geometry of the physics body.
- Point Constraint with Compound shapes - Constraints used with physics bodies of compound shapes.
To create a constraint, go to our physics object and click on the ‘Add Constraint Object’ button.
Then a constraint object would show up as a child of the Physics body. Move it around and change its Type to see how it affects your object.
Constraints cannot be moved (changes to their transforms after creation will be ignored). You can, however, move the target collider
to which the constraint is attached.
Advanced Example
This example lets you release a rubber duck into a pool of 100 balls. It showcases the capability of the Physics engine with the amount of collisions it can handle in real time.
A Physics constraint body is disabled by Behavior script at the top, then the main actor Ducky will fall into a pool of spheres, making a splash!
Try tapping on the screen to release the Ducky.
We recommend a maximum 200 physics objects for mobile devices and 100 physics objects for Spectacles (2021).
Kinematic Example
The kinematic example showcases different ways that changing properties of a non-dynamic Physics Body such as scale, rotation and position influences other Physics Bodies.
Multiple Worlds
The Multiple Worlds example demonstrates different objects being influenced by different gravity settings, one with regular and another with inverted gravity.
Each of these groups have a Physics World
parent that has a Physics World Settings
asset assigned. Feel free to check out and customize the settings to create new physical worlds!
Raycast Example
The Raycast example shows how you can cast a ray from any position of a certain length, and trigger an action based on the raycasting.
Similar approach can be used in many different ways. Check out the RayCast script attached to the Cannon Raycast Example to learn more on how to customize the Raycast interaction.
Collision Example
The Collision example shows how we can use existing animated objects with collisions to create new types of fun interactions with Physics! There are two examples under Collision Example. The first one using Behavior Script for collision where no scripting is needed. The other one is using the collision events API which uses script.
Collision Events with Behavior Script
In this example we have attached 2 sphere colliders to Mark the Monster. And presented a row of piano underneath. Every time when a piano key is collided with predetermined physics objects, a sound will be triggered.
The interactions are done by Behavior script with its Trigger
set as Physics Collider Event
. Check out the Behavior scripts attached to each piano key to see how it works!
Collision Events API
In this example, in addition to the sphere body collider, we also attached a CollisionEvents script to Mark the Monster
’s left and right feet. Check out the CollisionEvents
script to learn more on how to customize the collision events.
Overlap Event
An overlap event in physics means two objects’ colliders have overlapped. Unlike collision events, overlap events mean that two objects are intersecting without being bounced off.
Note that the Intangible
checkbox is checked on the tunnel physics objects so their colliders would be penetrable by other physics colliders.
There are also two examples under the Overlap Example. The first one using Behavior Script for overlap. The other one is using the overlap events API.
Overlap Events with Behavior Script
This example uses Behavior scripts to change colors of the Pizza Delivery folks on an overlapped event similar to how collision works in the previous example. Check out their Behavior scripts to see how it works!
Overlap Events API
This example uses the OverlapEvents
script for both tunnels. Check out the OverlapEvents
script to learn more on how to customize the overlap events.
Set Velocity
In this example, each chicken has an initial velocity when being instantiated. And they will collide with the cannon colliders to form a trajectory forward. There are also two examples under the Set Velocity Example
. The first one using Behavior Script. The other one is using the set velocity API.
Set Velocity with Behavior Script
The Set Velocity example showcases how to set force or velocity with Behavior script. You can find its velocity settings on the Behavior script attached to the object being instantiated on tap, which is attached to parent > Chick Physics Body
.
Check out the Force Type
and Mode
on its behavior script to try different settings!
Set Velocity API
This example uses the LaunchCannonBall
script for both tunnels. Check out the LaunchCannonBall
script to learn more on how to set velocity to a physics body.
Add Force
The AddForce
example allows you to add force with different properties by tapping on these objects. Each of these objects are pinned with a constraint and you can see how each object is affected by different types of forces added to them.
The type of force is added with tapping and the Raycast
script will trigger a function to apply force to certain objects based on ForceSetting
.
Check out the ForceSetting
script attached to each object to see how to apply custom forces to your physics objects!
Wind Zone
The Wind zone example simulates wind forces to affect objects inside the wind zone.
This interaction is done mostly with the WindZone
script that automatically adds forces to all objects inside the wind zone (an intangible
collider). Check out this script to learn more on adding forces on objects inside physics colliders!
Text 3D Physics
The Text3D component is a very convenient component that turns any of your written text into an interactive 3D object inside Lens Studio.
In this example, each text is being separated into a dynamic physics object with a box collider on tap. Try tapping on the screen and see for yourself!
All of these are done with the Text3DPhysics
script. Check out this script and try to customize its settings to create variants of 3D Texts with physics!
Face Examples
We have also included a simple demo on how physics can be used with tracking elements such as 3D face tracking.
You can explore this example inside the Face Camera Examples
object. We have included a pair of earrings and colliders tracked to the hand. Try moving your hand to interact with the earrings!
Previewing Your Lens
You’re now ready to preview your Lens! To preview your Lens in Snapchat, follow the Pairing to Snapchat guide.
Related Guides
Please refer to the guides below for additional information: