Object attributes randomly removed?

Hi, so I’ve been learning the maths involved with the calculations for the reflection velocity of objects hitting uneven or circular surfaces.
In this project: p5.js Web Editor
I have the problem where most of my objects attributes are randomly nullified after a certain amount of time.
Could anyone provide some help or feedback for this? Blown my mind trying to figure it out…

1 Like

I found your problem and will give you before some tips to debug next time:
i put a noLoop() after the print of circles in if(timeCount > timer) to have something readable.
I saw in the dump that object.dx and object.dy have a NaN (Not a Number) value . Ok found why this behavior.
To be able to repeat and repeat, i set a randomseed to have same run each time.
randomSeed(100) don’t give errors.
randomSeed(1000) was a good starting point, as i got NaN .
Next , put traces in the Circles loops like :
if (isNaN(obj1.dx)) console.log ("before dist dx NaN "+i+' '+r);
i found the first pair 23 36 that gives the NaN . Good step.

Next i put some trace within the different steps for this pair :

    vCollision = {x: obj2.x - obj1.x, y: obj2.y - obj1.y};
      if ((i==23)&& (r==36)) {console.log("vCollision:"+vCollision.x); }
      distance = sqrt((obj2.x-obj1.x)*(obj2.x-obj1.x) + (obj2.y-obj1.y)*(obj2.y-obj1.y));
      if ((i==23)&& (r==36)) { console.log("distance:"+distance);}
      vCollisionNorm = {x: vCollision.x / distance, y: vCollision.y / distance};
      if ((i==23)&& (r==36)) { console.log("vCollisionNorm:"+vCollisionNorm.x);}
      vRelativeVelocity = {x: obj1.dx - obj2.dx, y: obj1.dy - obj2.dy};
      if ((i==23)&& (r==36)) {console.log("vRelativeVelocity:"+vRelativeVelocity);}  

On the console, see :

Obvious: you don’t take care of a division by 0 in

{x: vCollision.x / distance, y: vCollision.y / distance};

and this error propagates itself in full other variables as an operation with a NaN result in a NaN.


1 Like

Thank-you! Great help and tips! I’ll take more care if I ever come across the same scenario in the future!