Physics Toys
To make it easier for you to mix-and-match our different examples, this template has been converted to an importable asset in the Asset Library with the name Physics Maze
and Physics Racetrack
.
The Physics Toys template demonstrates the following physics features in Lens Studio:
- Physics Matters
- Per-object world settings
- Cone and Cylinder collider shapes
To learn more about Physics in Lens Studio, visit the Physics Guide.
Guide
The Physics Toys template includes two examples:
- Maze example
- Learn about Physics Matters and customize Physics Matters variables such as friction and bounciness.
- Race Track example
- Build a toy car circling a racetrack with Physics.
Open the template to see both examples in the Scene Hierarchy
panel.
Maze example
In this example, a maze attaches to your head. As your head rotates, the maze rotates with it, and a ball rolls around the maze according to physics.
Physics World Settings
Each physics object has a Physics World parent with a Physics World Settings asset assigned to it.
View the Physics Front Camera World Settings asset in the Physics World Settings folder under the Asset Browser
panel. Inside Physics Front Camera World Setting, you can set the following variables:
- Gravity
- Default Matter
- Simulation Rate
- Slow Step
- Slow Time
- Max Speed
- Max Relative Speed
To learn more about Physics World Settings, visit Physics Assets and Components.
Drag Script
The included custom script, Drag Script, enables you to drag objects using raycasting. Raycasting projects a ray into a scene and determines whether the ray collides with any object in the scene. In this template, a ray is cast when you tap on the screen. If the ray collides with some object, the object follows your finger until you release the screen.
Try dragging the objects in the scene listed below.
Physics Matters
Attach a Physics Matter asset to each Physics Body and Physics Collider component. Within each Physics Matter asset, you can adjust the component’s friction and bounciness.
Navigate to Examples > Maze Example > Camera > Maze World > Physics Objects to see physics objects with different Physics Matters. For example, notice that the bounciness of the beach ball is higher than the baseball. Even though the Physics Body shapes are the same, the balls bounce differently after colliding with the floor.
All of the Physics Matter resources used in this template are in the Physics Matters folder under the Asset Browser
panel.
To learn more about Physics Matter, check out Physics Assets and Components
BindTransform script
Once the physics simulation starts, the physic objects (scene objects with dynamic Physics Body components) move in world-space. The Head Binding effect allows the maze physics objects to track your head. Since you cannot make the Physics Object a child of the Head Binding, utilize the BindTransfrom script to bind the Physics Object's initial transform to a pivot point on the Head Binding object.
Click on Examples > Maze Example > Camera > Maze World > Physics Objects > Single Physics Body and Complex Physics Body With Constraint to see how this is set up.
Enable Smoothing
Click on Examples > Maze Example > Camera > Maze World > Effects > Head Binding > Head Mesh.
The maze object consists of many box colliders. Click on any collider in the mesh to find the Enable Smoothing option. The Enable Smoothing option smoothly interpolates between changes to help reduce noise. This template uses Enable Smoothing to reduce motion tracking noise.
Enable the Show Collider field to see the smoothing effect.
Overlap Events with the Behavior script
The Maze example use a Behavior script to:
- Set up overlap zones for Physics Overlap Events.
- Trigger the rotation of each floor.
- Trigger the position of the lift.
View each Behavior script to see how it's configured.
Race Track example
The race track example applies torque to the wheels of a toy car. When friction is enabled on the wheels and race track, the car moves forward.
Disable the Maze Example before testing the Race Track Example.
The race track example utilizes the device’s rear camera. To test the example:
- Click the Switch Camera button in the Preview panel.
- Tap on the screen to enable the car.
- Keep pressing on the screen to move the car forward.
Physics World Settings and slow motion
Just like in the maze example, all of the physics objects are children of the Race Track World parent. The Race Track World is assigned the Physics Back Camera World Settings asset.
Click on Examples > Race Track Examples > Race Track World > World Object Controller > Track > Slow Motion Zone > Overlap Zone.
The Overlap Zone’s Behavior script calls the functions in SlowMotionSetting.js
. Whenever the car passes through the Slow Motion Zone, the script triggers the slow motion effect. Modify the effect’s Slow Step and Slow Time attributes in the Physics Back Camera World Settings asset.
Collision Events with the Behavior script
This example also uses Behavior scripts to handle Physics Collision Events and to change the Wall alpha to show the border of the Ground Collider and Wall Colliders.
BindTransform script
Click on Examples > Race Track Examples > Race Track World > Physics Objects > Complex Physics Body With Constraint and Complex Physics Body With Multiple Objects.
Like the first example that binds the initial pivot point of the Physics Object to your head, the car and traffic cones use a BindTransform script.
Car
Click on Examples > Race Track Examples > Race Track World > Physics Objects > Complex Physics Body With Constraint > Car.
The car is made of two different types of components: Physics Body and Constraints.
Per-object World Settings
View the Balloons object located within the Car object. Balloons are generated using ObjectsLayout.js
on the Awake Event.
Click on Balloon 1 under Balloons. Notice the Physics Balloon World Settings attribute assigned to the Physics Body Component.
Click on Physics Balloon World Settings in the Physics World Settings folder under the Asset Browser
panel. The balloons have a different gravity setting which creates a floating effect.
If you increase the gravity setting, the mass of the balloon's physics body components, and the balloon count in ObjectsLayout.js
, you can cause the upward force from balloons to be greater than the downward force from the car.
Cone and Cylinder colliders
Click on Examples > Race Track Examples > Race Track World > Physics Objects > Complex Physics Body With Multiple Objects > Traffic Cone Layout > Traffic Cone.
The Traffic Cone is formed from a Cylinder Physics Body and a Cone Physics Body.
To learn more about the Physics Body Component, visit Physics Assets and Components.
Apply force
Click on Examples > Race Track Examples > Race Track World > Physics Objects -> Complex Physics Body With Constraint > Car.
Notice the addRelativeTorque
method within WheelController.js
. The addRelativeTorque
method applies torque to each of the four wheels when you tap the screen without dragging an object.
Set the friction between the racetrack and the wheels in the Wheel Physics Matter, Wood Floor Physics Matter, and Wood Ramp Physics Matter assets. The combination of gravity, torque, and friction moves the car forward.
Preview your lens
To preview your Lens in Snapchat, follow the Pairing to Snapchat guide.