Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Collisions become unpredictable when changing localScale of a sphereCollider's GObject per-frame.

Discussion in 'Physics' started by perceptionforaction, Nov 12, 2020.

  1. perceptionforaction

    perceptionforaction

    Joined:
    May 22, 2016
    Posts:
    2
    I am designing a little VR catching game in which the thrown ball changes physical size during its approach to the user. Because this is VR, the user is catching using a disc/flat cylinder that is a child of the motion-tracked VR wand.

    Currently, rescaling of the ball is implemented by manipulating the game object's localScale within the Update() loop. The resize is a gain term which adds/subtracts a fraction of the ball's physical size on each frame. The change in size is uniform along the 3 spatial dimensions, and is not allowed to drop below 0.05 m, and does not grow beyond 1.5m.

    The ball is supposed to stick to the user's hand-held paddle upon collision, and this is implemented as an OnCollisionEnter() reparenting of the ball gameobject to the paddle. When the resize manipulation is off, everything works beautifully. When the resizing manipulation is on, the collisions are unpredictable, and the ball often bounces away from the paddle before coming into visual contact with it.

    The temporal resolution of the physics engine has been greatly increased to improve reduce issues related tunneling. I have tried all forms of collision detection mode, and this does not seem to help things.

    Any input would be greatly appreciated!

    Thanks.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Physics doesn't run per-frame, it runs per fixed-update (by default). Also, if you size-scale-up colliders then you're likely causing overlaps which then the solver has to resolve, some of which won't necessarily happen instantly during a single simulation step but might take several steps, none of which are per-frame.
     
    perceptionforaction likes this.
  3. perceptionforaction

    perceptionforaction

    Joined:
    May 22, 2016
    Posts:
    2
    Thank you for your diagnosis, Melv. I'm afraid I understand it only superficially, and not enough to speculate on possible remedies. Do you have an accompanying remedy to suggest? I have a few questions below that, if you have the time to answer, might help lead this horse to water.

    "Physics doesn't run per-frame, it runs per fixed-update (by default)."

    I understand that, but am not sure how it would impact the current problem. In any case, the issue persists whether the resizing method is called in Update() or in fixedUpdate().​

    "if you size-scale-up colliders then you're likely causing overlaps which then the solver has to resolve, some of which won't necessarily happen instantly during a single simulation step but might take several steps, none of which are per-frame."

    There are only two colliders in the scene: one on the ball gameobject, and one on the paddle gameobject. I'm a bit unclear on what an overlap is. I assume that the only overlap would be when the ball tunnels into the paddle, so correct me if I'm wrong.

    Apologies, the language "per-frame" was clearly just not specific enough. I'll use more specific language (ie update, fixedUpdate) in the future. Are you suggesting that it will take several rounds of fixedUpdate for the solver to deal with overlaps?

    The resizing of the ball should stop when OnCollisionEnter() is called, and the ball should then stick to the paddle, but does not. Instead, it bounces off, and often before the collision occurs, visually, as if the ball is bouncing off of a force-field. Could this issue you're raising also be the cause of the failure to stick to the paddle?


    Thanks again!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    I mention this because you're performing actions during the update and potentially other actions in other scripts. Those might occur several times before physics updates so of course this can affect it. I thought this important to point out.

    One collider overlapping another.

    It can be tunnelling but if you scale-up a collider then it's suddenly and instantly bigger which itself can cause overlap with other collider(s).

    Yes.

    It's pretty much impossible for me to tell you what's going on but you mention "visually" so I'll come back to the fact that visuals are updated per-frame whereas physics isn't (by default). Also, the gizmos might show you the resized collider based upon the properties but until a simulation step occurs it won't be processed. This is ultimately the issue with modifying physics per-frame and expecting results from it per-frame i.e. looking at gizmos and saying it should be touching now but no simulation step has occurred. At 200fps you'll get 1 physics step every 4 frame renders and update callbacks if the fixed-update is at 50hz.

    This smacks of part of the issue. Sounds like you've set the fixed-update to be a small time-step. This can also mean that many back-to-back fixed-updates will happen between frame-renders depending on what you've set the time to. I've seen devs set this to silly values like 1khz but it just means physics runs many times before each frame-render using small time-steps.

    I will mention that the OnCollisionEnter means a contact between two colliders began, it doesn't mean it's currently contact. There might be a follow-up OnCollisionExit because the things came into contact, bounced then exited contact. After the simulation you get Enter/Exit. I have no idea if this is the case, all I can do is throw this stuff at you. You describe it not being visually in contact so if that discrepancy is around the distance you're resizing then it's due to that. Try NOT resizing and see if that fixes the issue.

    Finally, if none of what I said above is appropriate then please remember that I've not seen this, only some basic description of the problem. The more info you provide the better we can zero in on the issue.