Search Unity

AddForce in opposite direction than gravity

Discussion in 'Physics' started by Magnumstar, Jan 3, 2016.

  1. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I understand force = mass * acceleration and my character's rigidbody2d has a mass set to 88.9 (this is what player would weigh in kilos) and my jump force calculation is equal to Vector2(0.0, 100.0) applied directly to the Rigidbody2D using .AddForce(), why does this not make the character leave the floor? My gravity scale is set to 1 for the rigidbody2d.

    I'm not trying to find an escape velocity here, just exceed gravity's force with opposite force. Can somebody explain to me the equations or theory behind this operation and help me wrangle this issue?

    Edit: Also, I read the size of your human character should be around 2 meters tall. When this is said, does that mean the transforms scale should make the sprite double the size of a unity cube object, or is there some other meaning to this?
     
    Last edited: Jan 4, 2016
  2. RichardP1985

    RichardP1985

    Joined:
    Mar 27, 2014
    Posts:
    5
    Be careful setting a rigidbody's mass to more than 10. From the docs:

    It is recommended to keep mass values between 0.1 and 10. Different Rigidbodies with large differences in mass can make the physics simulation unstable.
    Higher mass objects push lower mass objects more when colliding. Think of a big truck, hitting a small car.
    A common mistake is to assume that heavy objects fall faster than light ones. This is not true as the speed is dependent on gravity and drag.
     
  3. Fab4

    Fab4

    Joined:
    Sep 30, 2012
    Posts:
    114
    I am not at home so the actual commands might differ a little (I'll check as soon as possible)
    Code (CSharp):
    1. Vector3 counterGravity;
    2. counterGravity = new Vector3(0.0f , -Physics.gravity , 0.0f);
    3. rb.Addforce(counterGravity , Forcemode.acceleration);
    EDIT
    Sorry I didnt read your post correctly .
    I dont know what you mean by scale of gravity. Normally the magnitude is 9.81m\s²,
    so If you have a Mass of 10 the force is supposed to be 98.1 N.
    The force is proportional to the mass.
    Gravity * Mass = Force
     
    Last edited: Jan 4, 2016
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    It's unlikely that you're going to continuously apply a jump-force over time so when you apply this once then it'll be time-integrated i.e. only a small portion of it will be used during the simulation tick i.e. it'll be multiplied by the fixed-time-delta.

    For jump forces, apply them once during the fixed-update and use an impulse force that directly modifies the velocity instantly (no time integration).
     
    Magnumstar likes this.
  5. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I also read 1 mass = 1kg? Should I have a 10kg character?
    Reading here: http://docs.unity3d.com/Manual/class-Rigidbody.html
     
  6. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Nevermind, i see mass only matters in relation to the rest of your objects mass. If i set a scale and maintain that throughout the setup I cannot go wrong.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    Yes, mass is in Kg.

    It's not that Box2D cannot simulate masses larger than this but more that it is tuned to give good results using certain configurations. Mainly, Box2D is concerned with mass ratios during collisions where it is tuned for a mass ratio of 10:1 i.e. you should try to not have collisions involving bodies that have more than an order of magnitude difference. That said, this is when a stable/accurate simulation is required under delicate conditions such as when stacking multiple objects or under certain joint conditions. For most everything else, it matters very much less.

    The 0.1 to 10 refers to sizes in meters (soup-cans to buses). Box2D has been tuned to work with this size range but again, it's not to say that larger sizes will causes issues in your set-up.
     
    Magnumstar likes this.
  8. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    wouldn't it be easier to just unify everything to KG so we can use 1u = 1m & 1mass = 1kg? For everything? I am using objects with mass units in kg and not having any problems persay, but if 10 is meant to be the max, what possible useful function does this have only other than to confuse game designers?

    My main player character is mass 60 with a gravity multiplier of 10 and a height of 1.5 units, does this lower my chances of getting reliable results from Box2D? Would it be better to make him (and all relative objects, forces) lower to mass 6?

    Maybe a "pixel to units" style "kilograms to mass units" setting could be useful / less confusing if mass is a 0 to 10 scale?
     
    Last edited: Jan 5, 2016
  9. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    Yes, that is correct. The absolute mass isn't that important, just the ratios. Size is more important under the conditions I mentioned previously.

    Beyond that, Box2D is pretty resilient with most masses/sizes in practice for most games.
     
    enhawk likes this.
  11. freddannn

    freddannn

    Joined:
    Jan 2, 2016
    Posts:
    6
    Think about this: Gravity is constantly pulling your character downwards. When you add a force to it, it's only for an instance in time. Assuming that the character's speed (velocity) is 0, the character would briefly move upwards, then be pulled down by gravity again. If the character is actually already moving downwards then there might be no movement at all. Also keep in mind that there's more to movement than gravity, like intertia and drag. I'm not sure how that is modelled in Unity 2D.

    So in short, double check the various constants and potentially set velocity = Vector2.zero. You could also instead try and just set the velocity and see if that gets you the results you want.
     
  12. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I achieved the jumping I needed by lowering gravity on player jump and heightening it after he starts to decelerate past a certain threshold.