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

Sphere Collision Issues - Unity 5

Discussion in 'Physics' started by raiden, May 23, 2015.

  1. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Hi all, I am having some issues with collision detection in Unity 5 that were not present in Unity 4. The main issue is that randomly a sphere is not colliding with the bowl, and passing right through it.

    Here are the details of my setup:
    • 75 - 1 meter spheres inside a hollow bowl
    • The hollow "bowl" container has a Mesh Collider attached to it (Convex is not used, and cannot be used)
    • Each sphere has a rigidbody and mass of 1, and also a scale of 1,1,1
    • OnCollisionStay is being used to apply velocity to each sphere in the opposite "normal" direction of the contact point
    • The velocity is being applied directly in OnCollisionStay as: collision.rigidbody.velocity = (dir * speed)
    • "speed" is a floating point variable of 20.0f
    • In Physics manager, Gravity is set at -20.0f, and Solver Iteration Count is at: 7
    • In Time Manager, Fixed Timestep is at 0.0125,
    Not sure what else to say, except maybe seeing what is happening in this short video:


    I'm hoping someone can help me resolve this, I really need to finish this app up and get it up on the stores, but I can't in this state.

    Thanks

    -Raiden
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
  3. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Thanks for the reply, I've changed the video to public (DOH!), also, I should have listed in my bullets, that I have the spheres set as Continuous Dynamic, but nothing on the bowl, so I'll add that and see how it goes.

    Check out the video now please, you'll see exactly whats happening, and if you have anything to add, I'd greatly appreciate it.

    Thanks

    -Raiden
     
  4. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    [POSSIBLY] [SOLVED] :)

    Still needs more testing, but basically what I did was ensure the spheres were very light (mass: 0.001), and I also made sure the spheres were set to Continuous Dynamic on the Rigidbody, as RiokuTheSlayer suggested.

    The final fix was simply adding extra layers to the mesh collider of the bowl, so I duplicated the bottom half of the bowl 3 x's, scaling each one slightly larger (0.01f) on the x, y, z scale transform, and so far, all collisions are happening, and after about 94 passes, no issues.

    This info might help some who is doing something similar in the future.

    -Raiden
     
  5. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    [FORSURE] [NOTSOLVED] :(

    Well, just as I thought everything was stable, and working, it's back to "not". This is the part that sucks about development, there's always a middle ground for stability and bugs.

    Could anyone please shed some light on this issue? I would really like to resolve this asap.

    Thanks

    -Raiden
     
  6. bennyboy

    bennyboy

    Joined:
    May 5, 2015
    Posts:
    13
    I've had this kind of thing happen in self-coded games in the past (not with Unity). My first guess would have been framerate "tunneling," where a low framerate lerps an object right through a collider, but CollisionDetectionMode.Continuous is apparently designed to deal with that.

    My second guess would be the order in which the colliders are firing. Specifically, I think each ball is checking for collisions with the sphere first, and then the other balls. So the ball tries to bounce away from the sphere, hits another ball which bounces it back toward the sphere, and now the sphere collision is already finished and doesn't fire twice in one update.

    So you get a perfect storm-- a couple balls moving in just the right way "launch" another ball at a high rate, and CollisionDetectionMode.Continuous fails because the fast-moving ball has already been checked against the sphere.

    I don't know if it's possible to set the order of collisions. If I were you, and if I were really just working with spheres, I'd be tempted just to hand-check all the ball distances myself, and to rewind escaped balls back to the point of escape.
     
  7. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Hi bennyboy, hey thanks for the response.

    When you say "hand-check all the ball distances myself" basically the idea is I need to use Vector3.Distance at a center point in the container, and if a sphere falls beyond the distance of center to a small distance outside the container, I rewind it back to it's previous position?

    Just want to make sure I understand how to script this up, and that I am going about it the way your thinking.
     
  8. bennyboy

    bennyboy

    Joined:
    May 5, 2015
    Posts:
    13
    Before I answer, let me just say there could be a better answer. I think the simplest solution would be to force Unity to check the collision of all the balls with each other first, and that of the containing sphere last. Or, you could just force a manual check at the end of the update logic, though this is a little inefficient. What follows is only for if you want to have the full control of doing the calculations yourself.

    Basically, yes, you have the right idea, but I'd say two things. First, the Vector3.Distance is from the center of each ball and sphere, so to check the distance of the ball's surface, you'll have to add the radius.

    Second, if the ball is moving fast, the ball might have been pretty far from the sphere in its last position. So if you move it to its previous position and apply a bounce, it could bounce in the middle of the air. You need to calculate a virtual frame: the exact position of the ball when it WOULD have touched the sphere.

    Technically, you should rewind to a virtual frame, calculate your new velocity, and then fast forward back to the current time. But with so many objects in play, that's going to be almost impossible to do in real time, I think.
     
    Last edited: Jun 2, 2015
  9. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    collision.rigidbody.velocity = (dir * speed) might be the problem. Maybe it is possible that setting the velocity directly is causing some precalculations the physics have done, such as adding force to prevent passing through a collider, is being overwritten with your new velocity. Though since it is getting a direction which should theoretically always be facing inside the ball, it doesn't seem setting the velocity directly should be an issue, but its worth a shot. Try replacing rigidbody.velocity with rigidbody.addforce (Id also just do plain addforce and not something like addforce with VelocityMode.VelocityChange (at least for this test)), or maybe even do collision.rigidbody.velocity += (dir * speed) instead of just = (though this probably will make the strength weaker since it is now going against its previous velocity instead of just ignoring it).
     
  10. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    @HiddenMonk, I've changed directly setting velocity to AddForce using VelocityChange, still no improvement, balls are still falling through.

    My latest fix, being that I tripled up on the bottom collider of the bowl, going back to my previous post "[POSSIBLY] [SOLVED]" seems to have the most stable affect. I'm guessing the extra "thickness" of the mesh collider is correcting collisions breaking?

    I'll have to do some lengthy runs to ensure that its 100% solid prior to releasing. Thanks bennyboy & hiddenmonk for your help with this.

    -Raiden