{"id":22928833,"url":"https://github.com/mbiushelix/cluster","last_synced_at":"2025-04-01T16:19:31.815Z","repository":{"id":175233306,"uuid":"502103484","full_name":"Mbiushelix/Cluster","owner":"Mbiushelix","description":"A small projects that simulates the gravitational forces between particles within a system.","archived":false,"fork":false,"pushed_at":"2022-12-13T20:46:13.000Z","size":190,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-07T10:32:21.355Z","etag":null,"topics":["math","physics","physics-simulation","unity2d"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mbiushelix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-10T16:03:27.000Z","updated_at":"2024-03-11T10:09:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"e31af3a1-20a7-4be2-bbd7-377b2bd76a40","html_url":"https://github.com/Mbiushelix/Cluster","commit_stats":null,"previous_names":["mbiushelix/cluster"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mbiushelix%2FCluster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mbiushelix%2FCluster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mbiushelix%2FCluster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mbiushelix%2FCluster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mbiushelix","download_url":"https://codeload.github.com/Mbiushelix/Cluster/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246668905,"owners_count":20814744,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["math","physics","physics-simulation","unity2d"],"created_at":"2024-12-14T09:27:57.436Z","updated_at":"2025-04-01T16:19:31.794Z","avatar_url":"https://github.com/Mbiushelix.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cluster\nAvailable on Simmer.io : https://simmer.io/@Yudhishtiran/cluster-2d \n\nThe most central formula in this project is:\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://latex.codecogs.com/svg.image?{\\color{Emerald}F_G\u0026space;=\u0026space;G\u0026space;\\cdot\u0026space;\\frac{m_1\\cdot\u0026space;m_2}{r^2}}\" title=\"https://latex.codecogs.com/svg.image?{\\color{Emerald}F_G = G \\cdot \\frac{m_1\\cdot m_2}{r^2}}\" /\u003e\n\u003c/p\u003e\n... which states that the gravitational force between two objects depends on both the objects' masses and is inverse proportional to the distance between the mass centers (CM).\n\n### The first algorithm (inefficient): \n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://latex.codecogs.com/svg.image?{\\color{Emerald}\\vec{F_i}=\\sum_{j=1}^n\\frac{Gm_im_j}{\\lvert\\vec{r_{ij}}\\rvert^3}\\vec{r_{ij}}\u0026space;\\thinspace\u0026space;,\u0026space;\\quad\u0026space;i\\in\u0026space;\\{1,2,3,\\ldots,n\u0026space;\\}\u0026space;}\" title=\"https://latex.codecogs.com/svg.image?{\\color{Emerald}\\vec{F_i}=\\sum_{j=1}^n\\frac{Gm_im_j}{\\lvert\\vec{r_{ij}}\\rvert^3}\\vec{r_{ij}} \\thinspace , \\quad i\\in \\{1,2,3,\\ldots,n \\} }\" /\u003e\n\u003c/p\u003e\nAccording to Newton’s law of gravitation, the gravitational force between two objects will be the same. However, the direction of the forces will of course be the opposite. As a result, this algorithm would therefore do almost double the strictly necessary calculations.\n\n\nIn C# with Unity: \n\n\n```C#\nfor (int i = 0; i \u003c particle_num; i++)\n        {\n            for (int j = 0; j \u003c particle_num; j++)\n            {\n                if (i != j)\n                {\n                    delta_force += GravitationalPull(particles[i], particles[j]);\n                }\n            }\n            \n            particles[i].AddForce(delta_force, ForceMode2D.Force);\n            delta_force = new Vector2(0, 0);\n        }\n\nVector2 GravitationalPull(Rigidbody2D primary_object, Rigidbody2D secondary_object)\n    {\n        distance = Vector2.Distance(primary_object.transform.position, secondary_object.transform.position);\n\n        if (distance \u003e 1)\n        {\n            Force = gravitational_constant / Mathf.Pow(distance, 3);\n            force_vector = Force * new Vector2(secondary_object.position.x - primary_object.position.x,\n            secondary_object.position.y - primary_object.position.y);\n        }\n\n        else\n        {\n            force_vector = new Vector2(0, 0);\n        }\n        \n        return force_vector;\n    }\n```\n\n### The second algorithm (much more efficient):\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://latex.codecogs.com/svg.image?{\\color{Emerald}\u0026space;\\vec{F_i}\u0026space;=\u0026space;\\begin{cases}\\vspace{0.1cm}\u0026space;\\sum_{j=1}^n\\frac{Gm_im_j}{\\lvert\\vec{r_{ij}}\\rvert^3}\\vec{r_{ij}}\u0026space;,\u0026\u0026space;i\u0026space;=\u0026space;1\u0026space;\\\\\u0026space;\\vspace{0.1cm}\u0026space;\\sum_{j=i\u0026plus;1}^n\\frac{Gm_im_j}{\\lvert\\vec{r_{ij}}\\rvert^3}\\vec{r_{ij}}\u0026space;-\u0026space;\\sum_{k=1}^{i-1}\\frac{Gm_km_i}{\\lvert\\vec{r_{ki}}\\rvert^3}\\vec{r_{ki}}\u0026space;\\thinspace\u0026space;,\u0026space;\u0026\u0026space;i\\geq\u0026space;2\u0026space;\\\\-\\sum_{k=1}^{i-1}\\frac{Gm_km_i}{\\lvert\\vec{r_{ki}}\\rvert^3}\\vec{r_{ki}}\\thinspace\u0026space;,\u0026space;\u0026\u0026space;i=n\\end{cases}}\" title=\"https://latex.codecogs.com/svg.image?{\\color{Emerald} \\vec{F_i} = \\begin{cases}\\vspace{0.1cm} \\sum_{j=1}^n\\frac{Gm_im_j}{\\lvert\\vec{r_{ij}}\\rvert^3}\\vec{r_{ij}} ,\u0026 i = 1 \\\\ \\vspace{0.1cm} \\sum_{j=i+1}^n\\frac{Gm_im_j}{\\lvert\\vec{r_{ij}}\\rvert^3}\\vec{r_{ij}} - \\sum_{k=1}^{i-1}\\frac{Gm_km_i}{\\lvert\\vec{r_{ki}}\\rvert^3}\\vec{r_{ki}} \\thinspace , \u0026 i\\geq 2 \\\\-\\sum_{k=1}^{i-1}\\frac{Gm_km_i}{\\lvert\\vec{r_{ki}}\\rvert^3}\\vec{r_{ki}}\\thinspace , \u0026 i=n\\end{cases}}\" /\u003e\n\u003c/p\u003e\n\nThis algorithm takes into account that Newton’s formula for the gravitational force infers the force vector of both objects. This reduces the number of calculations that are performed. See the formula over or the code below for more information. \n\n```C#\nfor (int i = 0; i \u003c particle_num; i++)\n        {\n            for (int j = i+1; j \u003c particle_num; j++)\n            {                  \n                forces[i] += GravitationalPull(particles[i], particles[j], j);          \n            }\n\n            particles[i].AddForce(forces[i], ForceMode2D.Force);\n            forces[i] = new Vector2(0, 0);\n        }\n\nVector2 GravitationalPull(Rigidbody2D primary_object, Rigidbody2D secondary_object,int j)\n    {\n        distance = Vector2.Distance(primary_object.transform.position, secondary_object.transform.position);\n\n        if (distance \u003e 1)\n        {\n            Force = (gravitational_constant*primary_object.mass*secondary_object.mass) / Mathf.Pow(distance, 3);\n            force_vector = Force  * new Vector2(secondary_object.position.x - primary_object.position.x,\n            secondary_object.position.y - primary_object.position.y);\n            forces[j] += -1*force_vector;\n        }\n\n        else\n        {\n            force_vector = new Vector2(0, 0);\n        }\n\n        return force_vector;\n    }\n```\n\n### How much better you may ask.\n\nWell, let ![equation](https://latex.codecogs.com/svg.image?{\\color{Emerald}E_f(n)}) be an estimate of calculation reduction. \n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://latex.codecogs.com/svg.image?{\\color{Emerald}E_f(n)\u0026space;=\u0026space;\\frac{\\binom{n}{2}}{n^2}\u0026space;=\u0026space;\\frac{n-1}{n^2}}\" title=\"https://latex.codecogs.com/svg.image?{\\color{Emerald}E_f(n) = \\frac{\\binom{n}{2}}{n^2} = \\frac{n-1}{n^2}}\" /\u003e\n\u003c/p\u003e\nThat means that if we have 250 particles to simulate, we will save about 50% computation (31 375 iterations reduced).\n\n\nThe image below illustrates the difference between these algorithms: \n\n![Cluster algorithms (1)](https://user-images.githubusercontent.com/81691774/173643502-d04ecb5e-cec5-419d-9bf7-0b3536edc1c4.png)\n\n\n\n\u003ciframe src=\"https://i.simmer.io/@Yudhishtiran/cluster\" style=\"width:900px;height:600px\"\u003e\u003c/iframe\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbiushelix%2Fcluster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmbiushelix%2Fcluster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbiushelix%2Fcluster/lists"}