Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

I just wanted to share my appreciation to the dev team who implemented Vector2/3.Reflect() method

Discussion in 'Scripting' started by asperatology, Mar 7, 2020.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    976
    For the past 8 years, I still have issues with remembering how to implement a custom Math vector light reflection ray.

    Today, I just learned that Vector2 and Vector3 both have support for the class method, Reflect(). This method was implemented ever since 2014... or even earlier than that.

    This takes in an input vector and a normal vector, and outputs the reflected vector, unnormalized.

    Given how it's now easy to obtain the normal via calculating the vector of 2 points, this is a life-changing moment for me. Ball bouncing, physical contact collision responses, mirror reflections, ball simulations, they all now become easy to do.

    I just feel iffy about myself still having trouble with vector reflections, but I'm appreciating how it's now easier to make vector reflections. Maybe... a bit disappointed in myself for not being able to come up with a reflection algorithm.

    Please continue to make my life easier... or harsher...
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,130
    I get your appreciation, but it's actually fairly trivial
    Code (csharp):
    1. public static Vector3 Reflected(this Vector3 vector, Vector3 normal)
    2.   => vector - (2f * Vector3.Dot(vector, normal) * normal);
    it's the same in 2D.
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    976
    Yes, it is indeed trivial to do.

    But the fact that they went out and created a public method for trivial things is what I appreciated the most about.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,130
    Well, people asked for it, not everyone understands linear algebra that well, and besides it does belong to any serious gamedev toolbox. As I said, I get your appreciation, but I tend to appreciate the more invisible stuff that they did as well. This is what was truly hard.

    Sad is the life of a software developer, people love us only when the results are tangible, immediate, and fluffy, but when something has to be painstakingly and meticulously planned for weeks, every nut and bolt, every possible angle of attack, all we get is "Is it done yet? In plain English please..."

    This? This can be found on the internet in two clicks at most. It's not that groundbreaking.
    It was just a question of who will exactly get to fulfill the backlog of API changes for this month.

    On the other hand, don't get me wrong, I appreciate it. They didn't have to do it.
     
unityunity