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

Bounce angles are sometimes ignored.

Discussion in 'Physics' started by jtsmith1287, Jun 3, 2015.

Thread Status:
Not open for further replies.
  1. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    So we've got a game where the object is to bounce a ball as many times as possible off as many obstacles as possible. We're just using a sphere collider and rigid bodies and letting the physics engine do it's thing -- and we love the results. The issue though is that sometimes the ball will hit a level (as in, parallel to the ground) object at a slight angle, and instead of bouncing with the opposite angle and maintaining forward velocity, the ball loses 100% forward velocity and will just bounce up and down until it it loses energy.

    We've toyed with the physics material settings several times, with dynamic friction and whatnot and we haven't been able to eliminate the issue.

    Does anyone know what might cause this and how to get around it, without writing our own physics? It's a small game and the built in physics is plenty for what we need.
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    What is your physics timestep at? If its at the default .02, does putting it at .01 fix the issue?
    Do you have CollisionDetectionMode.ContinuousDynamic set on your ball? If so, I think I read somewhere that ContinuousDynamic might alter your physics a bit making it a little unrealistic. Not sure though. So maybe that slight angle was small enough for the physics to ignore it when fixing collisions.
    If you dont have CollisionDetectionMode.ContinuousDynamic, then maybe try putting it on. The ball might be
     
    werdehausenmax likes this.
  3. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I'm not actually sure where to find the time step. It's not Edit>Project Settings>Physics.
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Its in project settings Time fixedTimestep
     
  5. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I set it to continuous dynamic and it didn't fix it. I tried setting the timestep to .01 and it just broke everything. Some stuff was way faster, other stuff was way slower... the ball suddenly had no force when moving. I was going to investigate, but I don't think this is a time issue. It still bounces with the correct velocity and angles are ONLY a problem when the ball is traveling almost straight down. Like the phsyics engine doesn't have the accuracy or something to calculate the angle? Any other thoughts would be awesome though.
     
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    The timestep increases how many times the physics engine runs, so setting it to .01 would make things faster if you did not do things such as applying Time.fixedDeltaTime in your calculations.
    You could also try increasing the physics solver iteration count as well as lowering the default contact offset.

    I guess it is possible the angle is so small that the physics doesn't catch it. Or maybe the angle is so small you just think its bouncing straight up. If you debug.log its velocity (the floats of the velocity so you can see all of the decimal points), does it show no change in any direction but upward?
    You can use this to help debug the velocity to show more decimal points.
    (or you can just check the transform in the inspector)
    Code (CSharp):
    1.     public static string Vector3ToFloat(this Vector3 vector)
    2.     {
    3.         return Vector3sToFloats(vector);
    4.     }
    5.  
    6.     public static string Vector3sToFloats(params Vector3[] vectors)
    7.     {
    8.         string vector3ToFloats = "";
    9.         for(int i=0;i<vectors.Length;i++)
    10.         {
    11.             vector3ToFloats += vectors[i].x.ToString() + " " + vectors[i].y.ToString() + " " + vectors[i].z.ToString() + " @-@ ";
    12.         }
    13.         return vector3ToFloats;
    14.     }
    you can use it like this
    Code (CSharp):
    1. Debug.Log(someRandomVector3.Vector3ToFloat());

    Mind posting a video of the problem?

    When I do tests of a ball falling straight down, but has a very tiny bit of force added to its side, it seems to properly move to that side when bouncing (although it does loss its speed after each bounce, which is how its suppose to be).
     
    Last edited: Jun 3, 2015
  7. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I don't want to do a video, as it will just give away the gameplay completely and I'm not ready to reveal. BUT here's a picture. :D The red line demonstrated the problem. The lines are the paths of the bounce. If the angle is shallow (that the right word?) the ball does just fine, but if it's closer to 90 degrees it seems to just zero out. So I'm inclined to think this is some kind of floating point accuracy thing where the physics engine simply doesn't calculate more than one decimal place. I printed the objects transform.position to the console on several occasions and it was only displaying a single decimal place. I tried messing with the timesteps and the physics iterations and all that and nothing helped. So it's not that, it's not friction... I really think there's an accuracy issue. Maybe someone from Unity will chime in here and shed some light? But this is a major problem, seeing as our game is 100% about bouncing... so when this happens the player just loses the level entirely and has to restart (or wait for it to come to a complete stop and reset automatically).

    BounceIssue.png
     
  8. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    debug only shows one decimal place when printing vector3. you need to debug like this for more decimal spaces to be shown.
    Debug.Log(transform.position.x + " " + transform.position.y + " " + transform.position.z);
    this should give about 7 more decimals of accuracy when displaying in debug. (not sure if you already did this, so let us know).

    you can also possibly try removing any friction on the physics material that is on the ball or ground to allow the ball to not loose its sideways velocity to fast. (I know you said it's not friction, but just making sure you checked both the floor and ball, and for debug purposes it might make it more clear as to whether the ball still has sideways movement if you remove any friction.)
     
    Last edited: Jun 4, 2015
  9. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    We started off with no friction and added friction to see if that was the issue. Something I didn't think of before but I'll check now is the angular drag on the ball. I'll set everything to no friction though and see if that solves the issue, though spinning of the ball *is* desired at the end of the day.
     
  10. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Hey guys, did you ever find a solution to this? I seem to be experiencing the same problem.
     
  11. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Unfortunately not yet. Although I'm glad you said something because I forgot I thought about the angular drag and haven't tried it yet, haha. I don't think it's going to work but I'm going to try when I get home anyway. Again I'll say that i think this is a physics accuracy thing and I'd love someone from Unity putting in their 2 cents to shed some light.
     
  12. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Yeah Ive done quite a bit of work with Box2D and other phsysics engines so Im happy I know enough about some basic settings.. and can spot when something strange is occurring.

    Ive broke this down to a basic ball and ground.. I give the ball a velocity and with it selected in the scene view while playing the test I notice that when it bounces it actually receives a rotational force in the opposite direction to what you would expect... so in the game view we see the ball stopping in its forward motion. A bit like when you roll a ball forward on a surface with a reverse rotation/spin on it.. it stops more sudden and almost rolls back at you. The ball in my test has no rotation while in the air.. I'd expect it to get some forward rotation when it lands and continue in that direction with some bounce as per bounce material set.
     
  13. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    That's exactly what we saw as well. That's why we have been wondering about friction and angular drag, as the issue seems to ... revolve... (a joke!) around the rotation of the ball. With no friction at all the issue was exacerbated as the ball was unable to gain any spin at all and just slid around as if on ice.
     
  14. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    It might help if you guys posted a video of the issue.
     
  15. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    I'll try upload an example in a bit.. Cheers
     
  16. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Hey guys.. sorry being playing with a test before uploading anything.
    @jtsmith1287.. out of interest... couple of Q's... what transform-scale is applied to your ball? Are you using the default 1Unit-100Pixels? Did you use a 3D Sphere with a RigidBody2D & CircleCollider2D?
     
    Last edited: Jul 8, 2015
  17. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    5.1.1f test attached.... Observe sphere in scene view when playing... for each of the 3 scenes.... Notice lack of reflection in bounce and reverse rotation when sphere is scaled to 0.1.

    Seems scale is causing the issue? Not the value of transform scale itself, but the size of the ball in the scene. I added a 10x10px sprite as the ball... so it was the same size in the scene as the sphere scaled to 0.1..... but its scale was at 1. It behaved the same until I scaled it to 2... Seems to reduce the problem.. the bigger the better basically.

    Point being, if I want to simulate a golf ball it doesn't bounce/roll correctly at 10x10 size.
     

    Attached Files:

    Last edited: Jul 8, 2015
  18. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Ok guys, did a bit more investigating... it seems the physics fixed update interval is not able to handle the smaller sized ball very well.. meaning it intersects with the ground on collision.... and thus creates our problem. The Fixed Timestep is set to 0.02 by default.. which is a target 50fps Fixed Update. This works most of the time.

    Edit-ProjectSettings-Time>Fixed TimeStep

    If I reduce this to 0.01 and then to 0.005.. the results improve and the ball bounces more accurately at a small scale. The problem is performance. I wouldn't be keen on going lower than 0.01 (100fixed updates per sec).. on mobile devices.

    So where does the scale come in, well, obviously the bigger the ball means a smaller intersection and less effect in the resulting simulation I think!? When the ball is so small that it almost embeds fully on collision, the result are a spin in the opposite direction and some weird behaviour.

    Here are the screen shots of the collision at different timeSteps

    TimeStep = 0.02 = full intersection on bounce at steep angles

    TimeStep02.JPG

    TimeStep = 0.01 = slight intersection on bounce at steep angles, better bounce result

    TimeStep01.JPG

    TimeStep = 0.005 = not much intersection on bounce at steep angles (but too low, just testing)

    TimeStep005.JPG
     
    Last edited: Jul 8, 2015
  19. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Or, as stated in my first post, just set your balls rigidbody collision detection to continuous dynamic (since you have rigidbody2d, just set it to continuous)
    Edit - I just noticed my first post got cut off, but it still stated to either disable, or if it is already disabled, then to try enabling continuous dynamic.

    In your example project, on the small ball scene, just setting the rigidbody2d collision detection to continuous seems to have fixed the issue.
    Although, I had to downgrade your project from 5.1 to 5.0, so maybe you already tried that and it didnt work for some reason in 5.1?

    jsmith said he tried changing the collision detection, but it still didnt work, and yet it works for me in your (reechie.greene) example scene. It would be great if I can play around in jsmiths scene as well.
     
    Last edited: Jul 8, 2015
  20. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    @HiddenMonk Dude that fixes it in my 5.1.1 test scene. I'll give it a go in my actual game project and let yas know how it goes. Any thoughts on using that option vs using lower TimeStep? I'll look into it some more.. .I presume it has a performance hit too?
     
  21. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    yes, I am pretty sure it will have a performance hit (might not be such a big one though), but I think increasing the timestep from .02 to .01 or above would be a much bigger performance hit, especially if you have many physics objects.

    When in doubt, profile it :)
     
  22. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    ;)
     
  23. adbutorin

    adbutorin

    Joined:
    Mar 12, 2018
    Posts:
    1
    freeze rotation?
     
  24. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    4 year necro. Most impressive.
     
  25. Dimas42

    Dimas42

    Joined:
    Jul 15, 2019
    Posts:
    2
    Man, I faced the same issue(though in a 2D project), and I found a solution if it is still relevant. Go to edit - project settings - physics2D and lower the velocity threshold value. For a 3D project, lower the bounce threshold value.
     
    Dreanian and andersma like this.
  26. French_04

    French_04

    Joined:
    Sep 24, 2018
    Posts:
    1
  27. andersma

    andersma

    Joined:
    Jan 15, 2018
    Posts:
    2
    Thanks a lot, solved my problem! Had a problem in my 2D project where my ball, in a very low angle, wouldn't bounce, it would instead start sliding along the wall. So thanks for resurrecting this thread.
     
  28. suryamdg19

    suryamdg19

    Joined:
    Mar 29, 2022
    Posts:
    1
    I was stuck with the same problem in Unity 2D and then realized I have not set the friction to 0 in physics material 2D.
     
  29. HighTideDevelopment

    HighTideDevelopment

    Joined:
    Aug 20, 2023
    Posts:
    1
    I have fixed the issue though I'm not sure if this is still active. There is an option under the physics 2d section of the project settings with an option called "Velocity Threshold". If you set that to zero or 0.00001, your ball will bounce no matter what the velocity is. This was caused by the physics engine rounding down any velocity value lower than one to zero and stopping the bounce.
     
Thread Status:
Not open for further replies.