Skip to main content
Version: 5.x
Supported on
Snapchat
Camera Kit

Physics

Physics sample project is available on Lens Studio Home Page.

The Physics sample project demonstrates how you can use physics in Lens Studio to dynamically simulate real-world physical effects and define 3D geometry for raycasting or collision detection.

To learn more about physics in Lens Studio, please refer to the Physics guide for detailed explanations about the concepts and scripting.

Guide

Raycasting

Raycasting is a powerful feature enabled by the physics component. To demonstrate how raycasting works with custom scripts, we have included a Drag Script in this sample project. 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 sample project using raycasting. Try interacting with any examples provided below to see how it works!

Examples

Under Physics Intro Examples, there is a list of objects, each 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 showcase one aspect of using the physics component. Let's dive into the examples now!

Dynamic Setting

The Dynamic setting makes an object interact with gravity and other forces. A dynamic object can be tossed, will fall, and can be moved around.

You can make an object static by unchecking the Dynamic checkbox. If unchecked, the object will still react to other physical objects, similar to how a solid wall influences 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 specify a custom size for your mesh.

Mass Setting

Changing the mass on the Physics Body setting determines how heavy your item would be. As shown in this example, the heavier box will “win” its place in a seesaw match on any given day!

Density Setting

The density of the Physics Body determines how “dense” your object is. When two objects collide, the object with higher density is more likely to knock off the other object and continue maneuvering.

Physics Body Types

Different physics body types can be created with the physics collider presets. Currently, supported colliders include Box, Capsule, Cone, Cylinder, Mesh and Sphere.

You can also create a compound physics body by nesting different physics bodies to form a complex-shaped collider.

Constraint Types

Setting up a constraint on the Physics Body is an excellent way to add interactivity. In this sample project, we provide examples of Physics Body constraints:

  • Fixed Constraint: Completely freezes the physical movement of a physics body, turning it into a static object.
  • Point Constraint: Freezes a single point of a physics body.
  • Hinge Constraint: Freezes a side (axis) of the physics body geometry.
  • Point Constraint with Compound shapes: Used with physics bodies of compound shapes.

To create a constraint, go to our physics object and click on the Add Constraint Object button.

A constraint object will appear as a child of the Physics body. Move it around and change its type to see how it affects your object.

alt text

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, showcasing the Physics engine's capability to handle a large number of collisions 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 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 existing animated objects with collisions can create new interactions using Physics. There are two examples under Collision Example:

  • Using Behavior Script for collision, requiring no scripting.
  • Using the collision events API with scripting.

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!

Previewing Your Lens

You’re now ready to preview your Lens! To preview your Lens in Snapchat, follow the Pairing to Snapchat guide.

Was this page helpful?
Yes
No