-3

I am currently working on a solar system physics simulator in the JavaScript canvas. I have a object named "earthState" with some info that should be used by the physics system to create earth's orbit. I have attributed a numerical value to each of the keys in the object, but when I log the object to the console it says that all values are NaN.

"earthState":

    let earthState = {
        distance: {
            value: 1.496 * Math.pow(10, 11),
            speed: 0.00
        },
        angle: {
            value: Math.PI/2,
            speed: 1.990986 * Math.pow(10, -7)
        }
    };

    console.log(earthState);

I have the object logging when the page first loads, and I have stopped the page before it finishes loading and I get the correct values in the console:

{distance: {…}, angle: {…}}
  angle: {value: 1.5707963267948966, speed: 1.9909859999999998e-7}
  distance: {value: 149600000000, speed: 0}
  __proto_: Object

But, as soon as I let the page finish loading, I get NaN as all the values:

{distance: {…}, angle: {…}}
  angle: {value: NaN, speed: NaN}
  distance: {value: NaN, speed: NaN}
  __proto_: Object

The only thing that changes when the page loads is that an animation loop starts. There are no variables that should change anything in the "earthState" object. I have had a hard time trying to figure out the problem. It may be something simple that I am not aware of. Please let me know if this is the case, and if there is anything I should change to fix this problem. Thank you in advance!

  • Are you using P5js? Also post your whole code, so we can help you better. Thanks! – Esdras Xavier Sep 03 '19 at 12:53
  • You need to have some other code that causing this for instance animation code that do something like `disatance.value *= 10` and value is `"10px"`. Without looking at your actual code nothing we can do. – jcubic Sep 03 '19 at 12:53
  • 2
    Just FYI `1.496 * Math.pow(10, 11)` can be written as `1.496e11`. – Pointy Sep 03 '19 at 12:54
  • The "animation loop" code is clearly what's causing the problem. Without seeing that code, it's unlikely that anyone can help. – Pointy Sep 03 '19 at 12:55
  • 1
    Is the object and the display in the same scope? Try replacing let with var and see if you get the same problem. – SPlatten Sep 03 '19 at 12:56
  • 4
    "*There are no variables that should change anything in the "earthState" object.*" - well, but apparently there are. Use a debugger to find them, see [here](https://stackoverflow.com/q/11618278/1048572) or [there](https://developers.google.com/web/tools/chrome-devtools/javascript/reference#watch) – Bergi Sep 03 '19 at 12:56
  • @Pointy Thank you, I wasn't aware :D – Saahil Khatkhate Sep 03 '19 at 13:16

1 Answers1

0

Thank you all for your comments and ideas! My friend has helped me debug and find the error. I was simply using the "earthState" object instead of the "sunState" object in one of the functions. Cheers!