Search Unity

Springjoint transmit wrong forces

Discussion in 'Editor & General Support' started by BazLevelUp, Aug 10, 2012.

  1. BazLevelUp

    BazLevelUp

    Joined:
    Jun 27, 2012
    Posts:
    36
    Hello!

    We have a problem with physic here ^^



    So I have a rectangular plateform (perfectly flat) that has a boxCollider (not trigger) and a rigidody. I have above it 3 little cubes with boxCollider (not trigger) and a rigidody (with gravity)

    PROBLEM IS : the 3 littles cubes above the platform react to the platform moving, thus moving with it. But they are moving along the platform frozen axis, y and z. Thus, if a user click on a platform and push the mouse upward, the platform is still (axis frozen) but the little cubes above will jump!

    I use the script founded in the community :
    Code (csharp):
    1. var spring = 50.0;
    2. var damper = 5.0;
    3. var drag = 10.0;
    4. var angularDrag = 5.0;
    5. var distance = 0.2;
    6. var attachToCenterOfMass = false;
    7. var hit : RaycastHit ;
    8.  
    9. var platform : GameObject ;
    10. var go : GameObject ;
    11.  
    12. private var springJoint : SpringJoint;
    13.  
    14. function FixedUpdate ()
    15. {
    16.  // Make sure the user pressed the mouse down
    17.  if (!Input.GetMouseButtonDown (0))
    18.  return;
    19.  
    20.  var mainCamera = FindCamera();
    21.  
    22.  // We need to actually hit an object
    23.  if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, 100, 1<<LayerMask.NameToLayer("TouchableObject")))
    24.  return;
    25.  // We need to hit a rigidbody that is not kinematic
    26.  if (!hit.rigidbody || hit.rigidbody.isKinematic)
    27.  return;
    28.  
    29.  if (!springJoint)
    30.  {
    31.  go = new GameObject("Rigidbody dragger");
    32.  var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
    33.  springJoint = go.AddComponent ("SpringJoint");
    34.  body.isKinematic = true;
    35.  }
    36.  springJoint.transform.position = hit.point;
    37.  
    38.  if (attachToCenterOfMass)
    39.  {
    40.  var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
    41.  anchor = springJoint.transform.InverseTransformPoint(anchor);
    42.  springJoint.anchor = anchor;
    43.  }
    44.  else
    45.  {
    46.  springJoint.anchor = Vector3.zero;
    47.  }
    48.  
    49.  springJoint.spring = spring;
    50.  springJoint.damper = damper;
    51.  springJoint.maxDistance = distance;
    52.  springJoint.connectedBody = hit.rigidbody;
    53.  
    54.  StartCoroutine ("DragObject", hit.distance);
    55.  
    56.  }
    57.  
    58. function DragObject (distance : float)
    59. {
    60.  springJoint.axis = Vector3.zero ;
    61.  var oldDrag = springJoint.connectedBody.drag;
    62.  //var oldAngularDrag = springJoint.connectedBody.angularDrag;
    63.  springJoint.connectedBody.drag = drag;
    64.  //springJoint.connectedBody.angularDrag = angularDrag;
    65.  
    66.  var mainCamera = FindCamera();
    67.  while (Input.GetMouseButton (0))
    68.  {
    69.  var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
    70.  springJoint.transform.position = ray.GetPoint(distance); ;
    71.  yield;
    72.  }
    73.  if (springJoint.connectedBody)
    74.  {
    75.  springJoint.connectedBody.drag =oldDrag ;
    76.  //springJoint.connectedBody.angularDrag = oldAngularDrag;
    77.  springJoint.connectedBody = null;
    78.  }
    79.  hit.rigidbody.velocity = new Vector3(0, hit.rigidbody.velocity.y, 0);
    80.  hit.rigidbody.angularVelocity = Vector3.zero ;
    81. }
    82.  
    83. function FindCamera ()
    84. {
    85.  if (camera)
    86.  return camera;
    87.  else
    88.  return Camera.main;
    89.     }


    What this script does is when you click on the platform (which is a touchable object), it created a gameObject in the scene (with no parent) and when you drag, you move around that new gameObject. This gameObject has a SpringJoint as a component, and the SpringJoint is linked to the platform. So when you're dragging the created gameObject, the SpringJoint between it and the platform works its magic to move the platform.

    Moreover, we can add constraints to the rigidbody of the platform, so if we freeze the y and z axis, the platform only moves on the x axis.

    Woohoo ! We got our draggable platform ! :)


    We tried to trick a little bit the script by changing the transform.position of the created gameObject to stays all the time the same as the y and the z of the platform. Doing so, the SpringJoint works only on the difference between the platform and the created gameObject : the x-axis. But the 3 littles cubes are still jumping on the y axis and sliding on the z one.

    Can you please help us make a platform on which objects will only reacts to the x axis ? We would really appreciate it ! :)
     
  2. BazLevelUp

    BazLevelUp

    Joined:
    Jun 27, 2012
    Posts:
    36
  3. Poupi

    Poupi

    Joined:
    Jan 11, 2012
    Posts:
    111
    Hi there,

    We're working on the same project, and after doing some more simple tests, I can't figure out how the physic engine is working.
    My platform has the simpliest script attached to it, with only one line :

    Code (csharp):
    1. rigidbody.AddForce(Vector3.forward*100, ForceMode.Acceleration);
    The rigidbody component has the six constraints ticked.

    On the cube, there's a script that writes the velocity at each FixedUpdate, and there's the result :
    Code (csharp):
    1. Velocity 0.03395238   0.01283235  1.851493
    $force.png

    We don't know if there's something wrong with Nvidia PhysX, or if there's a bug in the Unity implementation.

    Some help of someone working at UT would be greatly appreciated :cool: