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

collision detection at high speed

Discussion in 'Editor & General Support' started by thijske, Nov 28, 2006.

  1. thijske

    thijske

    Joined:
    Jan 13, 2006
    Posts:
    15
    Hello,

    I'm working on a game that relies fairly heavily on Unity's physics capabilities and I've encountered a slight problem. I've noticed that sometimes Unity doesn't register a collision between my car objects (with wheel and box colliders) and the environment (mesh collider) when my car objects move at very high speeds. Sometimes, cars simple drive through my mesh colliders, instead of colliding with them; not good. This seems to happen only at very high speeds and appears to be getting worse when the car objects collide into sharp angles (where the angle between the 'floor' and 'wall' is less than 90 degrees). It's as though Unity first moves the car object according to its speed to its new position in the Update and THEN checks for collision at this new position, rather than check along its travelled path for obstructions.
    Has anybody ran into this problem before? Is there a way to tell Unity to be more precise in collision detection (I have time scale = 1 and fixed timestep = 0.01)? I would very much like to garantee that my cars stay within the scene and are not able to drive out of it and fall into oblivion...

    Best wishes,
    Thijs

    ps I'm on a dial-up right now, so I cannot attach a demo scene. Sorry :(
     
    guetta18 and wongi5469_unity like this.
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    If a collider object moves through another collider completely in one FixedUpdate, I don't think any collision is detected. This is probably a big deal when an object is moving faster than its length every fixed frame. You can increase the frequency of FixedUpdates by decreasing the fixed timestep in the Time settings of your project at the cost of performance. You can try keeping your FixedUpdate functions really lean and decreasing the time step to maybe 0.005.

    Hope that helps,
    -Jon
     
    Abdullah5893 and Dysson like this.
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Unity does collision detection at discrete timesteps. So if you have an object that during one physics frame moves more than it's entire size, it can tunnel colliders.

    There are three solutions to it:
    * Make the collider of the rigidbody a bit larger
    * Make the physics time step smaller. What you are optimizing for us making sure that in one physics step you never move more than the entire size of the collider.
    * Make sure the rigidbody doesn't exceed a certain speed at which you know, it can't tunnel. You can use drag for this purpose.
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Another solution (and that's what is usually done in "real games") is to craft your collision geometry carefully. No less than 90 degree angles for the car to run into; no super thin double sided fences, etc.

    In our car demo we actually have different visible and collision meshes exactly for this purpose. Some of the tips are in wheel collider docs as well.
     
    Abdullah5893 likes this.
  5. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Is there a calculation that we can do to get a close approximation of how far something will move in a fixed timestep? For example. if the physics timestep is set to .01 and the max speed of an object is 200,. can we approximate the distance it will move? Will multiplying the speed by the timestep give us something close, in this case 2 units? In all fairness, I have not checked this out yet, even though I have been meaning to do a test project for physics stuff :oops:
     
  6. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    It's an easy calculation, thankfully. If your object's rigidbody.velocity.x is 1, then it moves 1 * Time.fixedDeltaTime (default is 0.02) in the x direction each FixedUpdate. And it's the same for the other directions. Also, you can visually see how fast things move each fixed frame by first pausing your game and then using the Step button (it's to the right of the Pause button) to advance the game one fixed update period.

    Cheers,
    -Jon
     
  7. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Thanks Jon :)
     
  8. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    You're very welcome!
     
  9. thijske

    thijske

    Joined:
    Jan 13, 2006
    Posts:
    15
    I checked and my car was indeed moving further than the length of its own collider within one fixedupdate. I'll play around a bit to find the best solution. Thanx for all of your help!