In a prototype I'm building, a particle can spawn anywhere within a larger, confining circle. Important to note is that the particle will not spawn in the origin of the larger circle, but anywhere within it.
This particle will essentially be given a random x and y velocity and set on it's way.
When the particle hits the edge of the confining circle, I want it to bounce back inwards appropriately.
Unfortunately I'm extremely new to vector math, and have never really directly dealt with it before.
Given that I'm treating the particle as a point, and I don't care about any loss of force or friction, what should I ultimately do to the particle's x and y velocities after the bounce?
I will know the particle's x and y velocity, it's x and y coordinate when it hits the outer circle, the radius of the outer circle, and the x and y coordinates of the origin of the outer circle (since its origin will likely not be 0,0).
My brief research on vector math tells me I need to find the normal velocity at the point where the particle hits the outer circle and then negate that, but unfortunately I'm not sure how to do that exactly.
Answer
What you want to do can also be thought of as reflecting the particle's velocity off the plane tangent to the circle at the point of contact. If you know the equation for doing that, then all you need to know is the circle's normal at the point of contact. To get that, all you need to do is normalize the vector from the center of the circle to the point in question.
A property of circles is that the vector from any point on the circle to the center of the circle is orthogonal to the tangent line of the circle at that point. (In other words, the magenta lines are always at a right angle to each other.) And the easiest way to describe a plane is a normal vector (a vector orthogonal to every vector in that plane), which is easy enough to compute and turn into a unit-length vector.
At this point you can think of the problem as reflecting the velocity vector v across the plane with normal n (unit-length). The formula comes from taking the length of the projection of the vector v onto the line n, which is the dot product n · v. So the part of v that is perpendicular to the plane (the perpendicular component) is (n · v) n. The "rest" of it is the parallel component: v - (n · v) n. If we sum those two components, we get back to the original vector:
v = [v − (n · v) n] + [(n · v) n]
The reflection is the that same expression, but with the perpendicular component negated:
r = [v − (n · v) n] − [(n · v) n]
And simplified:
r = v − [2 (n · v) n]
No comments:
Post a Comment