Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Conveyor belt - Friction

Discussion in 'Scripting' started by LectorDeva, Aug 20, 2014.

  1. LectorDeva

    LectorDeva

    Joined:
    Aug 20, 2014
    Posts:
    1
    Hello,

    Im currently working on a physics based puzzle game and i want to implement a conveyor belt that transports rigidbodies. I have already used the search function and found some posts where ppl made simple belts. But all this solutions are based on 2 strategies:
    1. Using multiple moving platforms or rotation tubes.
    2. Setting the velocity of all colliding rigidbodies to a fixed value.

    I dont like the first idea because i thinks its not very performant and would not look very smooth because there are always small gaps between the platforms.
    The 2nd solution would also not work for me because the player is allowed to throw objects of different shape/size/mass on the belt in any direction he likes. And i want a realistic friction behavior.

    I have already been brainstorming and mentioned that my problem would be solved if the friction, which affects my rigidbodies, would be applied relative to the speed of my conveyor belt instead of relative to Vector3.zero.
    As far as i know it is not possible to apply this behavior with the friction-parameters of a physic-material. Thats why i have tryed to implement this friction behavior by myself. To avoid unity to apply additional friction to the bodies i have applied a physic-material to my belt with a friction of 0. Without the script all object slide on the belt like on ice.

    Code (CSharp):
    1. public void OnCollisionStay(Collision collision)
    2. {
    3.     if (collision.rigidbody != null)
    4.     {
    5.         foreach (ContactPoint p in collision.contacts)
    6.         {
    7.             Vector3 tangent = Quaternion.AngleAxis(90, Vector3.forward) * p.normal;
    8.             Vector3 velocity = collision.rigidbody.GetPointVelocity(p.point);
    9.             Vector3 targetVelocity = tangent * Speed * Value;
    10.             Vector3 relativeVelocity = targetVelocity - velocity;
    11.  
    12.             float friction = Mathf.Abs(Vector3.Dot(relativeVelocity, tangent));
    13.             Debug.Log("Friction: " + friction);
    14.  
    15.             //Apply friction to the relative velocity (doesn't look realistic yet)
    16.             Vector3 newRelativeVelocity = relativeVelocity * friction * Time.fixedDeltaTime * DYNAMIC_FRICTION;
    17.  
    18.             Vector3 newVelocity = newRelativeVelocity - targetVelocity;
    19.  
    20.             collision.rigidbody.AddForceAtPosition(newVelocity, p.point, ForceMode.VelocityChange);
    21.         }
    22.     }
    23. }
    I am transforming the absolute velocity of the colliding bodies into velocity relative to the conveyorbelt-surface.
    After that i try to apply friction to this relativeVelocity.
    At last im transforming the newRelativeVelocity back into absolute space and change the velocity of the contact point.

    The transformation from absolute to relative space and back seem to work. But the friction im going to apply is not correct because my objects relativeVelocity is getting lower but never hit 0. And i guess thats because i have absolutely no idea how to calculate friction :p.

    It would be very nice if some friction-expert of you could give me the correct formula, or an easier approach to solve this.
     
  2. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    I will do some thinking.