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

Question Collision call back a bit early

Discussion in 'Physics' started by Erenquin, Mar 22, 2023.

  1. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Hello,
    after that discussion about collision - https://forum.unity.com/threads/con...e-correct-contact-point.1397278/#post-8818864 - I changed my setup.

    I can't use dynamic body on the racket because when the ball and the racket collide they push each other which is something I do not want.

    So the racket is kinematic and the ball is dynamic.

    I get actual collision callback (OnCollisionEnter2D) on the ball at least and overall everything works fine.
    The ball collides with the bricks and the walls that are static, and the racket.

    But, this time around it collides early :( on the racket.
    upload_2023-3-22_16-10-6.png

    For reference the horizontal size of the racket is "1". I checked the colliders which are correct.
    From this matrix I understand we get collision (-> in the physics sense) between Dynamic and Kinematic rigidbodies:
    https://docs.unity3d.com/Manual/CollidersOverview.html

    So there is certainly something else I do not get, but what ...

    This is the racket setup:
    upload_2023-3-22_16-14-1.png

    And the ball setup
    upload_2023-3-22_16-14-33.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    They certainly don't happen early. You don't show the respective colliders and you don't mention if the ball has bounced. It doesn't stop where it hits, give you a callback then move in the bounce for the rest of the simulation step. OnCollisionEnter2D is telling you that it contacted during the simulation step only.

    In your image, the ball doesn't have any velocity which would be strange for a moving Rigidbody2D. Maybe you're setting the Transform or using MovePosition (kinematic motion) on it.

    You can go into the physics settings and turn on gizmos to show you the contact points. Also, you can go into the info foldout on the Rigidbody2D/Collider2D and look at the actual contact points.
     
  3. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Thanks for the reply.
    I can't provide a picture right now, but I checked the colliders. I will take picture later.

    The ball has no velocity because I set it to 0 in the OnCollisionEnter2D callback to be able to take the picture at that precise moment.

    The ball bounces normally, as it should. No problem on that.

    I opened the info panel to check the contact points, but it does not show any, for both the racket and the ball. Maybe the reason for this is also the cause of my problem.
    But I did this check when the ball was stopped (so at the "OnCollisionEnter2D" callback). I will try to check in normal play at once.
     
  4. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Here are pictures with the colliders.
    The 2 top pictures are in collider edit mode.
    The bottom picture is with gizmo and the racket and ball selected.
    The colliders are less visible, but there nonetheless.
    upload_2023-3-22_20-38-33.png

    As for contact points, I played step by step and definetely get some on the ball:
    upload_2023-3-22_20-43-41.png

    upload_2023-3-22_20-44-20.png

    Here is the transform of the ball at the same time:
    upload_2023-3-22_20-47-29.png
     
    Last edited: Mar 22, 2023
  5. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    And this is for the racket:
    upload_2023-3-22_20-57-12.png

    upload_2023-3-22_20-54-38.png
     
  6. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Wit the "show collider contact" physics setting I get this pink arrow:
    upload_2023-3-22_21-1-18.png
     
  7. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    One thing I can think of is that I scale the objects rather than the images/sprites. As you can see the ball is 0.2 x 0.2 and the racket 1 x 0.2. Maybe I should keep the objects at 1x1 and scale the images ? Keeping the rest - colliders, etc ... - as they are ?
    The setup is like so:
    upload_2023-3-22_21-5-21.png
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    In your image above, the ball is moving upwards +6 m/s so as I said previously, is this not simply the bounce?

    Set the bounce to zero to check that.

    Failing that, if you have a small project you could share, I'd be happy to take a look at it for you. Either attach it here or DM me and I can create a place where you can upload it privately.
     
  9. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    If remove the bounciness it is indeed much better (or at least closer to what I expected).

    Thanks for the checks and the info.
    upload_2023-3-23_13-19-28.png
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    When you say better, you mean correct. There's a default-contact-offset for any collision involving polygons for stability.

    Look at the contact point before and after you changed the bounce. They are both Y=-4.6475. With bounce, the body moves up to -4.45 and without bounce it stops at the contact and is at -4.54.

    I guess I just don't understand why you'd expect something that has bounce to not bounce? I can understand misinterpreting its final position as it not yet hit but if you get the collision callback and can see its velocity moving upwards, it's bounced. :)
     
  11. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Yes indeed I wanted to mean "visually better", not technically.
    With the bounce (which was at 1 by the way) it looked (player point of view) like the ball was not touching the racket, which is why I started this investigation.
    Without the boucne it looks better, like closer to the racket.