Weird issue with comparison


image

image
image
so basically, I’m trying to avoid using a cube that was already selected earlier, but then the following happened
even if they are the same objects (I have even compared they position via a.equals(vec3), it goes into a if-statement and assigns this cube to closest one, which has already been used
How is this possible and what am I doing wrong?

DrawBeziereCurve.prototype.findClosestEntity = function(resultEntity)
{
    var closestEntity = null;
    var closestDistance = 1000;
    var diff = 0;
    var tmpPos = resultEntity.getPosition();

    // for (var i = 0 ; i < this.childrenEntities.length; i++) 
    // {
    //     var distance = tmpPos.distance(this.childrenEntities[i].getPosition());
        
    //     var tmpPos1 = this.childrenEntities[i].getPosition();

    //     if (distance < closestDistance  && this.childrenEntities[i].name != resultEntity.name && this.previousEntity.equals(tmpPos1)) 
    //     {
    //         closestDistance = distance;
    //         closestEntity = this.childrenEntities[i]; 
    //         console.debug(this.childrenEntities[i].name);
    //     }
    // }

    for (var i = 0 ; i < this.childrenEntities.length ; i++) 
    {
        diff = this.childrenEntities[i].getPosition().sub(resultEntity.getPosition());

        var tmpChild = this.childrenEntities[i];
        var curDistance = diff.lengthSq();
        if (curDistance < closestDistance && tmpChild != resultEntity && this.previousEntity != tmpChild) 
        {
            closestEntity = tmpChild;
            closestDistance = curDistance;
        }
    }

    this.previousEntity = closestEntity.clone();

    return closestEntity;
};


box3_1 is the game object I click on in my game
and it’s also a previous box which is closestEntity
as you can see we compare it (box3_1) itself (it’s also a child in this case)
in this case we shouldn’t call if-statement because it didn’t get all requirements but it goes into if-statement I don’t know why!


also it shows different values in the hierarchy and different values in debug inspector
it’s the same object!

image
even using js epsilon won’t get any result

var isVecResultEqual = isEqual(tmpChildPos.x, tmpResultEntityPos.x);
        var isVecPreviousEqual = isEqual(this.previousEntity.x, tmpChildPos.x);
        
        if (curDistance < closestDistance && isVecResultEqual != true && !isVecPreviousEqual) 
        {
            closestEntity = this.childrenEntities[i];
            closestDistance = curDistance;
        }
    }


    this.previousEntity = closestEntity.getPosition().clone();

isVecResultEqual is true
isVecPreviousEqual is false
I have written in my if-statement isVecResultEqual != true
and still this code goes into if-statement and chooses wrong cube…(the previous one:D)
this is pure madness

image
image
lmao

Sometimes !== Instead of != Does a huge difference

I’ve tried it
bug still occurs

I haven’t dug too deeply into this, but the first issue I see is here:

    for (var i = 0 ; i < this.childrenEntities.length ; i++) 
    {
        diff = this.childrenEntities[i].getPosition().sub(resultEntity.getPosition());

        var tmpChild = this.childrenEntities[i];
        var curDistance = diff.lengthSq();
        if (curDistance < closestDistance && tmpChild != resultEntity && this.previousEntity != tmpChild) 
        {
            closestEntity = tmpChild;
            closestDistance = curDistance;
        }
    }

    this.previousEntity = closestEntity.clone();

    return closestEntity;

In regards to this comparison:

 this.previousEntity != tmpChil

This checks if both this.previousEntity and tmpChil are not referencing the same object in memory.

However, you clone the entity for some reason here:

this.previousEntity = closestEntity.clone()

This creates a completely new entity so this.previousEntity and tmpChil are never going to reference the same object in memory. (You are also constantly cloning the entities every time you do this check!)

Looks like you meant to do this instead:

this.previousEntity = closestEntity;

So this.previousEntity is referencing the entity and not a clone of it.

I’m already using this code

this.previousEntity = closestEntity;

in my newest script but that didn’t fix the bug

In which case, you will be best off sharing a project for people to look at and steps on how to reproduce the issue.

https://playcanvas.com/editor/project/937185
like this? or how do I share the project

On mobile so can’t confirm that’s the only issue but on line 198 of the DrawBeizerCurve script, you sre still cloning the entity.

Change it to:

closestEntity = tmpChild;

For the same reason as above as my last post. You want a reference to the entity in the scene. You don’t want to clone it.

I was not able to continue working on the code today and only now got some free time
so
I changed this line as well, but the problem still persists

Not entirely sure what the expected result here should be.

Is the issue that it sometimes goes in a straight line like this and it should be a curve instead?

Is this what you were expecting? https://playcanvas.com/project/939045/overview/f-technical-assignment

If so, the issue was that this.previousEntity was referencing the middle point, not the previous entity that was clicked on.

1 Like

Oh my god, hahaha
I probably should have slept more:DDD
thanks for fixing the problem!