Search Unity

BuoyancyEffector2D - should this be happening?

Discussion in 'Physics' started by Abhinav91, Jan 24, 2016.

  1. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    So I recently noticed some strange behavior in the BuoyancyEffector2D component. Not sure if it's strange behavior or it was meant to be that way so I thought I'd ask here.
    Let me mention some values first. The BuoyancyEffector2D's Density is 0.92, Flow Angle is 20 and Flow Magnitude is 10. The object falling into it has it's Rigidbody2D's Mass set to 1. Now based on these values, what happens is, the object falls into the 'water', sinks a little, and then because of the Flow Angle and Flow Magnitude, starts moving towards the right side, gradually floating to the top. However, when I change the Flow Magnitude to -10, and the object falls into the 'water', it starts going on the opposite direction (as expected) but because of the Flow Angle, it now starts getting pulled down.
    So I was just wondering, is this supposed to happen? Or is the object supposed to go in the opposite direction (left) and start floating to the top?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
  3. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    Thanks for the info @MelvMay

    About this, I tried experimenting by setting Rigibody2D Use Auto Mass to true and setting the Collider2D Density to 3. The BuoyancyEffector2D property values are as follows:
    Density = 4
    Linear Drag = 5
    Angular Drag = 5
    Flow Angle = 0
    Flow Magnitude = 0
    Flow Variation = 0
    The object sinks a little, and then floats to the top.

    Then I set Rigidbody2D Use Auto Mass to false and this time set the Rigidbody2D Mass to 3 instead (BuoyancyEffector2D Density still 4). The object still sinks a little, and then floats to the top.

    Then I set the BuoyancyEffector2D Density to 2 (Rigidbody2D Mass still 3 and Rigidbody2D Use Auto Mass still false). This time the object just sinks.

    Does the BuoyancyEffector2D Density compare against Rigidbody2D.Mass or Collider2D.Density?
     
    Last edited: Jan 24, 2016
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    It use density only. The object won't sink unless it enters the effector with some velocity i.e. falling via gravity. From a standstill, it would rise if the density is less than the effector.
     
  5. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    Alright so I'm a little confused now. I tried another experiment.
    Rigidbody2D Mass = 3
    Collider2D Density = 1
    BuoyancyEffector2D Density = 2

    I set the Rigidbody2D above the BuoyancyEffector2D Surface Level and played the game. The object entered the effector and sank.
    Then, I set the Rigidbody2D's position such that it is within the effector from the start and then played the game. It still sank.

    Even the documentation for BuoyancyEffector2D.density says
    "Buoyancy forces are calculated by comparing the density of the effector to the ::Collider2D::density of the Collider2D."

    If the Collder2D Density is 1 and BuoyancyEffector2D Density is 2, shouldn't the object float to the top? Why is it sinking?
     
    Last edited: Jan 25, 2016
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    No idea, that's not what happens when I test it. There was an issue a while back where Rigidbody2D.gravityScale wasn't taken into account but as I say, that was fixed a while ago.

    Try using the latest version or set gravity scale to 1 as a test.

    From above you mention that mass is 3. As I stated already; if you turn-off Rigidbody2D.useAutoMass and set the mass explicitly then the density on colliders is fixed at 1. The mass is irrelevant for the effector.

    Beyond that I have no idea why that would happen.

    If you are still seeing issues then I would suggest creating a bug case and posting the case number here so I can help you further.
     
  7. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    Alright I have created a bug case. The case number is 764556.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    So my previous statement wasn't entirely accurate. The actual relative density is what is used which is what I was trying to get across however the total force applied (just like any other force) is relative to the mass. In other words, a larger mass will react less per unit force than a smaller mass.

    With a fixed density of 1 then to get more buoyancy you need to either increase the area of the collider and/or reduce the mass to increase the forces effect.

    This is why useAutoMass should be used for all objects in the effector. This way, their size and density ensure they all react relative to each other. When you select this in your project, the mass is changed from 3 (explicit) to 1 (auto-calculated) i.e. the force has three times the effect which is why the upwards force can then counteract gravity.

    The buoyancy force is calculated as:

    Code (CSharp):
    1. var buoyancyForce = -EffectorDensity * SubmergedCollider2DArea * WorldGravity * Rigidbody2DGravityScale
     
  9. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    Ah I see. Thanks for clearing that up.

    Sorry, I didn't understand.

    Also, how do you calculate relative density?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    By relative density I mean the density difference between the effector density and the collider density. The bigger the difference, the bigger the force. Also the bigger the submerged area of the collider the bigger the force. Finally, the lower the mass of body, the more effect the force will have.

    In simple terms; use auto-mass. When you've got multiple objects floating, the mass will be calculated automatically meaning that they'll all act similarly according to the density you give them as the mass will be calculated as the density * collider-area. If you don't use it then the density is fixed at 1 and the only variable you have is the mass of the object (which changes how the force acts on it) and the area of the collider which is used to calculated the submerged area and the size of the force.

    In your case, you assumed that because the density was 1 that it would float in an effector set with a density of 2. This wasn't the case because the mass of the object reduced the effect of the buoyancy force. Reducing the mass to 1 (for instance) meant it floated. Alternately, you can increase the buoyancy force by increasing the area of the collider.
     
    Abhinav91 likes this.
  11. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Does this effector work with projects that don't make use of Auto-mass?

    It's a limited use case option. What if you have a huge plank of wood and really tiny lump of lead? Auto-mass is not applicable in this case, where the game designer would probably want the wood to float and the lead to sink, unless theres a work around for this?
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    I presume you mean not using auto-mass on Rigidbody2D components as it isn't limited to whole projects using it or not. Sure, as I've stated, with it off on the Rigidbody2D, the Collider2D have their density fixed at 1 (because you are explicitly setting the mass) so it's down to the area of the colliders and the mass of the body only.

    In your example you don't give a reason why you would want auto-mass off; you use it and give the lead a high density and the wood a low density (use real values if you like).
     
  13. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    @MelvMay just one question, where is the density setting? Is it hidden?

    Screen Shot 2016-01-29 at 5.40.11 PM.png
     

    Attached Files:

    Last edited: Jan 29, 2016
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    The density is specified on the colliders when the Rigidbody2D they are attached to is using auto-mass. With it off, it is not shown.
     
  15. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    Thanks for the help @MelvMay ! My doubt has now been cleared.

    I have a question relating to 3D Physics. Since it is not related to the topic that this thread is about, I'm not going to ask it here. I haven't made a thread about it because upon doing research, I found that a thread for the same topic already existed. Unfortunately, the question hasn't been answered yet. Instead of creating my own thread I decided to bump that thread. It's been a couple of days and there's still no reply.
    What do you suggest?