quadtree, pancam

This commit is contained in:
Pascal Engélibert 2023-12-25 21:12:51 +01:00
parent e494d3992c
commit 8fc0c391ca
4 changed files with 366 additions and 125 deletions

221
Cargo.lock generated
View file

@ -233,7 +233,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ca33f4bc4ed1babef42cad36cc1f51fa88be00420404e5b1e80ab1b18f7678c"
dependencies = [
"concurrent-queue",
"event-listener 4.0.0",
"event-listener 4.0.1",
"event-listener-strategy",
"futures-core",
"pin-project-lite",
@ -280,16 +280,16 @@ version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7125e42787d53db9dd54261812ef17e937c95a51e4d291373b670342fa44310c"
dependencies = [
"event-listener 4.0.0",
"event-listener 4.0.1",
"event-listener-strategy",
"pin-project-lite",
]
[[package]]
name = "async-task"
version = "4.5.0"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4eb2cdb97421e01129ccb49169d8279ed21e829929144f4a22a6e54ac549ca1"
checksum = "e1d90cd0b264dfdd8eb5bad0a2c217c1f88fa96a8573f40e7b12de23fb468f46"
[[package]]
name = "atomic-waker"
@ -339,6 +339,15 @@ dependencies = [
"bevy_internal",
]
[[package]]
name = "bevy-fps-counter"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "999a4718c50c5d7172b4a0a8e2a2597925d91fe10e086514d5d6f664eb802075"
dependencies = [
"bevy",
]
[[package]]
name = "bevy_a11y"
version = "0.12.1"
@ -427,7 +436,7 @@ dependencies = [
"bevy_macro_utils",
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -493,7 +502,7 @@ checksum = "f484318350462c58ba3942a45a656c1fd6b6e484a6b6b7abc3a787ad1a51e500"
dependencies = [
"bevy_macro_utils",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -541,7 +550,7 @@ dependencies = [
"bevy_macro_utils",
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -713,7 +722,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustc-hash",
"syn 2.0.41",
"syn 2.0.43",
"toml_edit 0.20.7",
]
@ -736,6 +745,15 @@ dependencies = [
"glam",
]
[[package]]
name = "bevy_pancam"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0fc188c00741b4e7736cf1ba8e394abce7daf479284f5ca551408d36147c7bd"
dependencies = [
"bevy",
]
[[package]]
name = "bevy_pbr"
version = "0.12.1"
@ -796,7 +814,7 @@ dependencies = [
"bevy_macro_utils",
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
"uuid",
]
@ -855,7 +873,7 @@ dependencies = [
"bevy_macro_utils",
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -1025,7 +1043,7 @@ checksum = "7aafecc952b6b8eb1a93c12590bd867d25df2f4ae1033a01dfdfc3c35ebccfff"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -1086,7 +1104,7 @@ dependencies = [
"regex",
"rustc-hash",
"shlex",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -1196,7 +1214,7 @@ checksum = "965ab7eb5f8f97d2a083c799f3a1b994fc397b2fe2da5d1da1626ce15a39f2b1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -1211,6 +1229,12 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
[[package]]
name = "cap"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f125eb85b84a24c36b02ed1d22c9dd8632f53b3cde6e4d23512f94021030003"
[[package]]
name = "cc"
version = "1.0.83"
@ -1433,9 +1457,9 @@ dependencies = [
[[package]]
name = "crossbeam-channel"
version = "0.5.9"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c3242926edf34aec4ac3a77108ad4854bffaa2e4ddc1824124ce59231302d5"
checksum = "82a9b73a36529d9c47029b9fb3a6f0ea3cc916a261195352ba19e770fc1748b2"
dependencies = [
"cfg-if",
"crossbeam-utils",
@ -1443,9 +1467,9 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.17"
version = "0.8.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d96137f14f244c37f989d9fff8f95e6c18b918e71f36638f8c49112e4c78f"
checksum = "c3a430a770ebd84726f584a90ee7f020d28db52c6d02138900f22341f866d39c"
dependencies = [
"cfg-if",
]
@ -1514,7 +1538,7 @@ checksum = "3fe2568f851fd6144a45fa91cfed8fe5ca8fc0b56ba6797bfc1ed2771b90e37c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -1549,9 +1573,9 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "event-listener"
version = "4.0.0"
version = "4.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "770d968249b5d99410d61f5bf89057f3199a077a04d087092f58e7d10692baae"
checksum = "84f2cdcf274580f2d63697192d744727b3198894b1bf02923643bf59e2c26712"
dependencies = [
"concurrent-queue",
"parking",
@ -1564,7 +1588,7 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "958e4d70b6d5e81971bebec42271ec641e7ff4e170a6fa605f2b8a8b65cb97d3"
dependencies = [
"event-listener 4.0.0",
"event-listener 4.0.1",
"pin-project-lite",
]
@ -1641,7 +1665,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -1658,15 +1682,15 @@ checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
[[package]]
name = "futures-core"
version = "0.3.29"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-io"
version = "0.3.29"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
[[package]]
name = "futures-lite"
@ -1724,9 +1748,9 @@ dependencies = [
[[package]]
name = "gilrs-core"
version = "0.5.9"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "178769da179a47b187837d1ab2b5b9b684a21180166a77a4ca37e7e58ee3833d"
checksum = "0af1827b7dd2f36d740ae804c1b3ea0d64c12533fb61ff91883005143a0e8c5a"
dependencies = [
"core-foundation",
"inotify",
@ -1740,7 +1764,7 @@ dependencies = [
"vec_map",
"wasm-bindgen",
"web-sys",
"windows 0.51.1",
"windows 0.52.0",
]
[[package]]
@ -1779,32 +1803,33 @@ dependencies = [
[[package]]
name = "gltf"
version = "1.3.0"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad2dcfb6dd7a66f9eb3d181a29dcfb22d146b0bcdc2e1ed1713cbf03939a88ea"
checksum = "3b78f069cf941075835822953c345b9e1edd67ae347b81ace3aea9de38c2ef33"
dependencies = [
"byteorder",
"gltf-json",
"lazy_static",
"serde_json",
]
[[package]]
name = "gltf-derive"
version = "1.3.0"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2cbcea5dd47e7ad4e9ee6f040384fcd7204bbf671aa4f9e7ca7dfc9bfa1de20"
checksum = "438ffe1a5540d75403feaf23636b164e816e93f6f03131674722b3886ce32a57"
dependencies = [
"inflections",
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
name = "gltf-json"
version = "1.3.0"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d5b810806b78dde4b71a95cc0e6fdcab34c4c617da3574df166f9987be97d03"
checksum = "655951ba557f2bc69ea4b0799446bae281fa78efae6319968bdd2c3e9a06d8e1"
dependencies = [
"gltf-derive",
"serde",
@ -2084,6 +2109,9 @@ name = "jsb-gravity"
version = "0.1.0"
dependencies = [
"bevy",
"bevy-fps-counter",
"bevy_pancam",
"cap",
"opensimplex_noise_rs",
"rand",
]
@ -2196,9 +2224,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "mach2"
version = "0.4.1"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d0d1830bcd151a6fc4aea1369af235b36c1528fe976b8ff678683c9995eade8"
checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709"
dependencies = [
"libc",
]
@ -2477,7 +2505,7 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -2527,9 +2555,9 @@ dependencies = [
[[package]]
name = "object"
version = "0.32.1"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
@ -2678,9 +2706,9 @@ dependencies = [
[[package]]
name = "pkg-config"
version = "0.3.27"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a"
[[package]]
name = "png"
@ -2722,18 +2750,18 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.70"
version = "1.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "profiling"
version = "1.0.12"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1de09527cd2ea2c2d59fb6c2f8c1ab8c71709ed9d1b6d60b0e1c9fbb6fdcb33c"
checksum = "d135ede8821cf6376eb7a64148901e1690b788c11ae94dc297ae917dbc91dc0e"
[[package]]
name = "quote"
@ -2955,7 +2983,7 @@ checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -3061,9 +3089,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.41"
version = "2.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269"
checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53"
dependencies = [
"proc-macro2",
"quote",
@ -3131,7 +3159,7 @@ checksum = "e4c60d69f36615a077cc7663b9cb8e42275722d23e58a7fa3d2c7f2915d09d04"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -3142,7 +3170,7 @@ checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -3217,7 +3245,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]
[[package]]
@ -3386,7 +3414,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
"wasm-bindgen-shared",
]
@ -3420,7 +3448,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -3619,21 +3647,21 @@ dependencies = [
[[package]]
name = "windows"
version = "0.51.1"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9"
checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be"
dependencies = [
"windows-core",
"windows-targets 0.48.5",
"windows-targets 0.52.0",
]
[[package]]
name = "windows-core"
version = "0.51.1"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.48.5",
"windows-targets 0.52.0",
]
[[package]]
@ -3706,6 +3734,21 @@ dependencies = [
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
@ -3718,6 +3761,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
@ -3730,6 +3779,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
@ -3742,6 +3797,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
@ -3754,6 +3815,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
@ -3766,6 +3833,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
@ -3778,6 +3851,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
@ -3790,6 +3869,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "winit"
version = "0.28.7"
@ -3822,9 +3907,9 @@ dependencies = [
[[package]]
name = "winnow"
version = "0.5.28"
version = "0.5.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c830786f7720c2fd27a1a0e27a709dbd3c4d009b56d098fc742d4f4eab91fe2"
checksum = "9b5c3db89721d50d0e2a673f5043fc4722f76dcc352d7b1ab8b8288bed4ed2c5"
dependencies = [
"memchr",
]
@ -3854,20 +3939,20 @@ checksum = "0fcb9cbac069e033553e8bb871be2fbdffcab578eb25bd0f7c508cedc6dcd75a"
[[package]]
name = "zerocopy"
version = "0.7.31"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c4061bedbb353041c12f413700357bec76df2c7e2ca8e4df8bac24c6bf68e3d"
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.31"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3c129550b3e6de3fd0ba67ba5c81818f9805e58b8d7fee80a3a59d2c9fc601a"
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.41",
"syn 2.0.43",
]

View file

@ -5,5 +5,8 @@ edition = "2021"
[dependencies]
bevy = "0.12.1"
bevy_pancam = "0.10.0"
bevy-fps-counter = "0.3.0"
cap = "0.1.2"
opensimplex_noise_rs = "0.3.0"
rand = "0.8.5"

View file

@ -3,9 +3,19 @@ mod quadtree;
use bevy::{ecs::query::BatchingStrategy, prelude::*, sprite::MaterialMesh2dBundle};
#[global_allocator]
static ALLOCATOR: cap::Cap<std::alloc::System> =
cap::Cap::new(std::alloc::System, 1024 * 1024 * 1024);
static UNIVERSE_POS: (Vec2, Vec2) = (Vec2::new(-1e6, -1e6), Vec2::new(1e6, 1e6));
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins((
DefaultPlugins,
bevy_fps_counter::FpsCounterPlugin,
bevy_pancam::PanCamPlugin,
))
.insert_resource(Constants { g: 6.674e-11 })
.add_systems(Startup, setup)
.configure_sets(Update, (Set::Force, Set::Apply).chain())
@ -24,14 +34,13 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(Camera2dBundle {
camera: Camera { ..default() },
..default()
});
commands
.spawn(Camera2dBundle::default())
.insert(bevy_pancam::PanCam::default());
commands.spawn(Planet {
mass: Mass(1.988e18),
speed: Speed { x: 0., y: 0. },
speed: Speed(Vec2::new(0.0, 0.0)),
mesh: MaterialMesh2dBundle {
mesh: meshes.add(gen::planet()).into(),
material: materials.add(ColorMaterial::from(Color::YELLOW)),
@ -41,7 +50,7 @@ fn setup(
});
commands.spawn(Planet {
mass: Mass(5.9736e14),
speed: Speed { x: 0., y: 500. },
speed: Speed(Vec2::new(0.0, 500.0)),
mesh: MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(10.).into()).into(),
material: materials.add(ColorMaterial::from(Color::BLUE)),
@ -51,7 +60,7 @@ fn setup(
});
commands.spawn(Planet {
mass: Mass(5.9736e14),
speed: Speed { x: 0., y: -500. },
speed: Speed(Vec2::new(0.0, -500.0)),
mesh: MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(10.).into()).into(),
material: materials.add(ColorMaterial::from(Color::BLUE)),
@ -59,10 +68,10 @@ fn setup(
..default()
},
});
for i in 0..100u32 {
for i in 0..4000u32 {
commands.spawn(Planet {
mass: Mass(1.),
speed: Speed { x: 0., y: -500. },
speed: Speed(Vec2::new(0.0, -500.0)),
mesh: MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(5.).into()).into(),
material: materials.add(ColorMaterial::from(Color::RED)),
@ -80,10 +89,7 @@ enum Set {
}
#[derive(Component)]
struct Speed {
x: f32,
y: f32,
}
struct Speed(Vec2);
#[derive(Component)]
struct Mass(f32);
@ -100,6 +106,23 @@ struct Constants {
g: f32,
}
struct Body {
mass: f32,
pos: Vec2,
}
impl quadtree::Body for Body {
fn mass(&self) -> f32 {
self.mass
}
fn pos(&self) -> Vec2 {
self.pos
}
fn add_mass(&mut self, mass: f32) {
self.mass += mass;
}
}
/*fn weight_system(
constants: Res<Constants>,
query1: Query<(&Transform, &Mass)>,
@ -126,19 +149,30 @@ fn weight_system(
mut query: Query<(&Transform, &Mass, &mut Speed)>,
time: Res<Time>,
) {
let mut tree = quadtree::Node::new(UNIVERSE_POS);
let gdt = constants.g * time.delta_seconds();
let mut iter = query.iter_combinations_mut();
while let Some([(n1_pos, n1_mass, mut n1_speed), (n2_pos, n2_mass, mut n2_speed)]) =
iter.fetch_next()
{
let d2 = (n1_pos.translation.x - n2_pos.translation.x).powi(2)
+ (n1_pos.translation.y - n2_pos.translation.y).powi(2);
let f = gdt / (d2 * d2.sqrt());
n1_speed.x -= (n1_pos.translation.x - n2_pos.translation.x) * f * n2_mass.0;
n1_speed.y -= (n1_pos.translation.y - n2_pos.translation.y) * f * n2_mass.0;
n2_speed.x += (n1_pos.translation.x - n2_pos.translation.x) * f * n1_mass.0;
n2_speed.y += (n1_pos.translation.y - n2_pos.translation.y) * f * n1_mass.0;
for (pos, mass, _speed) in query.iter() {
tree.add_body(Body {
mass: mass.0,
pos: pos.translation.xy(),
});
}
query.par_iter_mut().for_each(|(pos, _mass, mut speed)| {
speed.0 += gdt * tree.apply(pos.translation.xy(), 0.5);
});
// let gdt = constants.g * time.delta_seconds();
// let mut iter = query.iter_combinations_mut();
// while let Some([(n1_pos, n1_mass, mut n1_speed), (n2_pos, n2_mass, mut n2_speed)]) =
// iter.fetch_next()
// {
// let d2 = (n1_pos.translation.x - n2_pos.translation.x).powi(2)
// + (n1_pos.translation.y - n2_pos.translation.y).powi(2);
// let f = gdt / (d2 * d2.sqrt());
// n1_speed.x -= (n1_pos.translation.x - n2_pos.translation.x) * f * n2_mass.0;
// n1_speed.y -= (n1_pos.translation.y - n2_pos.translation.y) * f * n2_mass.0;
// n2_speed.x += (n1_pos.translation.x - n2_pos.translation.x) * f * n1_mass.0;
// n2_speed.y += (n1_pos.translation.y - n2_pos.translation.y) * f * n1_mass.0;
// }
}
fn apply_system(mut query: Query<(&mut Transform, &Speed)>, time: Res<Time>) {
@ -147,7 +181,7 @@ fn apply_system(mut query: Query<(&mut Transform, &Speed)>, time: Res<Time>) {
.par_iter_mut()
.batching_strategy(BatchingStrategy::fixed(128))
.for_each(|(mut pos, speed)| {
pos.translation.x += speed.x * dt;
pos.translation.y += speed.y * dt;
pos.translation.x += speed.0.x * dt;
pos.translation.y += speed.0.y * dt;
});
}

View file

@ -2,40 +2,159 @@ use bevy::prelude::*;
pub trait Body {
fn mass(&self) -> f32;
fn center_of_mass(&self) -> (f32, Vec2);
fn pos(&self) -> Vec2;
fn add_mass(&mut self, mass: f32);
}
pub enum Node<L: Body> {
Node([Node; 4]),
Leaf(Vec<L>),
}
impl<L: Body> Body for Node<L> {
fn mass(&self) -> f32 {
match self {
Node::Node([n1, n2, n3, n4]) => n0.mass() + n1.mass() + n2.mass() + n3.mass(),
Node::Leaf(v) => v.iter().map(Body::mass).sum(),
}
}
fn center_of_mass(&self) {
match self {
Node::Node([n1, n2, n3, n4]) => n0.mass() + n1.mass() + n2.mass() + n3.mass(),
Node::Leaf(v) => {
let mut mass = 0.0;
let mut center_of_mass = Vec2::zero();
for(n in v) {
let (n_mass, n_center) = n.center_of_mass();
mass += n_mass;
center_of_mass += mass * n_center;
}
(mass, center_of_mass / mass)
},
}
}
pub enum Node<L> {
Branch {
nodes: Box<[Node<L>; 4]>,
center: Vec2,
mass: f32,
center_of_mass: Vec2,
width: f32,
},
Leaf {
body: Option<L>,
pos: (Vec2, Vec2),
},
}
impl<L: Body> Node<L> {
fn add_body(&mut self, body: L) {
pub fn new(pos: (Vec2, Vec2)) -> Self {
Node::Leaf { body: None, pos }
// let center = (pos.1 - pos.0) / 2.0;
// Node::Branch {
// nodes: [
// Box::new(Node::Leaf {
// body: None,
// pos: (pos.0, center),
// }),
// Box::new(Node::Leaf {
// body: None,
// pos: (Vec2::new(center.x, pos.0.y), Vec2::new(pos.1.x, center.y)),
// }),
// Box::new(Node::Leaf {
// body: None,
// pos: (Vec2::new(pos.0.x, center.y), Vec2::new(center.x, pos.1.y)),
// }),
// Box::new(Node::Leaf {
// body: None,
// pos: (center, pos.1),
// }),
// ],
// center,
// mass: 0.0,
// center_of_mass: center,
// width: pos.1.x - pos.0.x,
// }
}
pub fn add_body(&mut self, new_body: L) {
match self {
Node::Branch {
nodes,
center,
mass,
center_of_mass,
..
} => {
let new_body_pos = new_body.pos();
let new_body_mass = new_body.mass();
*center_of_mass = (*center_of_mass * *mass + new_body_mass * new_body_pos)
/ (*mass + new_body_mass);
*mass += new_body_mass;
nodes[if new_body_pos.x < center.x {
if new_body_pos.y < center.y {
0
} else {
2
}
} else {
if new_body_pos.y < center.y {
1
} else {
3
}
}]
.add_body(new_body)
}
Node::Leaf { body, pos } => {
if let Some(mut body) = body.take() {
if body.pos().distance_squared(new_body.pos()) < 1.0 {
body.add_mass(new_body.mass());
*self = Node::Leaf {
body: Some(body),
pos: *pos,
};
return;
}
let center = (pos.0 + pos.1) / 2.0;
*self = Node::Branch {
nodes: Box::new([
Node::Leaf {
body: None,
pos: (pos.0, center),
},
Node::Leaf {
body: None,
pos: (Vec2::new(center.x, pos.0.y), Vec2::new(pos.1.x, center.y)),
},
Node::Leaf {
body: None,
pos: (Vec2::new(pos.0.x, center.y), Vec2::new(center.x, pos.1.y)),
},
Node::Leaf {
body: None,
pos: (center, pos.1),
},
]),
center,
mass: 0.0,
center_of_mass: center,
width: pos.1.x - pos.0.x,
};
self.add_body(body);
self.add_body(new_body)
} else {
*body = Some(new_body);
}
}
}
}
pub fn apply(&self, on: Vec2, theta: f32) -> Vec2 {
match self {
Node::Branch {
nodes,
mass,
center_of_mass,
width,
..
} => {
if on == *center_of_mass {
return Vec2::ZERO;
}
let dist = on.distance(*center_of_mass);
if width / dist < theta {
*mass * (*center_of_mass - on) / (dist * dist * dist)
} else {
nodes[0].apply(on, theta)
+ nodes[1].apply(on, theta)
+ nodes[2].apply(on, theta)
+ nodes[3].apply(on, theta)
}
}
Node::Leaf { body, .. } => {
if let Some(body) = body {
if on == body.pos() {
return Vec2::ZERO;
}
let dist = on.distance(body.pos());
body.mass() * (body.pos() - on) / (dist * dist * dist)
} else {
Vec2::ZERO
}
}
}
}
}