1
\$\begingroup\$

I have a game I'm building in Unity with some objects orbiting a sphere via rotating around a pivot point (0,0,0) at a radius of 10 units on the z-axis. The orbiting objects all have the same velocity. This all works great, but now I need to add a bouncing effect when they run into each other so they start orbiting in a new direction when colliding.

I am currently detecting a collision between two orbiting objects, and trying to determine the resulting reflection vector of the collision. The orbiting objects all have sphere colliders on them.

Here is a relevant code example for how I am orbiting the objects:

var velocity = 15f;
var directionVector = new Vector2(Random.Range(-1f, 1f) * velocity, Random.Range(-1f, 1f) * velocity);
transform.Rotate(directionVector.y * Time.deltaTime, -directionVector.x * Time.deltaTime, 0.0f);

Here is how I am currently trying to reflect the collision on the directionVector:

var bounceVector = Vector3.Reflect(transform.forward, collider.gameObject.transform.forward).normalized;
directionVector.x = bounceVector.x * velocity;
directionVector.y = bounceVector.y * velocity;

This does cause the objects to "bounce" off of each other, but the bounce direction doesn't seem right, so I have something screwed up. I also feel like this probably doesn't even matter that it's on a sphere because all I need to do is take two direction vectors and find their resulting reflection vector on a collision. Any thoughts?

\$\endgroup\$
1
\$\begingroup\$

Try using the normal. I'm not familiar with Unity, so I won't give you the straight code solution.

1)Find a collision point C

2)Calculate normal N using this formula, where O is an origin of the opposite sphere

N = normalize(C - O)

3)Find reflected movement vector using this formula, where V is an actual movement vector of the sphere

R = V - 2(dot(V, N))N

(Or use Unity's Reflect function instead)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .