I need to make vector operations without seting my vectors

I noticed that the vector math functions, such as .add and .mult set the value of the vector you input.

I’m doing somewhat complex operations so I need to use this functions many times without altering the value of the vector, is there any other way than making my own functions? And isn’t this the least convinient way of implementing this functions?

Vectors in Iron are mutable and the functions for them are designed with the same principle – however, there are considerations to change that.

But the node shouldn’t change the input vector. If you look in the .hx source code you can see that the input vector is actually copied into a new object so that this doesn’t happen. A example file would indeed really help here, I can’t reproduce this as well.

2 Likes

Probably this situation causes problems:

var v1 = new iron.math.Vec3(1, 1, 1);
var v2 = new iron.math.Vec3(2, 2, 2);
var v3 = v1.add(v2);

Resuls:

v1 = (3, 3, 3)
v2 = (2, 2, 2)
v3 = (3, 3, 3)
2 Likes

Probably this situation causes problems

Yes, thats the mutability of Iron vectors. But the VectorMath node calls v.setFrom(v1) (v1 is the input vector) which should actually prevent this by copying the vector.

Judging by the message, they write the code there, and not use logical nodes. And it is believed that the .add function should only return a new value.

2 Likes

Judging by the message, they write the code there, and not use logical nodes.

Oh of course, I was blinded a bit by the logic node screenshot above^^

A solution is to use myVec.clone().add(otherVec). Of course this is tedious to write but unfortunately that’s the current state (I hope that it will be improved when Iron finally moves to C). The functions for kha.math.FastVector3 on the other hand work like vectors are immutable, so if you don’t need to use Iron’s math classes, just switch to Kha’s.

Another solution would be to create a result vector and call addvecs():

var result = new Vec3();
result.addvecs(vec1, vec2);
3 Likes

For some reason when I read it, I thought it was logic nodes, sorry I already had removed the message. :sweat_smile:

It can be a little shorter:

var result = vec1;
result.add(vec2).add(vec3).mult(vec4);

I think this is how you can write a suitable formula.

Many thanks everyone for your answers, now I know my options. And sorry forn not being more specific.