Thread: Matrix transformation tables

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Matrix transformation tables

    I'm experimenting with matrix transformations on 2 and 3 dimensions, hoping to construct a application that teaches the mathematical concepts behind matrix algebra. It's not really game related, it's about the actual math behind it. But I'm having trouble finding a comprehensive table of matrix transformations.

    Those of you working on 2D and 3D, do you use these tables? Do you have any good reference I can use?

    I'm after the multiplication matrixes (for 2 and 3 dimensions) for rotation on any axes, scaling, reflection, slanting, and projections.
    Last edited by Mario F.; 09-01-2015 at 06:03 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are you looking, for example, something like this pdf?
    Devoted my life to programming...

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Any good 2D/3D math book should cover this in the first few chapters. What I found more interesting than the tables is their mathematical derivation from basic principles. The tables were most useful when writing a Matrix3D style API, where you can construct the matrix just by reading what each value is off the table.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by GReaper View Post
    Are you looking, for example, something like this pdf?
    That is perfect, more than I expected. Very comprehensive and even includes translations. Thanks so much!

    Quote Originally Posted by MacNilly View Post
    Any good 2D/3D math book should cover this in the first few chapters. What I found more interesting than the tables is their mathematical derivation from basic principles.
    That is sort of my purpose, to come up with interesting ways to show students the power and interesting uses of algebraic manipulation of matrixes and how they can represent an abstraction of so many different types of more concrete concepts. I've already constructed some interesting models using parametric equations as a basis for matrix representation and manipulation. But these are limited in their ability to get that "cool!" factor from the kids. Besides I've been basing most of that on physics problems because of how easily their graphs can be recognized.

    But physics isn't necessarily an interesting topic to many students. I'm hoping that with a better understanding on transformation matrixes, I can do better and pique their interest.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    The 3D graphics course I took was one of the cooler courses because it tied math in with games. I'm sure a lot of students will feel the same way.

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Are you going to include basic vector algebra, too?

    One of my favourite problems has to do with spheres and horizons and rendering spheres:
    Matrix transformation tables-sphere-horizon-png
    From Pythagorean theorem we know that
    h2 = d2 - r2
    Because we have a right-angled triangle,
    β = 180° - α
    and
    cos β = r / d
    cos α = h / d

    The interesting bit:

    When rendering images that contains lots and lots of spheres, we really want to know if our view ray intersects sphere (, r), being the location of its center, and r radius.

    To make everything really simple and fast, we rotate and translate the universe so that the observer eye (camera) is at origin. (It is usually also oriented so that X increases to the right, Y down, and positive Z is visible.)

    First, we calculate the unit vectors, i.e. scale the two vectors to length 1:
    ñ = / ∥∥ = / sqrt( · )
    = / ∥∥ = / sqrt( · )
    We can now apply the dot product:
    ñ · = cos φ
    If φα, ray intersects sphere.
    Since φ ≤ 90° and α ≤ 90°, their cosines are monotonically decreasing and nonnegative, and we can write the above as
    If (cos φ)2 ≥ (cos α)2, ray intersects sphere.
    Substituting stuff back, we get
    (ñ · )2h2 / d2
    (ñ · )2 / ( · ) ≥ h2 / d2
    (ñ · )2 ≥ ( · ) h2 / d2
    (ñ · )2 ≥ ( · ) (d2 - r2 ) / d2
    Since d2 = · , we have
    (ñ · )2d2 - r2
    (ñ · )2 · - r2
    You can write it also as
    ñ · ≥ sqrt( · - r2 )
    but computers are much faster at multiplication than they are in square roots, so the squared one computes faster.

    Note that the angles implicitly assume that you only consider spheres in the positive direction, i.e. that
    ñ · ≥ 0
    Otherwise you're basically considering spheres behind you.

    We can now go back to Cartesian coordinates. If we use
    ñ = (nX, nY, nZ)
    = (pX, pY, pZ)
    then we can simplify the ray-intersects-sphere test to
    (nXpX + nYpY + nZpZ)2pX2 + pY2 + pZ2 - r2
    The total cost of the test is 8 multiplications, 4 additions and 1 subtraction, and one comparison.

    The right hand side is invariant in rotations, and only changes at translations. (Since this requires nX2 + nY2 + nZ2 = 1, we cannot cheat by translating ñ instead.)

    (The form of the inequality is such that we can vectorize two (at double precision) or four (at single precision) sphere tests at once using Intel/AMD SSE vector extensions, or four (at double precision) or eight (at single precision) sphere tests using AVX. However, for optimum vectorization, each component -- say, sphere radius, or center X coordinate -- must be in their own array, with different spheres consecutive in that array.)

    You can even modify your transformation matrix calculations so that in addition to the transformed sphere center coordinates, it also calculates the right hand side of above at the same time. (This means four floating-point variables per sphere, which happens to also vectorize really well.) The ray check cost then drops to 4 multiplications, 2 additions, and one comparison.

    There are similar tricks that are derived using vector algebra and geometry, that yield very simple expressions using Cartesian coordinates, for calculating the surface normal vector at the intersection point, the distance from eye/camera to the intersection point, and other interesting stuff needed to either ray-trace or apply one of the shading techniques to decide what color to paint each pixel.

    Descriptive geometry is really fun, in my opinion.

    I wonder if making a simple Python program that renders such sphere images, but lets the user tweak the calculations any way they wish, might engage the students? You know, instant gratification and all, but requires understanding and effort to get anything really neat done.

    For example, you could let the users rotate the view using a virtual ball. However, you use cumulative transformation to a single matrix, deliberately left unnormalized. When they notice the image is getting .. squished?, you can explain what happens and how to fix it. ("Add transform = transform.normalize() after the rotation transformation, there.")

    I prefer to convert the rotation part to a quaternion, normalize the quaternion, and then convert that back to matrix form, just to treat all directions equally. It's also better to "blend" between two rotation matrixes using quaternions, as you get more reasonable transitions that way.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Nominal Animal View Post
    Because we have a right-angled triangle,
    β = 180° - α

    β = 90° - α

    Quote Originally Posted by Nominal Animal View Post
    When rendering images that contains lots and lots of spheres, we really want to know if our view ray intersects sphere (, r), being the location of its center, and r radius.
    It's an interesting problem, thanks. Well worth investigating of ways to include it.

    I think I'm seeing another efficient solution on the complex plane. And I may save it for that. Instead of matrix calculations, I may use this with polar coordinates and complex numbers in polar form.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Mario F. View Post
    β = 90° - α
    Ouch! Right.

    Quote Originally Posted by Mario F. View Post
    It's an interesting problem, thanks.
    To get that far, you obviously first need to introduce ways of how to rotate and translate the world coordinates, so that the eye/camera is at origin, and view rays intersecting the view plane, and so on.

    (You can construct a rotation matrix by finding the three perpendicular axis vectors; this is an often used method. To understand why it works, you must understand why and how the vector-matrix multiplication transforms the vector.)

    Also, the figure can also be used to answer the question "If my eye level is X meters above the sea level, assuming a calm sea, how far can I see? How far is the horizon?".

    In general, coordinate transformations, and looking for a way to see the problem at hand in a way that simplifies the problem is extremely useful. The Wikipedia trilateration article is one example. It describes how to find the coordinates to a point, when you know the distances to three fixed points. The old version used all kinds of trigonometric functions to transform to the simple coordinate system (where the three fixed points are coplanar), my suggestion and example C source simplified the solution quite a lot.

    I guess you could summarize my approach as "if you grok these vector algebra and matrix operations, and a bit of descriptive geometry, you can simplify quite complex-seeming problems, and save yourself a lot of effort."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Geometry Transformation and Bilinear Interpolation
    By KFill Filter in forum C Programming
    Replies: 5
    Last Post: 11-09-2012, 12:49 PM
  3. simple string transformation function
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2010, 05:34 PM
  4. Buggy Transformation Update Function..
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-04-2006, 04:52 PM