Scaling by a matrix

I’m trying to make a matrix that will perform a scale, rotation, and translation with the following code

function startDirMatrix(start, dir, matrix) {
  // Generates the 4x4 matrix which would transform <0,1,0> into the vector with tail at start and tip at start+dir.
  // Rotation -> Scale -> Translate
  const len = dir.length();
  BABYLON.Matrix.RotationAlignToRef(new BABYLON.Vector3(0,1,0), dir.normalizeToNew(), matrix);
  matrix.scale(len);
  matrix.setTranslation(start);
  return matrix;
}

You can see the result here: Issue / AxiomTutor | Observable

It seems to successfully perform the rotation and translation, but not the scale. I’ve also tried matrix = matrix.scale(len) which also doesn’t work.


On that topic, when using the documentation, I often find it hard to tell which methods are instance methods and which are class methods. I also often have a hard time telling whether a method mutates the input or merely returns a new object. I see all these symbols in the documentation, like a blue dot, and a blue square, and a some of them have white symbols inside. I can’t quite decipher all the symbols – is there a legend, or reference for what they mean?

scale is used to scale all the “components” of the matrix not creating a scale matrix.

What you are looking for is the Matrix.Compose function Matrix | Babylon.js Documentation

1 Like

The convention is PascalCase is static (if you mean that by Class methods). properCase is instance method. At least with the math libraries InPlace and ToRef are pretty good indicators. The comments for intellisense usually indicate.

It’s my understanding that there can be class functions (i.e. functions called on the entire class rather than an instance of the class) which are static or non-static. Likewise there can be instance functions (called on instances rather than the class itself) which are static or non-static.

For example, if dir is an instance of Vector3 then dir.length() seems to me to be what I’d call an “instance” method, and I believe it would be static because it does not change the properties of the class or instance. (I could be wrong about what “static” means, but this reflects my best attempt at understanding what I’ve read.) But dir.addInPlace(v) would be non-static because it mutates the instance. If there are class variables in Vector3 (I’m not sure that there are, but if there are) then if any function mutates them then I’d think that method would also be called non-static.

On the other hand BABYLON.Vector3.Zero() is what I’d call a class method and it is static. Since I’m not sure whether any class in BJS has class variables, I can’t come up with an example of a class method that I’d call non-static. But if there were class variables, then it would be possible for a class method to be non-static.

So anyway, long story short is that I don’t believe that the distinction I am looking for in the docs is static versus non-static.


Edit: As I look over the documentation, I think maybe I’m picking up that it uses “static” to mean what I’ve been calling “class methods”. Maybe I just have a wrong idea about what static means – perhaps dir.addInPlace is what most programmers call “static”? I dunno, I thought static had to do with whether it mutates variables but maybe not.

As I would probably explain it pretty poorly: here is the JS reference: static - JavaScript | MDN and TS https://www.typescriptlang.org/docs/handbook/2/classes.html#static-members They would be accessed through “class”.xxx

The none static class members would be on the other hand per instance and accessed through “instance”.xxx