Reflection off a Line

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey Guys, I'm trying to implement basic collision response of a ball bouncing against a non-axis aligned line segment. I imagine there are a ton of examples of how to do this, but I'm not sure what it's actually called, so I'm having a little trouble finding resources. What is the mathamatical term for this? Or if it is fairly simple and you feel like posting some forulas or example code that would be great! The goal is to find the final forward vector of the ball. I know it's current forward vector, and it's speed. I also have access to the closest point on the line we are bouncing off of, as well as the normal of said line (I treat the line as a plane, but the game is '2D' so the plane is always rotated around the z-axis only). Thanks guys! Matt

Share on other sites
The term to google for is 'vector reflection'. The equation is short and simple though, so I'll just go ahead and give it here:
v' = v-2(v.n)n
Where v is the input vector, v' is the reflected vector, n is the unit-length normal, and '.' is the dot product.

Share on other sites
As jyk said, vector reflection is what you're interested (it is the second diagram on that page).

To intuitively understand the formula:

- Be sure n is normalized.

- Imagine v as pointing towards n, and that the direction of n is in the same general direction as that of v.

- Since n is a unit vector, then the component of v parallel to n is simply v.n (this can be verified by trigonometry). We can build a vector from this by (v.n)n.

- Subtracting (v.n)n from v effectively reduces that component of v parallel to n by |(v.n)n| or equivalently just (v.n). The perpendicular component is left unaffected through this entire process.

- Subracting (v.n)n again from (v - (v.n)n) effectively flips the component of v initially parallel to n, thereby flipping the direction of v, which completes the reflection.

Share on other sites
Awesome! Worked great, thanks guys!

Another question for yah: does friction reduce velocity in all directions? Or is depenedant on the stuff like the angle we hit a surface at, etc?

Basically right now, when I detect a collision and apply the formula above, I simply scale the new forward by something like 0.75, so every time it collides it slows down a bit. But it seems more 'realistic' when i scale the differen components by different numbers (less in the x, more in the y).

Just curious what the real theory behind this stuff is!

Thanks again for answering my OP! Helped alot!
Matt

Share on other sites
you can decompose the vector into the part parallel to the normal of the collision, and the part perpendicular to it (or tangent to the surface).

so,

replace

V -= 2 * (V . N) * N

by

Vn = (V . N) * N; // normal component, or impact velocity.
Vt = V - Vn; // tangencial component, or surface velocity.
V = -Vn * Restitution + Vt * (1.0f - Friction); // new velocity after collision

to apply friction, you can reduce Vt after collision.

restitution is a number between 0, and 1.

it's the amount of 'bounce' you get off the collision.

Friction is also a number between [0, 1].

If you set it to 0.1f, you'll slow down a bit along the surface.

Note that's a vbery crude way of doing it, but easy enough it;s worth a try.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 41
• Forum Statistics

• Total Topics
634837
• Total Posts
3019558
×