Search Unity

Questions about 2D Joints and vehicle rigs

Discussion in '2D' started by LaneFox, Sep 9, 2014.

  1. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,518
    Hello!

    I've gone through most of the 2D videos and documentation, but I don't have a clear understanding of how the Physics 2D Joints are really supposed to work. I have an idea of how they work but there aren't really any intermediate/advanced or really practical references for making vehicles or covering some of the Joints, like Wheel Joint 2D for example, or a vehicle example of a spring joint, constraining angles and trying to prevent the joint's from basically just being rubber slinkies.

    • Why do joints 'die' when under any sort of significant pressure?
    .

    Here I've got a rig that works decent until it hits the wall, or is actually just met with any significant opposition - this happens on slopes, coming down from a jump or something. The front wheel completely inverts and doesn't try to go back to its origin position. I've seen this happen on Wheel, Hinge and Spring joints.

    Why does this happen? Is something wrong with the setup?






    • Why doesn't a wheel joint hold to its angle of alignment?


    Now for this one its a bit extreme because I have a kinematic rigidbody that the wheel is attached to and pushing it pretty far up on the wheel. But, i still don't know why a wheel collider is allowed to move at all on these other axi. If it gets a force from the side it should just be forced on its allowed axis, right? Thats what happens when you hit bumps, the wheel goes up. If the bump is sharper, it goes up faster. Not backwards/forwards. Is this intended behavior? The rig actually performs decent under mild circumstances, but doesn't seem to handle higher speeds or pressure at all.

    The thing with this is that its not bad on simple stuff but once things start moving faster then it starts getting weird, I did a bunch of tests on a bunch of different types of rigs trying to get these wheels and general suspension to work right but it always seems to break down when I put it under load and complicate the playground for it. What does help is increasing the iterations in preferences but that also seems to stiffen up everything altogether.

    • Where are the extents controls?
    I hope I'm not completely missing something here, but there doesn't seem to be a way to designate hard min/max extents for any joint at all. Basically we have 1) Distance, 2) Dampening, 3)Frequency... But without designating min/max how can we configure any suspension system? I was thinking that the Physics2D here in Unity was basically the Box2D system shown in this video:


    If we can't define min/max for a joint then how do we simulate that when this bike does a wheelie and slams into a wall that the wheel joint/spring can only travel up its alignment 1 unit before shoving the remainder of any force up into the body and stopping right at the maximum?

    I've been trying lots of different rigs with no great improvement, so any feedback at all is appreciated, or any practical examples of vehicle rigs with the 2D system would be really helpful to take a look at as well.
     
    TomasJ likes this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    > Why do joints 'die' when under any sort of significant pressure?

    Box2D is tuned to work within a range of sizes, forces etc. Things like keeping the mass ratio of colliding objects no more than 10:1, sizes between 0.1m and 10m etc are important for a good simulation. This may be your problem, not sure, but large forces can also cause issues with joints. Also, it's really important to not just reposition rigid-bodies by modifying the transform.position as that causes overlaps.

    > Why doesn't a wheel joint hold to its angle of alignment?

    When you drag an object in the editor you are simply transporting an object from position to position potentially causing overlaps. When you do this, you can temporarily break things like joint constraints. During normal collisions, this shouldn't happen but dragging objects in the editor isn't normal movement/collisions.

    > Where are the extents controls?
    Unity exposes pretty much all the properties for joints so if you look-up the equivalent joint in the Box2D manual, everything should still apply which of course includes looking at Box2D tutorials etc. Box2D has a b2WheelJoint which we expose as WheelJoint2D but unfortunately it doesn't have min/max translation limits (which it badly needs) so we can't provide them yet. You could conceivably use a distance-joint to limit the maximum distance (use max-distance-only property which uses Box2Ds b2RopeJoint rather than the b2DistanceJoint.

    You might consider using a SliderJoint2D (b2PrismaticJoint) in combination with a HingeJoint2D (b2RevoluteJoint) as described here: http://www.codeproject.com/Articles/445461/Box-d-vehicles-part
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,518
    Hi Melv, thanks for taking the time to reply!

    I had read elsewhere that this was a kind of rule of thumb and FP might be related, but my sizes are pretty normal. I've done a setup to-scale along the 1 unit = 1m suggestion (where spring travel would be ~0.25m) and also arbitrarily made it much larger (spring travel ~2 units) but didn't notice it reacted different in regards to this particular issue. I briefly considered that the forces might just be too high when a fast object collides with a static object but given that the simulation isn't really in turbo-ultra hyperdrive it didn't seem applicable. I'll keep messing with it though.

    I do know about the oddities of transform vs physics, but it does happen outside of this scenario. For the most part it isn't horribly noticable, especially in cartoony rig-ups. In more practical rigs with hard connections though, joints (hinge in particular) will stutter away from their anchors when under normal play strain unless you set the iterations upwards of 50. I figured that was a bit unnatural, since the default is like 3 or 8 or something. I haven't tested that to see if its unacceptable in terms of optimization yet so maybe thats a totally viable fix and the iterations should just go way up for these sorts of scenarios? I first noticed it because I have a swingarm and the rear tire was skating away from the hinge position under normal circumstances until the iterations were cranked up.

    Ah, cool! I posted the video mostly as reference, but apparently I can dig a little deeper. Thanks for the link as well, I'll have another go at things and ping back with my findings.

    It would be handy if there were some notes on the Box2D implementation locally, for instance clarifying that SliderJoint2D should be treated as b2PrismaticJoint and HingeJoint2D (b2RevoluteJoint). Wouldn't it be easier to draw the relationship between existing Box2D material on the internet with the Unity integration? Does something like that exist already?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    > Wouldn't it be easier to draw the relationship between existing Box2D material on the internet with the Unity integration? Does something like that exist already?

    Those are good ideas. I'll investigate what the best way is to do that.