Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question How do you make SpringJoint2D behave more like an actual spring?

Discussion in '2D' started by Sophophilia_Studios, Sep 7, 2023.

  1. Sophophilia_Studios

    Sophophilia_Studios

    Joined:
    Jul 28, 2015
    Posts:
    21
    My main issue is lateral movement seems to have almost no consideration to spring joints, only vertical movement. So if there is ever enough force applied to the spring, instead of absorbing that force into the spring, it just moves to one side of the other.
    I have a simple setup That you can see, a heavy object above, and then two "feet" below. and they are attached by springjoints. The problem is if I increase the frequency enough to bare the weight of the heavy object, the feet just shoot out to the side. This is totally not how springs act in real life, and I was just wondering if you all have simple, clean ways of making spring joints work more like actual springs.

    upload_2023-9-7_15-39-8.png

    I have tried many things, like combining a slider joint, with a spring joint. The problem with that is it completely gets ride of any lateral movement. I want some not none. I thought of creating 2 invisible objects to either side of the feet, and then attaching them with small springjoints, but that seems gross, and not really that good of a fix. Can I basically tap into the spring joint code, and make it apply to both lateral and vertical forces? I would imagine if it was that simple Unity would have done that, since that would make it behave like the actual thing it is named after...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    I've struggled with this too. I've never found a suitable solution, but one thing I have done for some spaceship feet is to use a FixedJoint2D and adjust its frequency property. Angle them outwards a bit and they will deflect laterally a bit, spreading upon load, especially if the lever arm is longer.

    With springs, I wonder if you also tied the two feet together with a spring if that would help things... * * * I just tried it... it keeps the feet together but by default it doesn't look right. Your mind "sees" the rubber band effect pulling the feet together and it intuitively looks... rigged.

    Perhaps by playing more with the damping and the forces? And also the damping on the Rigidbodies...
     
    Sophophilia_Studios likes this.
  3. Sophophilia_Studios

    Sophophilia_Studios

    Joined:
    Jul 28, 2015
    Posts:
    21
    Yeah I have done the spring joint connecting the 2 feet, and while it does help them not splay out, like you said it does not look right, Also this is just a simplified example, What I want to build is way more complex and irregular than this, So I am trying to find a solution that is more versitile, for example I want it to work right with say one foot. I just feel like I want to build a spring component that works like an actual spring, not some weird pivoting arm, vertical only "spring"

    The solution I am currently working on is making the arm out of a bunch of nodes all with spring joints connecting them, a mesh of sorts, Basically a node based soft body system. But it feels quite heavy and I was wondering if anyone had a more elegant, more performant system. I belive what I am trying to build is material plasticity simulation, and that is a very complex system so it makes sense that there is not a simple solution. But here's to hoping someone has an idea on how to do it cleanly
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Interesting, sort of like a truss?

    Hm, could be triangular or even cartesian square but with diagonal bucklers ("stiffeners" maybe? cross-connect spars?) across the middle of each square, like seismic retrofits on buildings.
     
    Sophophilia_Studios likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    SpringJoint2D isn't a compound constraint, it's a single linear constraint, it has no rotational constraint therefore it has no directionality at all.

    Well this obviously isn't "real life" and joints in physics engines are specific constraints, despite the words used so you need to use constraints/joints in combination to get the effect you require; smoke and mirrors and all that. :)

    Normally I'd say use a HingeJoint2D and its rotational limits however that doesn't use a spring/damper for its rotation constraint but is a hard rotational limit constraint. Adding one for each "leg" with its limit active will stop the motion however it won't be "springy". I'd be curious to look at that though and maybe adding in an optional angular constraint to the spring would be interesting to investigate. In the end, Unity exposes the Box2D joints with some tweaks.

    As Kurt says, one way to do it is to add a SpringJoint2D from the main body to the feet then add one between the feet configured with the same frequency/damping. I did this and I can increase the mass of the main body as long as I adjust the spring configuration to suit the impulses that such a body hitting a surface requires i.e. stiffness in the form of frequency. I guess this depends on the look you're going for.

    Without foot to foot spring:
    https://gyazo.com/9b938d3e70efd55bf9305324ab8641a6

    With foot to foot spring:
    https://gyazo.com/0108dd55d5e8851ad8ac419f79c7a922

    Unfortunately though, with a single foot/arm, there isn't a spring-torsion constraint available but you can place a Kinematic body orthogonal to it (which tracks the parent automatically) and use a SpringJoint2D against it to cause the kind of sideways spring motion you want; shown here:
    https://gyazo.com/1bf93c85f8697442210ea536473a8490


    NOTE: If you're using joints, ensure you increase the solver iterations it's allowed to use so it can get to a soltuion quicker. The default is just that and clearly isn't valid value for everything. This is especially true when using lots of chained joints in combination, especially when collisions are also being resolved.

    Hope that helps.
     
    Sophophilia_Studios likes this.
  6. Sophophilia_Studios

    Sophophilia_Studios

    Joined:
    Jul 28, 2015
    Posts:
    21
    Yeah thank you for that specificity, I should have said along the axis that the spring is on, or maybe to be even more clear, the same vector? But yes I understand that it is not technically vertical. This is actually part of the problem for searching for something like this, hahah. I tried several different ways to word it.

    Yeah I was playing with a combo of hinge and spring joints, And like you said it does not have the "Springy" nature to it, But it is still a step in the right direction. But I have not worked with them a lot and was having some issues with them, I plan to work on them more today.

    One solution I was trying to work on before calling it a night yesterday was basically trying to replicate a hydraulic foot. So I put 2 slider joints on the outside edges of the foot, and then put a spring joint in the center. The initial results didn't turn out how I wanted, but there is still plenty of tweaking to be had.

    This is very intersting! This is what I was trying to do when I was talking about 2 invisible objects to either side and then putting springs on them, but my tests did not yeild results like yours. your example seems to have the "side spring" Glued in place. If I could make it move "up and down" along with the "foot" then that could be very interesting! I will be trying this first. Thanks!!!


    I did not mess with this, and I definitely should, because like I mentioned the thing I am actually building is way more complex with loads of springs, and I have been getting a lot of weird results some times. Thanks again, you were a huge help
     
    Last edited: Sep 8, 2023
    MelvMay likes this.
  7. Sophophilia_Studios

    Sophophilia_Studios

    Joined:
    Jul 28, 2015
    Posts:
    21
    The problem I am running into is It seems like combining different joints causes tons of issues. For example hinge joints restrict all movement except rotational. While spring joints don't restrict rotation only axial movement. so because the hinge joint restricts all movement other than rotational using both effectively makes it as if only the hingeJoint is enabled.

    In short It does not work, Hahahaha. I was reaching similar issues with slider joints and springs. Slider joints restrict all movement except on the axis of the slider. So if you match up a spring joint with a slider, you can get a sort of suspension system, but there is just absolutely no lateral/rotational movement. I know there is a Configurable Joint for 3d where it would allow you to customize what I am trying to do (I think) it makes me sad there does not seem to be a built in solution for 2d :(
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    In 2023 you can turn off the positional constraint in the HingeJoint2D.

    There wouldn’t be any “lateral” movement in a SliderJoint2D because that’s the whole point of it. That’s like saying the problem with Red is that it’s not Blue. ;)

    Yes, there’s no customizable 2D joint.

    So you’re saying you can’t do what I suggested and demonstrated earlier?
     
  9. Sophophilia_Studios

    Sophophilia_Studios

    Joined:
    Jul 28, 2015
    Posts:
    21
    I solved it!! As I was writing out my last post, detailing how each joint has restrictions I was like, well.... I might not be able to combine them in one object, but I can change several together to make a complex system that works like I want!

    upload_2023-9-9_0-55-1.png
    So I made sort of "ball joint", with a hingeJoint, attached it to the big heavy body. And then made a "ankle" and attached it to the ball joint object with a sliderJoint and springJoint, on the same exact axis and then I made a "foot" and attached to the ankle with another hinge joint, so that it can make good contact to the ground. And it works really well,

    And while hinge joints don't have the springiness of spring joints it does have a bit of a bounce when it hits the limits. I am experimenting with directly adding counter forces to the angular velocity of the ball joint, and then make it exponentially stronger the farther the rotation is from the "target" rotation. But that is a "nice to have" not a requirement, so for now its working well enough to continue on with the project.

    Thank you both for your help!
     
    Chubzdoomer and MelvMay like this.
  10. Sophophilia_Studios

    Sophophilia_Studios

    Joined:
    Jul 28, 2015
    Posts:
    21
    I will play with this suggestion too, I was having issues with it moving naturally with the foot. In your example it seems stuck in one location too I was having that issue too. Also if it gets too high a force, it can push past anchor point, and then the spring will start to push it in the opposite way (I think) So it makes me a little wary to rely fully on it. but I think if I can get it to play with the system I made, then I could be a nice addition