As a follow up post to the Distance Constraint post, we can also create a maximum distance constraint using the same solution we found in the Distance Constraint post.
The previous solution created a fixed length distance constraint which forced a pair of bodies to be a given length apart. We can simply add an if statement to make this constraint a max length constraint.
A max length constraint only specifies that the distance between the joined bodies does not exceed the given maximum. So before applying the fixed length distance constraint just check whether the bodies have separated past the maximum distance. If not, then do nothing. Simple!
1
2
3
4
5
l = (pa - pb).getMagnitude();
if (l > maxDistance) {
// apply constraint
}
// otherwise do not apply the constraint
Thanks a million kajillion times over!!!! I really love these constraint tutorials; so far the simplest and clearest I've seen. Very helpful, and again thank you!!