Applying transformation matrices to vectors

An issue I fell into recently: I was creating transformation matrices and when applying them to vectors, I was getting Vector4(0.0, 0.0, 0.0, 0.0) no matter how I constructed those matrices.

It turns out I was using Vector4.ZERO as a starting point (a vector with four zeros). My intention was to start at the origin. But the last zero, the w component, is used as a multiplier. If you multiply any numbers by zero, you know what we get.

What I should have done instead was to use Vector4.UNIT_W instead: (0.0, 0.0, 0.0, 1.0), representing a point located at (0.0, 0.0, 0.0) and the scaling factor being 1.0.

vtt

Maybe it’s obvious, but if not, now you know :slight_smile:

import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.ffmpeg.ScreenRecorder
import org.openrndr.math.Vector3
import org.openrndr.math.transforms.transform
fun main() = application {
    program {
        extend(ScreenRecorder()) { maximumDuration = 10.0 }
        extend {
            drawer.clear(ColorRGBa.WHITE)
            repeat(6) { x ->
                repeat(6) { y ->
                    val tr = transform {
                        translate(x * 100.0 + 20.0, y * 60.0 + 50.0)
                        rotate(x * 60 + y * 15.0 + seconds * 36.0)
                        translate(50.0, 0.0)
                    }
                    //val p = tr * Vector4.ZERO // BAD
                    val p = tr * Vector4.UNIT_W // GOOD

                    drawer.fill = ColorRGBa.PINK
                    drawer.circle(p.xy, 5.0)
                }
            }
        }
    }
}

If you want to apply transformations to any Vector3 (even if they are not at the origin), do it like this: val p = tr * myVec3.xyz1.

And with Vector2: val p = tr * myVec2.xy01.