[Example Project] Chain Physics with Two Anchors
FeaturedHi again!
Last time we talked about how we can modify the chain controller script to add a collider. This time, we’ll go over how we can modify the chain controller to simulate a chain pinned to two points.
To do this, let’s consider how the chain controller works. The first point in the chain is updated according to its world position (let's call it an anchor), and then every point after has its position calculated based on its distance from the previous points position.
Let's try to move the last point (make the second anchor). When we change last point position we see that position of a point before that will not be recalculated, because we have only forward constraints. What we need to do in this case is to add another set of constraints from the end to the beginning of the chain. And also add some iterations.
We’ll start by setting up the Chain Controller as usual. Take a look at the template for more information
Next we will modify the chain controller based on our idea above. Take a look at the chain controller script in the example project to see the complete script.
For each pair of chain links we will create a pair of distance constraints - forward (first anchor) and backward one (second anchor).
for (var i = 0; i < script.joints.length; i++) {
if (i > 0) {
var c = new global.Constraint(points[i - 1], points[i], script.stiffness, script.type == 0);
constraints.push(c);// forward constraints
links[i - 1].startDir = points[i].getPosition().sub(points[i - 1].getPosition());
}
if (i < lastIndex) {
var c = new global.Constraint(points[i + 1], points[i], script.stiffness, script.type == 0);
constraints.push(c); //adding back constraints
}
}
And in updatePhysics function we will update last link, as well as the first one.
points[lastIndex].setPosition(global.MathLib.vec3.fromEngine(links[lastIndex].transform.getWorldPosition()))
For this example, we will use the elastic chain calculation. Since this type of chain is more dynamic, let's increase our calculation iterations from 3 to 5 to make it more stable. At the picture you can see how point positions converge with iterations
Let’s take a look at what we’ve got at this point:
Looks great! Let’s now use this technique to create a real Lens.
I’ve created a face piercing chain in Blender, rigged it to the bones, and imported it into Lens Studio.Then, I set up a chain using updated ChainController script as above.
Now that the chain works, let's attach it to the face. To do this, we’ll pin the ends of our chain to a face mesh - that way as the user changes their face, our chain will continue to be pinned to the same part of the face. In this case, our Face Mesh is the same one we use to occlude anything behind the user’s face.
Approximate nostril attachment setup:
Approximate ear lobe attachment setup:
Let’s add some final touches. We can add other objects to pin to the same face mesh, as well as imported makeup from the Makeup Template to complete the look. (You can see a stream recording about mixing different templates)
To recap:
- Added a new set of constraints from the each next chain to the previous one
- Set up a rigged chain model
- Pinned first and last chain to the face mesh
- Added Makeup controller and post effects
Have fun!
Wow this is great! Such an in-depth walkthrough, thank you Olha!
I was just about to make a post asking for exactly this. Thanks!
Is there a way to do this with just a simple chain of spheres too - like in the Chain Physics tutorial?
Hi, Dweeh
Yes, this project has a setup for spheres included!
Please look for the Test sceneObject
Best
Olha
Oh... :) Thanks!
I tried to mess around with it a lot - but I can not make it do what I want.
Is it because it is rigged that you can make your chain links stay together without space between the links?
Hi, Dweeh!
To achieve this please try to set type to Elastic and Iterations to 4-5
Best
Olha
Project was not opened! !extractDataFiles:No data and scene file missing
Hi, TTT!
I just tried both on Windows and MacOS machines and it works for me.
I am using the most recent build from Lens Studio Website
Could you please try again and provide more information if you still experience the issue
Best!
Olha
Still the same error even though I am using version 2.3.1
Hi TTT!
Did you try to delete the file and download it again - just to make sure the file is not damaged?
hi:Dweeh
Yes, I deleted the file and uploaded it more than once and the problem continues
https://www.snapchat.com/unlock/?type=SNAPCODE&uuid=b6d249bf4ead4f9b9668c3bf96edb855&metadata=01
I want a way to make such a lens, can you help me?
Olha
Do you have a example project where the chain is attached to the shoulders like a necklace would be?
❤️❤️❤️