Hello,
my sugestion is to add a at(index) and setAt(index, v) to the vectors. For example:
with at and setAt, we can avoid the code repetion to check each value of the vector.
let v = new BABYLON.Vector3(1,2,3)
let p = new BABYLON.Vector3(4,5,6)
if (v.x > p.x)
//do something
else if (v.y > p.y)
//do something
else if (v.z > p.z)
//do something
Note we check which value with the dot operator. If Vectors had a at(index) our code stays more simple:
let v = new BABYLON.Vector3(1,2,3)
let p = new BABYLON.Vector3(4,5,6)
for (let i = 0; i < 3; i++)
if (v.at(i) > p.at(i))
//do something
With setAt, we just use something like:
for (let i = 0; i < 3; i++)
v.setAt(i, 42) //for set to 42
Sorry, the code formating doesn’t not format here.
let v = new BABYLON.Vector3(1,2,3)
let p = new BABYLON.Vector3(4,5,6)
if (v.x > p.x) {
//do something
} else if (v.y > p.y) {
//do something
} else if (v.z > p.z) {
//do something
}
For me the occasional coding advantage of using at and setAt is outweighed by the loss in readability.
The at and setAt will be only used on some special cases.
For example:
Yesterday I had to do a code like this:
look as the code stays without at and setAt
v, p, min, max are Vector3
if (v.x < 0)
p.x = (p.x - min.x) / p.x;
else if (v.x > 0)
p.x = (p.x - max.x) / p.x;
if (v.y < 0)
p.y = (p.y - min.y) / p.y;
else if (v.y > 0)
p.y = (p.y - max.y) / p.y;
if (v.z < 0)
p.z = (p.z - min.z) / p.z;
else if (v.z > 0)
p.z = (p.z - max.z) / p.z;
Now, look as the code stays with at and setAt:
for (let i = 0; i < 3; i++) {
if (v.at(i) < 0)
p.setAt(i, (p.at(i) - min.at(i)) / p.at(i));
else if (v.at(i) > 0)
p.setAt(i, (p.at(i) - max.at(i)) / p.at(i));
}
At and setAt will not substitue the dot operator, but is just to facilitate iterates over the vectors. And, in some cases like the above, it will facilitate the iteration over the vector.
@Gijs wow man, so simple! With this, now I agree that there isn’t need for at and setAt. @aWeirdo I did something like this, but not directly added the at and setAt, cool idea.