{"author_link":"\/users\/flying-dog-fish","author_name":"Flying Dog Fish","author_uid":"flying-dog-fish","comments":[],"epoch":1713817176,"event":"LD55","format":"md","ldjam_node_id":394523,"likes":14,"metadata":{"p_key":"206577","p_author":"Flying Dog Fish","p_authorkey":"1122902","p_urlkey":"443111","p_title":"How erroneus c# math cost me a couple of hours and nerve cells","p_cat":"LDJam ","p_event":"LD55","p_time":"1713817176","p_likes":"14","p_comments":"0","p_status":"WAYBACK","us_key":"1122902","us_name":"Flying Dog Fish","us_username":"flying-dog-fish","event_start":"1712966400","event_key":"120","event_name":"Ludum Dare 55"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD55","removed_author":false},"_superparent":383062,"_trust":10,"author":122902,"body":"The last thing you want in a jam is to struggle with your language of choice, but that is exactly what happened to me.\n\nIn my custom rectangular grid world checking logic I've got this piece of code to calculate the position and type of field in rectangle grid into which an enemy is moving:\n```\npublic (int, int, FieldEnum) getGridPositionAndType(Vector2 position, bool up)\n    {\n        float fj = (position.y - transform.position.y + wholeHeight \/ 2 - wallSize \/ 2) \/ wallSize;\n        int j;\n        if (up)\n            j = Mathf.FloorToInt(fj);\n        else\n            j = Mathf.CeilToInt(fj);\n        float fi = (position.x - transform.position.x + wholeWidth \/ 2 - wallSize \/ 2) \/ wallSize;\n        int i;\n        if (up)\n            i = Mathf.FloorToInt(fi);\n        else\n            i = Mathf.CeilToInt(fi);\n        if (i < 0) i = 0;\n        if (j < 0) j = 0;\n        if (i >= gridWidth) i = gridWidth - 1;\n        if (j >= gridHeight) j = gridHeight - 1;\n        return (i, j, GetFieldType(pixelArray[j * gridWidth + i]));\n    }\n```\nFor some inexplicable reason the enemies on the left and bottom sides of the screen were going berserk. I lost some precious hours of my compo development time and was going crazy until I realized the problem was with negative numbers. So I tried adding a large number before doing the operation and subtracting it later:\n```\npublic (int, int, FieldEnum) getGridPositionAndType(Vector2 position, bool up)\n    {\n        float fj = (position.y - transform.position.y + wholeHeight \/ 2 - wallSize \/ 2) \/ wallSize;\n        int j;\n        if (up)\n            j = Mathf.FloorToInt(fj+30f)-30; \/\/ force positive math\n        else\n            j = Mathf.CeilToInt(fj+30f)-30; \/\/ force positive math\n        float fi = (position.x - transform.position.x + wholeWidth \/ 2 - wallSize \/ 2) \/ wallSize;\n        int i;\n        if (up)\n            i = Mathf.FloorToInt(fi+30f)-30; \/\/ force positive math\n        else\n            i = Mathf.CeilToInt(fi+30f)-30; \/\/ force positive math\n        if (i < 0) i = 0;\n        if (j < 0) j = 0;\n        if (i >= gridWidth) i = gridWidth - 1;\n        if (j >= gridHeight) j = gridHeight - 1;\n        return (i, j, GetFieldType(pixelArray[j * gridWidth + i]));\n    }\n```\nIt worked like a charm. Then I remembered that most programming languages including c++ and C# are doing modulo operations wrong. Instead of doing proper euclidean map they take the absolute value, calculate modulo on that, and return the sign.\n![image_2024_04_13T21_11_54_833Z.png](\/\/\/raw\/610\/e1\/z\/64b2d.png) \nThe same goes for rounding off to the nearest higher or lower number (ceiling and floor operation). \n\nI learned this lesson the hard way so I decided to write this up in case it helps anyone.\n","comments":2,"comments-timestamp":"2024-04-22T21:53:11Z","created":"2024-04-22T19:45:34Z","files":[],"files-timestamp":0,"id":394523,"love":14,"love-timestamp":"2024-04-25T13:49:29Z","meta":[],"modified":"2024-04-25T13:49:29Z","name":"How erroneus c# math cost me a couple of hours and nerve cells","node-timestamp":"2024-04-22T20:19:36Z","parent":384581,"parents":[1,5,9,383062,384581],"path":"\/events\/ludum-dare\/55\/cthulhu-defense\/how-erroneus-c-math-cost-me-a-couple-of-hours-and-nerve-cells","published":"2024-04-22T20:19:36Z","scope":"public","slug":"how-erroneus-c-math-cost-me-a-couple-of-hours-and-nerve-cells","subsubtype":"","subtype":"","type":"post","version":1235949},"node_metadata":{"n_key":"394523","n_urlkey":"443111","n_parent":"384581","n_path":"\/events\/ludum-dare\/55\/cthulhu-defense\/how-erroneus-c-math-cost-me-a-couple-of-hours-and-nerve-cells","n_slug":"how-erroneus-c-math-cost-me-a-co","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"122902","n_created":"1713815134","n_modified":"1714052969","n_version":"1235949","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/55\/cthulhu-defense\/how-erroneus-c-math-cost-me-a-couple-of-hours-and-nerve-cells","text":"The last thing you want in a jam is to struggle with your language of choice, but that is exactly what happened to me.\n\nIn my custom rectangular grid world checking logic I've got this piece of code to calculate the position and type of field in rectangle grid into which an enemy is moving:\n```\npublic (int, int, FieldEnum) getGridPositionAndType(Vector2 position, bool up)\n    {\n        float fj = (position.y - transform.position.y + wholeHeight \/ 2 - wallSize \/ 2) \/ wallSize;\n        int j;\n        if (up)\n            j = Mathf.FloorToInt(fj);\n        else\n            j = Mathf.CeilToInt(fj);\n        float fi = (position.x - transform.position.x + wholeWidth \/ 2 - wallSize \/ 2) \/ wallSize;\n        int i;\n        if (up)\n            i = Mathf.FloorToInt(fi);\n        else\n            i = Mathf.CeilToInt(fi);\n        if (i < 0) i = 0;\n        if (j < 0) j = 0;\n        if (i >= gridWidth) i = gridWidth - 1;\n        if (j >= gridHeight) j = gridHeight - 1;\n        return (i, j, GetFieldType(pixelArray[j * gridWidth + i]));\n    }\n```\nFor some inexplicable reason the enemies on the left and bottom sides of the screen were going berserk. I lost some precious hours of my compo development time and was going crazy until I realized the problem was with negative numbers. So I tried adding a large number before doing the operation and subtracting it later:\n```\npublic (int, int, FieldEnum) getGridPositionAndType(Vector2 position, bool up)\n    {\n        float fj = (position.y - transform.position.y + wholeHeight \/ 2 - wallSize \/ 2) \/ wallSize;\n        int j;\n        if (up)\n            j = Mathf.FloorToInt(fj+30f)-30; \/\/ force positive math\n        else\n            j = Mathf.CeilToInt(fj+30f)-30; \/\/ force positive math\n        float fi = (position.x - transform.position.x + wholeWidth \/ 2 - wallSize \/ 2) \/ wallSize;\n        int i;\n        if (up)\n            i = Mathf.FloorToInt(fi+30f)-30; \/\/ force positive math\n        else\n            i = Mathf.CeilToInt(fi+30f)-30; \/\/ force positive math\n        if (i < 0) i = 0;\n        if (j < 0) j = 0;\n        if (i >= gridWidth) i = gridWidth - 1;\n        if (j >= gridHeight) j = gridHeight - 1;\n        return (i, j, GetFieldType(pixelArray[j * gridWidth + i]));\n    }\n```\nIt worked like a charm. Then I remembered that most programming languages including c++ and C# are doing modulo operations wrong. Instead of doing proper euclidean map they take the absolute value, calculate modulo on that, and return the sign.\n![image_2024_04_13T21_11_54_833Z.png](\/\/\/raw\/610\/e1\/z\/64b2d.png) \nThe same goes for rounding off to the nearest higher or lower number (ceiling and floor operation). \n\nI learned this lesson the hard way so I decided to write this up in case it helps anyone.\n","title":"How erroneus c# math cost me a couple of hours and nerve cells","wayback_source":[]}