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

rigidbody position not accurate OnCollisionEnter2D

Discussion in '2D' started by samifruit514, Jul 23, 2016.

  1. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    Hello,

    I have a ball with a rigidbody2d attached that collides with a box collider2d (kinematic). In OnCollisionEnter2D, I expect the distance between the ball and the contactPoint to be the same (or almost) as the half of the ball height (or width) which is 0.1.

    The problem: I get between 0.1 and 0.2.

    Why is this happening? Its like if oncollisionenter2d is not called immediately after a fixedUpdate...

    Unity 5.4.0b24

    Thanks
     
    Last edited: Jul 23, 2016
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I think, ball is moving every frame. FixedUpdate is calling at fixed time steps (0.02 as default ?).And distance can be different. You can try to lower fixed time step (edit-project settings-time). I dont think it is good solution), or make something other (maybe raycast every frame).
     
  3. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    According to the lifecycle diagram (https://docs.unity3d.com/Manual/ExecutionOrder.html) the OnCollisionXXX methods should be called at the end of each physics loop. Would it be possible that the collisions are calculated in a previous loop which is why we are getting a different rigidbody position?
     
  4. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    Forgot to say, the goal is to find out where did the contact happened on the sphere collider 2d.
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
  6. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    I was able to get the contact point on the collider with a spherecast from the rigidbody.position with relativeVelocity.normalized as direction:
    1. void OnCollisionEnter2D(Collision2D coll) {
    2. Collider2D collider = coll.collider;
    3. Vector2 contactPoint = coll.contacts[0].point;
    4. RaycastHit2D rc = Physics2D.CircleCast(rb.position,ballSize, coll.relativeVelocity.normalized,5);
    5. if(rc.collider != null && rc.collider == collider) {
    6. // rc.centroid is the exact position where it collided
    7. Debug.Log("the right position: "+rc.centroid);
    8. }
    9. }

    Thanks for the raycast clue vakabaka.

    imo the exact position of the collider should be passed somewhere (in collision2d?). After all, if unity can find the contact point, it means it has been computed from the right collider2d position
     
    Last edited: Jul 27, 2016
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    coll.contacts is the list of exact positions the colliders contacted. Are you trying to get the position the objects were at when they collided, or like the center points of the colliders? What is the end goal you want with this information? Collision2D should have all the information you need about the collisions.
     
  8. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    Hi,

    the goal is to check if the contact point was above the half of the collider or below. collision2d has contact point info but does not have the exact current rigidbody/collider position. The raycast hack above works.
     
  9. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You should be able to use Collision.gameObject.transform.position and Collision.rigidbody.position to get the positions of the other collider at the time of the event. The object with the event implementation can get that info through the normal means, GetComponent<Rigidbody2D>().position and transform.position. They are the current positions, as the game doesn't do a regular Update until after FixedUpdate.
     
  10. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    Thanks for your quick replies,

    The problem is that the rigidbody/transform.position are not where the contact point occured. The contactpoints are about 0 to 0.1 away from the edges rigidbody/transform.position. It can be more depending on the velocity of the rigidbody
     
    Last edited: Jul 31, 2016
  11. dmoney

    dmoney

    Joined:
    Oct 6, 2014
    Posts:
    15
    just set your collision detection on your two objects to continuous, you will get a more accurate contact point.
     
  12. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    only one of the two objects has a rigidbody. afaik, I can set collision detection on rigidbodies only ?