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

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/wikipedia/commons/thumb/0/0b/30_degree_reference_angles.svg/730px-30_degree_reference_angles.svg.png

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

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.

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).

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.

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:

Vector3 direction = Vector3.right; // is already normalized, and is the same as 'new Vector3(1,0,0)'
Vector3 distance = 10;

Vector3 changeInPosition = direction * distance; // equals (10,0,0)

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:

Rigidbody rb; // get component in Awake or Start, etc.

private void OnTriggerEnter(Collider other) {
    Vector3 direction = (transform.position - other.transform.position).normalized;

    // get our current speed
    float speed = rb.velocity.magnitude; // magnitude is the length of the vector

    // set velocity with the same speed, in the new direction
    rb.velocity = direction * speed;
}

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.

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.

1 Like