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

Any math/code boffins?(Bounce object in direction of collision angle)

Discussion in 'Scripting' started by sanatan, Aug 24, 2017.

  1. sanatan

    sanatan

    Joined:
    Nov 14, 2015
    Posts:
    10
    Hi Guys

    Im using a custom movement system and trying to add some basic physics. This one has me stumped for days, Im sure the answer might be simple but I could just not wrap my head around it.

    My question is
    Using OnTriggerEnter to pick up the collision, How would I get the angle of collision between two objects?.

    If Object A moving right collides with Object B moving left:
    Then Using a 360 degree format like https://upload.wikimedia.org/wikipe....svg/730px-30_degree_reference_angles.svg.png

    A collides at 180 degrees and B at 0 degrees, How do I get those values?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    https://en.wikipedia.org/wiki/Boffin

    Are we talking basic shapes colliding? Like centerpoint to centerpoint, or collision surface normals?

    At any point you can get the direction between two objects with (object1.position - object2.position).normalized, which would be the direction from object2 to object 1.

    Otherwise you'll need to get the surface normal of the colliding surfaces, and use something like Vector3.SignedAngle to find the collision angle of those polys. You can do that using a contact point of the Collision, or raycasting.

    https://docs.unity3d.com/ScriptReference/Collision-contacts.html
    https://docs.unity3d.com/ScriptReference/ContactPoint.html

    That being said, none of the angle functions return 0-360, but with SignedAngle you can add 180 to put it in that range.

    Also, you can't get those contacts with Triggers, because OnTriggerEnter only supplies the other collider. You need to use OnCollisionEnter to get the Collision object parameter (or do a custom raycast solution to get the info you need at the time of TriggerEnter).
     
    Last edited: Aug 24, 2017
  3. sanatan

    sanatan

    Joined:
    Nov 14, 2015
    Posts:
    10
    hi jeffrey

    Thanks for the assistance.

    Basic shapes colliding.
    So I did this based on what you said
    Debug.Log((transform.position-other.transform.position).normalized.ToString());
    And ok it returns a vector 3 position like
    (-0.9, 0.0, -0.4)

    Now I dont understand how to further take it from there?. Because I want them to bounce in the opposite direction based on that info. How would I use that because I dont see it as an angle or direction still?

    Btw, Pseudo code would be fine. I generally can work around scripting.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    A quick FYI, the Debug.Log function will automatically do a "ToString()" on objects you pass in.

    So the vector that subtraction gives you can be imagined as an arrow pointing from "other" to "transform". The length of that vector is equal to the distance between them. When you "normalize" it, it shrinks the arrow down until it has a length of 1 but it still points the same way. That's what is commonly called a "direction", a normalized vector or "unit vector" can be used as a direction.

    Generally speaking, you would multiply a direction by a distance.

    For example, here is how you can move something 10 units in a direction:
    Code (CSharp):
    1. Vector3 direction = Vector3.right; // is already normalized, and is the same as 'new Vector3(1,0,0)'
    2. Vector3 distance = 10;
    3.  
    4. Vector3 changeInPosition = direction * distance; // equals (10,0,0)
    5.  
    6. transform.position += changeInPosition;
    So one way to get this bounce to happen is to have each object get the direction from the other one to themselves, and set their velocity going in that direction.

    Keep in mind that Triggers are generally not used for blocking collisions like this, but without changing that, this could potentially work:
    Code (CSharp):
    1. Rigidbody rb; // get component in Awake or Start, etc.
    2.  
    3. private void OnTriggerEnter(Collider other) {
    4.     Vector3 direction = (transform.position - other.transform.position).normalized;
    5.  
    6.     // get our current speed
    7.     float speed = rb.velocity.magnitude; // magnitude is the length of the vector
    8.  
    9.     // set velocity with the same speed, in the new direction
    10.     rb.velocity = direction * speed;
    11. }
    Assuming both objects have this function, and both objects get their respective OnTriggerEnter called (I would hope that is the case every time), this will send both objects away from eachother at the same speed they were going when they collided.
     
    Last edited: Aug 25, 2017
  5. sanatan

    sanatan

    Joined:
    Nov 14, 2015
    Posts:
    10
    Wow It works. I also actually understand *how* it works because you explained it so intuitively.

    Thank you very much. May blessings and good karma follow your path buddy. Most appreciated.
     
    LiterallyJeff likes this.