# Object attributes randomly removed?

**URL:** <https://discourse.processing.org/t/object-attributes-randomly-removed/36214>\
**Category:** Coding Questions\
**Created:** [April 8, 2022, 9:00am UTC](https://discourse.processing.org/t/object-attributes-randomly-removed/36214 "2022-04-08T09:00:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PrincessLeia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/princessleia/32/16030_2.png) [@PrincessLeia](https://discourse.processing.org/u/PrincessLeia)\
**Post date:** [April 8, 2022, 9:00am UTC](https://discourse.processing.org/t/object-attributes-randomly-removed/36214/1 "2022-04-08T09:00:38Z")

</div>

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](https://editor.p5js.org/LeiaMyGirl/sketches/SQYOsDUWU)  
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…

---

<div class="post-metadata">

**Author:** ![pirela](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pirela](https://discourse.processing.org/u/pirela)\
**Post date:** [April 8, 2022, 7:51pm UTC](https://discourse.processing.org/t/object-attributes-randomly-removed/36214/2 "2022-04-08T19:51:22Z")

</div>

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 :  
vCollision:100  
distance:0  
CollisionNorm:NaN.

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.

HTH

---

<div class="post-metadata">

**Author:** ![PrincessLeia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/princessleia/32/16030_2.png) [@PrincessLeia](https://discourse.processing.org/u/PrincessLeia)\
**Post date:** [April 9, 2022, 10:28pm UTC](https://discourse.processing.org/t/object-attributes-randomly-removed/36214/3 "2022-04-09T22:28:14Z")

</div>

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