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. Dismiss Notice

2D Collision/Physics Conundrum

Discussion in 'Scripting' started by Nigey, Jul 8, 2014.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi guys,

    I'm trying to do something very simple and specific, but I can't see a way to get Unity to do it.

    I have two objects using Rigidbody2D for their movement with gravity.

    One I want to bounce off the other. One I want to bounce away, the other I want to be unaffected. If I make either kinematic then one of them wont be able to use gravity and AddForce to move around.

    I've tried setting the one I don't want to move to have 'new Vector2(0,0)' velocity after collision with it, but the initial physics of the collision is called before OnCollisionEnter() so it still moves a little bit.

    Can anyone think of an alternative, asides actually setting the immovable object's mass to 1000000 :S.

    Thanks!
     
  2. Scissors

    Scissors

    Joined:
    Jun 13, 2013
    Posts:
    22
    I would make the one you dont want to react IsKinematic. Rigidbody2D has an inherrent function called MovePosition which does check for collision along the way. Use this instead of AddForce to move your player around. as for gravity, I would simply use the same MovePosition method to manually apply gravity. If you want to use the same gravity as every other object, simply reference the project settings' gravity co-efficient. does that help?
     
  3. Scissors

    Scissors

    Joined:
    Jun 13, 2013
    Posts:
    22
    Theres not really a way to achieve the same result. manually calculating movement speeds and adding a downward force for gravity to your movement is the best option I can think of, unless I dont understand what youre trying to do entirely. Apply the movement using rigidbody2D.MovePosition(Vector3: Direction)
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Yeah that does make sense.. The only issue is that applying gravity via MovePosition() wont take into account the objects mass vs the force of gravity and any other affects the physics engine normally takes into account.

    Is there maybe a more complete physics algorithm out there for applying force to an object with mass?
     
  5. Scissors

    Scissors

    Joined:
    Jun 13, 2013
    Posts:
    22
    Unfortunately not. Unity really isnt doing alotta calculations to determine the forces being applied. including mass in your calculation is as simple as multiplying your end force by the mass number associated with the rigidbody. so endForce * rigidbody2D.mass. anything else I can help with or is that cool? :)
     
    Nigey likes this.
  6. Scissors

    Scissors

    Joined:
    Jun 13, 2013
    Posts:
    22
    Keep in mind, in the real world, a feather and a ton of bricks fall at the exact same velocity in a vacuum. However, due to displacement of air (or what ever fluid/gas the object is in), an objects volume also needs to be taken into account to calculate real-world physics. GAMES DO NOT DO THIS. so whilst in the real world you would take many more factors into account like volume, viscosity etc., Games currently only take mass and drag into account. in other words, If a real-world object had a mass of 1 gram but a volume of 2kmsq, it would float due to the apparent buoyancy caused by displacement of air. In games, it would just fall really slowly, regardless of volume displaced. Incorporating drag into the equation is not hard, but my brain is exhausted and im struggling to think so ill have to get back to you on how to do that :p sorry
     
    Nigey likes this.
  7. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    That's an awesome explanation, thanks Scissors I'll give it a go :D
     
    Scissors likes this.
  8. Scissors

    Scissors

    Joined:
    Jun 13, 2013
    Posts:
    22
    Good luck with the game, glad I could help :)