Rubygame 3: Day 16, Part 2

I've checked in my transformation Matrix class. Those who are curious can take a peek at the C code, but more importantly the header file. Those who are very curious can compare with the Ruby implementation of Matrix3 I had been tinkering with in the past.

In addition to the speed advantages of simply using C over Ruby for the hard math, I also decided to discard the bottom row of the 3x3 matrix, which would always be [0, 0, 1] for valid transformations. In addition to saving a little bit of memory that would otherwise need to be allocated for 3 floats that never change, I was also able to simplify the multiplication math by assuming the bottom row will always be [0, 0, 1]. The zeroes handily eliminate some factors in the equations, and X * 1 can just be simpified to X.

I also took the opportunity to horribly pervert Chipmunk's Vect struct by adding that extra Boolean field, called P (for point!), that marks it as being a point rather than a vector. (Actually, I used an int for that. I did feel a little bad about allocating a whole byte when I only need a bitfield, but then I was informed the system wouldn't have allocated less than a byte for it, anyway.)

In actuality, that Boolean field is standing in for a third value that is 1.0 for points and 0.0 for vectors when using homogenous coordinates, as Scott Lembcke noted earlier tonight. If this were "real" matrix math, the third value would be a factor in the equations when applying the transformation matrix to a vector/point. But rather than waste some multiplications, I'm just using it as a Boolean and rerouting to a simplified equation if it's 0.

All in all, it was fun to get my hands dirty with some geeky math stuff.


Comments

Have something interesting to say about this post? Email your thoughtful comments to comments@rubygame.org.