0
\$\begingroup\$

So there's a ball moving on a 2D plane with any vector. When it comes into contact with a collider, it should reflect in a linear fashion. What formula should be used in order to find the angle that this new direction should have? The pieces of information that are known are:

  • The X and Y velocity of the ball when it was hit
  • The angle it was travelling at when it was hit
  • The point of contact

I've found many solutions for if the ball can only hit vertical or horizontal walls, but have yet to find a one for a general case.

\$\endgroup\$
  • 1
    \$\begingroup\$ We usually don't do this with angles, but by reflecting the velocity vector along the collider's normal. Do you have these two vectors, or are you required to work in angles here? \$\endgroup\$ – DMGregory Nov 8 '18 at 8:19
  • \$\begingroup\$ I have the ball's vector and angle. I'm wondering how to find the collider's normal which I've collided with. If I'm able to find the resulting x and y components of the reflected vector then I can find the new angle too, I know the equation to do that. How would I find the collider's normal and the resulting vector? \$\endgroup\$ – Morgan Nov 8 '18 at 21:21
  • \$\begingroup\$ How are you detecting the collision in the first place? OnCollisionEnter2D, OnTriggerEnter2D, raycasts/circlecasts, or some other method? \$\endgroup\$ – DMGregory Nov 9 '18 at 0:00
1
\$\begingroup\$

Exit angle is equal to entry angle.

Sketch

In the sketch, angles labeled X are equal to each other and angles labeled Y are equal to each other. The line is the normal (perpendicular) to the wall (rectangle)

However, if you are doing this programmatically (and I assume you are, since you are asking about Unity on the game dev so) you may be better off not worrying about what the resulting angle will be.

What I suggest you do, is take your balls velocity vector, rotate it by an amount equal to the slant of the wall, flip the x-component of the vector (assuming you rotated so your wall would lay flat), then rotate the resulting vector back again.

If your ball has no velocity, since you seem to only have an angle, simply create a vector of magnitude 1 (or whatever) and the correct angle, and apply process above.

\$\endgroup\$
  • \$\begingroup\$ This description helped me come to the correct result for my specific issue. I believe your way of doing it is correct so I'll mark it as so. Thank you. \$\endgroup\$ – Morgan Nov 20 '18 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.