• bampop@lemmy.world
      link
      fedilink
      English
      arrow-up
      11
      ·
      edit-2
      2 hours ago

      Let’s say I’m writing a computer game, which features a robot holding a gun. I have a list of vectors representing the 3D model of the gun, but how do I know where to draw those points on screen? To transform the gun model to screen coordinates I just need to do this simple process:

      1. scale the gun model so it’s the right size for the game
      2. rotate and translate it so it will be in the same coordinate space as the robot’s hand, where the wrist joint is at (0,0,0)
      3. rotate again to reflect the current angle of the wrist, now it aligns with the forearm
      4. translate by forearm length so the elbow joint is at (0,0,0)
      5. rotate again to reflect elbow angle, now it aligns with upper arm
      6. translate by upper arm length so the shoulder joint is at (0,0,0)
      7. rotate again to reflect shoulder angle, now it aligns with body
      8. translate by shoulder position so body center of rotation is at (0,0,0)
      9. OK let’s just assume we’re defining body position directly, so we’ll apply another rotation and translation to reflect the robot’s position, now our coordinates are in “game space”
      10. of course the “camera” through which we view the action might be moving as well, so we’ll need another rotation and translation so transform the coordinates into “camera space”
      11. we need to apply 3D perspective to get the on-screen coordinates. If the z axis of camera space were in the direction we are looking, with 0 at the view point, you could get x and y screen coordinates by dividing camera space coordinates by z, and scaling the result as needed to fit the screen

      Oh dear, that wasn’t so simple. Are we going to do this for every vector in the gun model? Well, as it turns out, the first 10 steps are all linear transformations that can be represented by a matrix. And we can encapsulate the entire process by multiplying those matrices together, so instead of 10 operations, we can combine it into one, a single matrix which will take us all the way from the gun model to a position in camera space. So we just need to pass the graphics card some instructions to tell it what to do, plus the list of vectors for the gun model, plus the combined matrix for transforming them.

      There’s many other cool things to do with matrices in graphics programming but that’s a starting point.

    • Aqarius@lemmy.world
      link
      fedilink
      English
      arrow-up
      18
      ·
      6 hours ago

      You know how you can write “5+5+5-4-4-4” as “35-34”, and then use it for weird shortcuts like “3*(5-4)”? Well, matrices make for relatively easy handling of certain systems of equations, letting you fiddle with the whole system at once instead of restating equations one by one.

        • Aqarius@lemmy.world
          link
          fedilink
          English
          arrow-up
          4
          ·
          3 hours ago

          Essentially.

          A straightforward example would be an equation system: 2x+4y=10, 3x+5y=13. You could solve it manually, or you could call Cramer’s rule and plug the numbers into the formula:

          Ax+By=C
          Dx+Ey=F

          x=[CB/FE]/[AB/DE] = (CE-BF)/(AE-BD)=(10*5-4*13) / (2*5 - 3*4) = (50-52)/(10-12)=1,
          y=[AC/DF]/[AB/DE]=AF-CD / AE-BD = -4/-2=2

          Pure algo, no thinking required. Also note that you don’t strictly need x to get y and vice versa.

          In a more complex example: You’re making a program to draw something in 2D. You could implement mirroring, rotation, scaling etc…, or, you could declare each point (xy) a vector V=[X Y], implement matrices, and then V times [1 0/0 1] gives you V, [-1 0/0 1] gives you V mirrored on the Y axis, [1 0/0 -1] mirrors on the X axis, [j 0/0 j] scales it by j, [cosw -sinw/sinw cosw] rotates it by w… Makes life much easier.

        • lime!@feddit.nu
          link
          fedilink
          English
          arrow-up
          1
          ·
          edit-2
          4 hours ago

          that’s a good parallel. as for more uses, a graphics card is basically a big parallel math coprocessor for matrices. so CFD? matrices. call of duty? matrices. all this “ai” stuff nowadays? matrices.

    • Farid@startrek.website
      link
      fedilink
      English
      arrow-up
      2
      ·
      6 hours ago

      Take into account that I have my linear algebra exam in a week and I merely hope to get a passing grade, but apparently, in the very least they are useful for solving systems of equations using very simple algorithm-based operations.

      • neukenindekeuken@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        9
        ·
        7 hours ago

        The irony is there’s a bridge design exactly for that process. Its called a wicked bridge. The only way to know how its going to fail, is by building it in such a way that it fails, non catastrophically, and informs them of exactly where the stress points are so they can build a real bridge to handle the environment and traffic.