Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Weigh each child box Collider in a rigidbody

Discussion in 'Physics' started by b4cksp4ce, Dec 21, 2014.

  1. b4cksp4ce

    b4cksp4ce

    Joined:
    Apr 13, 2011
    Posts:
    114
    Hi, I'm starting a new game and it involves some kind of "crane".

    My whole object crane has a rigidbody on the root object and each of its child has a boxCollider. As expected, the centerOfMass of the crane rigidbody changes when one of the boxCollider moves.

    I'm wondering if it's possible to weigh each of these colliders because the whole weight is not supposed to be equally distributed. Let's say we have a big mass at the end of the crane, what can I do to simulate this kind of behavior ?

    EDIT : While I'm at it, I know that if you do not set the "centerOfMass" it is automatically calculated depending of the Colliders of the childs. It this possible to set just an offset while keeping this "automated" behavior ? let's say substract 1 from the Y axis to get a more stable crane ?

    I tried to put a rigidbody as well on the objects with the boxCollider but the objects started falling on the ground and I don't think the parent rigidbody would add up these weight also.

    I also tried the variable "attachedRigidbody" of the colliders but that returns me the rigidbody of the main object.

    Thanks for your time.
     
    Last edited: Dec 21, 2014
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    Offset would just be doing:

    rigidbody.centerOfMass = rigidbody.centerOfMass + new Vector3(0f, -1f, 0f);

    If you want to tune the center of mass more manually a good solution would be a simple script that sets center of mass with a gizmo in OnDrawGizmos to visualize where the center of mass is at edit time...
     
  3. b4cksp4ce

    b4cksp4ce

    Joined:
    Apr 13, 2011
    Posts:
    114
    Thanks but when I set the centerOfMass manually in the script, it cease to be automatically updated whenever I move the part of the crane around !

    Moreover, I already have a gizmo to see where my center of mass is at anytime on the scene mode but I actually want is to make, for example, one box collider more "important" (by its weight) compared to other so that the centerOfMass would be offset more to this boxCollider more than the others.

    Sorry if I'm not very clear !
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    Then you'd just need to re-apply the center of mass offset whenever you move a part of the crane.

    Changing the collider makeup of a rigidbody is a pretty heavy operation internally, though, and also runs the risk of weird physics motion if you regularly do it. You might want to look into multiple rigidbodies in some kind of joint relationship instead.

    I'm not sure if you're building a simulation or a game, though, so maybe that would feel too game-y for a simulation. I did a pretty simplified crane setup years for Crane Wars, but wasn't at all intended to be simulation-levels of realism:
     
  5. b4cksp4ce

    b4cksp4ce

    Joined:
    Apr 13, 2011
    Posts:
    114
    I must missing something because I don't have the same behaviour as yours.

    When I'm not setting the centerOfMass, it moves around automatically whenever I move one part of it (i.e translate the arm).

    If I set it (in the "Start()" or "Update()" function), it cease to be updated by the ridigbody and it stayed where I set it. I can move every child Collider, it won't move.

    I know "crane wars" but what I'm building is more of a simulation "serious" game so it has to be some kind of "precise" :).

    For my weighing problem, what if I set (in the Update()) the centerOfMass by being the weighted average of all the childs colliders localPosition ? Would that work ? Is that what the ridigbody Do "automatically" ?