[SOLVED] Comparing vectors reporting wrong value

Hi! I have a script, that makes an object float up and down. This whole math magic is running inside the update(dt) function. The code is pretty simple.

However it contains a start and end lerp to actually float up to a proper value, then float down when turning it off. To do this, I save the original position of the entity on the start, and compare the current position to this. So the update function (stripped down) looks like this:

Float.prototype.update = function(dt) {
    
    if(weShouldFloat_Bool){
        
        // Calculate and modify position here.

    } else {

        // If we should not float, lerp back to start here

        console.log("GetLocalPos():" + this.entity.getLocalPosition() + " and OriginalPos: " + originalPos + " are not equal.");

        if(!this.entity.getLocalPosition().equals(originalPos)){

            console.log("Running inside getLocalPosition().equals(originalPos) loop");
        } 
    }
};

Both of the values are printed as [0, -0.1, 0] yet the If runs as if they were not equal, as seen on these images below. I have tried getLocalPosition != originalPos as well as the getLocalPosition().equals(originalPos) as you can see in the code snippet above.

vec-comparison

vec-comparison-function

Sooo… what is exactly happening here?

EDIT:

Tried Vector1 !== Vector2 as well. Both of them are initialized as new pc.Vec3();

The !==/!=/==/=== comparison checks if they are the same JS object, not same internal values.

Use the equals function on the pc.Vec types to check if the vectors are the same value

Also bear in mind, that floating point values are rarely the same as another. You would want to check the distance between the two vectors and check if they are lower than a certain tolerance value (eg 0.0001)

I have created a function that checks each component for 2 decimals, as the values get really wonky after moving the mesh a tiny bit. Eg setting it to 0.2 gives results of 0.2000000004 and so because of the floating point precision.

I’ll try to re-write the check to see if it fixes it.

Thank you for the input!

Using the .equals everywhere instead of ==/=== comparison checks solved the issue. Thank you!