├── .github └── workflows │ ├── clippy.yml │ └── release.yml ├── .gitignore ├── CONTRIBUTING.md ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── examples ├── event_loop_example │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ └── main.rs └── raqote_example │ ├── Cargo.lock │ ├── Cargo.toml │ ├── shell.nix │ └── src │ └── main.rs ├── flake.lock ├── flake.nix ├── release.toml └── src ├── collision ├── mod.rs └── shape.rs ├── event_loop └── mod.rs ├── forces ├── force.rs ├── gravity.rs ├── mod.rs └── move.rs ├── formulas ├── dot.rs ├── elastic_collision.rs ├── mod.rs ├── pitagoras.rs ├── pow.rs └── sqrt.rs ├── lib.rs ├── obj ├── mod.rs └── obj_2d.rs └── vec ├── mod.rs └── vec_2d.rs /.github/workflows/clippy.yml: -------------------------------------------------------------------------------- 1 | name: Clippy pedantic check 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | clippy: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Checkout code 10 | uses: actions/checkout@v3 11 | - name: Setup Rust 12 | uses: actions-rs/toolchain@v1 13 | with: 14 | toolchain: stable 15 | override: true 16 | - name: Check Clippy 17 | run: cargo clippy -- -D clippy::pedantic 18 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release Crate 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | release: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v3 14 | - name: Setup Rust 15 | uses: actions-rs/toolchain@v1 16 | with: 17 | toolchain: stable 18 | override: true 19 | - name: Publish to crates.io 20 | uses: katyo/publish-crates@v2 21 | with: 22 | registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }} 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Motion 2 | 3 | ## Creating a Branch 4 | 5 | To contribute, please create your own branch following this pattern: -. This helps in keeping contributions organized and identifiable. After making your changes, contribute by making a pull request (PR) to the main branch. Ensure that your branch is up to date with main before creating a PR. 6 | 7 | 8 | ## Commit Messages 9 | 10 | Commit messages should follow commit lint rules, and be clear in their objective. Use simple present tense to describe your changes, e.g., "Add new collision detection algorithm." Avoid any kind of offensive language or inappropriate comments. Clear and respectful communication ensures a professional and collaborative environment. 11 | 12 | ## Documenting Features 13 | 14 | If you add new features, remember to document them thoroughly. Update relevant sections in the codebase, including `README.md`, and any other documentation files. This helps other developers understand the changes and how to utilize the new features effectively. 15 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "motion" 7 | version = "0.1.6" 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "motion" 3 | version = "0.1.6" 4 | edition = "2021" 5 | authors = ["Juanperias"] 6 | description = "A bare metal physics engine." 7 | license = "Apache-2.0" 8 | repository = "https://github.com/juanperias/motion" 9 | homepage = "https://github.com/juanperias/motion" 10 | documentation = "https://docs.rs/motion" 11 | readme = "README.md" 12 | 13 | [features] 14 | vec = [] 15 | formulas = [] 16 | obj = ["forces", "vec"] 17 | forces = [] 18 | event_loop = [] 19 | collision = ["obj"] 20 | default = ["vec", "formulas", "obj", "forces", "event_loop", "collision"] 21 | 22 | 23 | [dependencies] 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Deprecated, this project will probably be reincarnated in the form of another project that is somewhat better done 4 | # Motion🍃 5 | 6 | Motion is a **bare metal physics engine** with which you can make simulations easily and quickly, also in rust. 7 | 8 | ## Get started ✨ 9 | 10 | let's start by making a simple event loop 11 | 12 | ```rust 13 | use std::{thread, time::Duration}; 14 | 15 | use motion::event_loop::EventLoopBuilder; 16 | 17 | // The definition of this function depends on the context in which motion is used 18 | fn sleep(duration: Duration) { 19 | thread::sleep(duration); 20 | } 21 | 22 | fn main() { 23 | let el = EventLoopBuilder::new().fps(1).build(); 24 | 25 | el.start(|_config| println!("Hello! in the event loop"), sleep); 26 | } 27 | ``` 28 | 29 | now we are going to do something more complex by creating an object 30 | 31 | ```rust 32 | let obj = Object2dBuilder::new() 33 | .position(vec2(2.0, 2.0)) 34 | .density(2.0) 35 | .mass(3.0) 36 | .velocity(vec2(4.0, 4.0)) 37 | .acceleration(vec2(3.0, 3.0)) 38 | .radius(2.0) 39 | .shape(Shape::Circle) 40 | .build(); 41 | ``` 42 | 43 | ## Why rust 🦀 44 | 45 | Rust is a fast and efficient programming language, which makes it perfect for motion, plus it is very flexible allowing motion to be used everywhere. 46 | -------------------------------------------------------------------------------- /examples/event_loop_example/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "event_loop_example" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "motion", 10 | ] 11 | 12 | [[package]] 13 | name = "motion" 14 | version = "0.1.5" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "a683ed62b398f5b2860267545b1d3708e60d24a586d57c37100552400c27191f" 17 | -------------------------------------------------------------------------------- /examples/event_loop_example/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "event_loop_example" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | motion = "*" 8 | -------------------------------------------------------------------------------- /examples/event_loop_example/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::{thread, time::Duration}; 2 | 3 | use motion::event_loop::EventLoopBuilder; 4 | 5 | // The definition of this function depends on the context in which motion is used 6 | fn sleep(duration: Duration) { 7 | thread::sleep(duration); 8 | } 9 | 10 | fn main() { 11 | let el = EventLoopBuilder::new().fps(1).build(); 12 | 13 | el.start(|_config| println!("Hello! in the event loop"), sleep); 14 | } 15 | -------------------------------------------------------------------------------- /examples/raqote_example/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "arrayvec" 7 | version = "0.7.6" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" 10 | 11 | [[package]] 12 | name = "autocfg" 13 | version = "1.4.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 16 | 17 | [[package]] 18 | name = "bitflags" 19 | version = "1.3.2" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 22 | 23 | [[package]] 24 | name = "bitflags" 25 | version = "2.6.0" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" 28 | 29 | [[package]] 30 | name = "bumpalo" 31 | version = "3.16.0" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" 34 | 35 | [[package]] 36 | name = "cc" 37 | version = "1.1.31" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" 40 | dependencies = [ 41 | "shlex", 42 | ] 43 | 44 | [[package]] 45 | name = "cfg-if" 46 | version = "1.0.0" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 49 | 50 | [[package]] 51 | name = "dlib" 52 | version = "0.5.2" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412" 55 | dependencies = [ 56 | "libloading", 57 | ] 58 | 59 | [[package]] 60 | name = "downcast-rs" 61 | version = "1.2.1" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" 64 | 65 | [[package]] 66 | name = "errno" 67 | version = "0.3.9" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" 70 | dependencies = [ 71 | "libc", 72 | "windows-sys 0.52.0", 73 | ] 74 | 75 | [[package]] 76 | name = "euclid" 77 | version = "0.22.11" 78 | source = "registry+https://github.com/rust-lang/crates.io-index" 79 | checksum = "ad9cdb4b747e485a12abb0e6566612956c7a1bafa3bdb8d682c5b6d403589e48" 80 | dependencies = [ 81 | "num-traits", 82 | ] 83 | 84 | [[package]] 85 | name = "fastrand" 86 | version = "2.1.1" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" 89 | 90 | [[package]] 91 | name = "futures" 92 | version = "0.3.31" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" 95 | dependencies = [ 96 | "futures-channel", 97 | "futures-core", 98 | "futures-executor", 99 | "futures-io", 100 | "futures-sink", 101 | "futures-task", 102 | "futures-util", 103 | ] 104 | 105 | [[package]] 106 | name = "futures-channel" 107 | version = "0.3.31" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" 110 | dependencies = [ 111 | "futures-core", 112 | "futures-sink", 113 | ] 114 | 115 | [[package]] 116 | name = "futures-core" 117 | version = "0.3.31" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" 120 | 121 | [[package]] 122 | name = "futures-executor" 123 | version = "0.3.31" 124 | source = "registry+https://github.com/rust-lang/crates.io-index" 125 | checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" 126 | dependencies = [ 127 | "futures-core", 128 | "futures-task", 129 | "futures-util", 130 | ] 131 | 132 | [[package]] 133 | name = "futures-io" 134 | version = "0.3.31" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" 137 | 138 | [[package]] 139 | name = "futures-macro" 140 | version = "0.3.31" 141 | source = "registry+https://github.com/rust-lang/crates.io-index" 142 | checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" 143 | dependencies = [ 144 | "proc-macro2", 145 | "quote", 146 | "syn", 147 | ] 148 | 149 | [[package]] 150 | name = "futures-sink" 151 | version = "0.3.31" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" 154 | 155 | [[package]] 156 | name = "futures-task" 157 | version = "0.3.31" 158 | source = "registry+https://github.com/rust-lang/crates.io-index" 159 | checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" 160 | 161 | [[package]] 162 | name = "futures-util" 163 | version = "0.3.31" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" 166 | dependencies = [ 167 | "futures-channel", 168 | "futures-core", 169 | "futures-io", 170 | "futures-macro", 171 | "futures-sink", 172 | "futures-task", 173 | "memchr", 174 | "pin-project-lite", 175 | "pin-utils", 176 | "slab", 177 | ] 178 | 179 | [[package]] 180 | name = "instant" 181 | version = "0.1.13" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" 184 | dependencies = [ 185 | "cfg-if", 186 | "js-sys", 187 | "wasm-bindgen", 188 | "web-sys", 189 | ] 190 | 191 | [[package]] 192 | name = "js-sys" 193 | version = "0.3.72" 194 | source = "registry+https://github.com/rust-lang/crates.io-index" 195 | checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" 196 | dependencies = [ 197 | "wasm-bindgen", 198 | ] 199 | 200 | [[package]] 201 | name = "lazy_static" 202 | version = "1.5.0" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" 205 | 206 | [[package]] 207 | name = "libc" 208 | version = "0.2.161" 209 | source = "registry+https://github.com/rust-lang/crates.io-index" 210 | checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" 211 | 212 | [[package]] 213 | name = "libloading" 214 | version = "0.8.5" 215 | source = "registry+https://github.com/rust-lang/crates.io-index" 216 | checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" 217 | dependencies = [ 218 | "cfg-if", 219 | "windows-targets", 220 | ] 221 | 222 | [[package]] 223 | name = "libm" 224 | version = "0.2.11" 225 | source = "registry+https://github.com/rust-lang/crates.io-index" 226 | checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" 227 | 228 | [[package]] 229 | name = "libredox" 230 | version = "0.1.3" 231 | source = "registry+https://github.com/rust-lang/crates.io-index" 232 | checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" 233 | dependencies = [ 234 | "bitflags 2.6.0", 235 | "libc", 236 | "redox_syscall", 237 | ] 238 | 239 | [[package]] 240 | name = "linux-raw-sys" 241 | version = "0.4.14" 242 | source = "registry+https://github.com/rust-lang/crates.io-index" 243 | checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" 244 | 245 | [[package]] 246 | name = "log" 247 | version = "0.4.22" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" 250 | 251 | [[package]] 252 | name = "lyon_geom" 253 | version = "1.0.6" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | checksum = "8af69edc087272df438b3ee436c4bb6d7c04aa8af665cfd398feae627dbd8570" 256 | dependencies = [ 257 | "arrayvec", 258 | "euclid", 259 | "num-traits", 260 | ] 261 | 262 | [[package]] 263 | name = "memchr" 264 | version = "2.7.4" 265 | source = "registry+https://github.com/rust-lang/crates.io-index" 266 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 267 | 268 | [[package]] 269 | name = "memoffset" 270 | version = "0.6.5" 271 | source = "registry+https://github.com/rust-lang/crates.io-index" 272 | checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" 273 | dependencies = [ 274 | "autocfg", 275 | ] 276 | 277 | [[package]] 278 | name = "minifb" 279 | version = "0.27.0" 280 | source = "registry+https://github.com/rust-lang/crates.io-index" 281 | checksum = "b0c470a74618b43cd182c21b3dc1e6123501249f3bad9a0085e95d1304ca2478" 282 | dependencies = [ 283 | "cc", 284 | "dlib", 285 | "futures", 286 | "instant", 287 | "js-sys", 288 | "lazy_static", 289 | "libc", 290 | "orbclient", 291 | "raw-window-handle", 292 | "serde", 293 | "serde_derive", 294 | "tempfile", 295 | "wasm-bindgen-futures", 296 | "wayland-client", 297 | "wayland-cursor", 298 | "wayland-protocols", 299 | "winapi", 300 | "x11-dl", 301 | ] 302 | 303 | [[package]] 304 | name = "motion" 305 | version = "0.1.5" 306 | source = "registry+https://github.com/rust-lang/crates.io-index" 307 | checksum = "a683ed62b398f5b2860267545b1d3708e60d24a586d57c37100552400c27191f" 308 | 309 | [[package]] 310 | name = "nix" 311 | version = "0.24.3" 312 | source = "registry+https://github.com/rust-lang/crates.io-index" 313 | checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" 314 | dependencies = [ 315 | "bitflags 1.3.2", 316 | "cfg-if", 317 | "libc", 318 | "memoffset", 319 | ] 320 | 321 | [[package]] 322 | name = "num-traits" 323 | version = "0.2.19" 324 | source = "registry+https://github.com/rust-lang/crates.io-index" 325 | checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 326 | dependencies = [ 327 | "autocfg", 328 | "libm", 329 | ] 330 | 331 | [[package]] 332 | name = "once_cell" 333 | version = "1.20.2" 334 | source = "registry+https://github.com/rust-lang/crates.io-index" 335 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 336 | 337 | [[package]] 338 | name = "orbclient" 339 | version = "0.3.48" 340 | source = "registry+https://github.com/rust-lang/crates.io-index" 341 | checksum = "ba0b26cec2e24f08ed8bb31519a9333140a6599b867dac464bb150bdb796fd43" 342 | dependencies = [ 343 | "libc", 344 | "libredox", 345 | "sdl2", 346 | "sdl2-sys", 347 | ] 348 | 349 | [[package]] 350 | name = "pathfinder_geometry" 351 | version = "0.5.1" 352 | source = "registry+https://github.com/rust-lang/crates.io-index" 353 | checksum = "0b7b7e7b4ea703700ce73ebf128e1450eb69c3a8329199ffbfb9b2a0418e5ad3" 354 | dependencies = [ 355 | "log", 356 | "pathfinder_simd", 357 | ] 358 | 359 | [[package]] 360 | name = "pathfinder_simd" 361 | version = "0.5.4" 362 | source = "registry+https://github.com/rust-lang/crates.io-index" 363 | checksum = "5cf07ef4804cfa9aea3b04a7bbdd5a40031dbb6b4f2cbaf2b011666c80c5b4f2" 364 | dependencies = [ 365 | "rustc_version", 366 | ] 367 | 368 | [[package]] 369 | name = "pin-project-lite" 370 | version = "0.2.15" 371 | source = "registry+https://github.com/rust-lang/crates.io-index" 372 | checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" 373 | 374 | [[package]] 375 | name = "pin-utils" 376 | version = "0.1.0" 377 | source = "registry+https://github.com/rust-lang/crates.io-index" 378 | checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" 379 | 380 | [[package]] 381 | name = "pkg-config" 382 | version = "0.3.31" 383 | source = "registry+https://github.com/rust-lang/crates.io-index" 384 | checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" 385 | 386 | [[package]] 387 | name = "proc-macro2" 388 | version = "1.0.89" 389 | source = "registry+https://github.com/rust-lang/crates.io-index" 390 | checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" 391 | dependencies = [ 392 | "unicode-ident", 393 | ] 394 | 395 | [[package]] 396 | name = "quote" 397 | version = "1.0.37" 398 | source = "registry+https://github.com/rust-lang/crates.io-index" 399 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 400 | dependencies = [ 401 | "proc-macro2", 402 | ] 403 | 404 | [[package]] 405 | name = "raqote" 406 | version = "0.8.5" 407 | source = "registry+https://github.com/rust-lang/crates.io-index" 408 | checksum = "324990858d2a5df9ccd30b2e8b474030bd503297378a3fc0944c37af00eb8903" 409 | dependencies = [ 410 | "euclid", 411 | "lyon_geom", 412 | "pathfinder_geometry", 413 | "sw-composite", 414 | "typed-arena", 415 | ] 416 | 417 | [[package]] 418 | name = "raqote_example" 419 | version = "0.1.0" 420 | dependencies = [ 421 | "minifb", 422 | "motion", 423 | "raqote", 424 | ] 425 | 426 | [[package]] 427 | name = "raw-window-handle" 428 | version = "0.6.2" 429 | source = "registry+https://github.com/rust-lang/crates.io-index" 430 | checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" 431 | 432 | [[package]] 433 | name = "redox_syscall" 434 | version = "0.5.7" 435 | source = "registry+https://github.com/rust-lang/crates.io-index" 436 | checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" 437 | dependencies = [ 438 | "bitflags 2.6.0", 439 | ] 440 | 441 | [[package]] 442 | name = "rustc_version" 443 | version = "0.4.1" 444 | source = "registry+https://github.com/rust-lang/crates.io-index" 445 | checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" 446 | dependencies = [ 447 | "semver", 448 | ] 449 | 450 | [[package]] 451 | name = "rustix" 452 | version = "0.38.38" 453 | source = "registry+https://github.com/rust-lang/crates.io-index" 454 | checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a" 455 | dependencies = [ 456 | "bitflags 2.6.0", 457 | "errno", 458 | "libc", 459 | "linux-raw-sys", 460 | "windows-sys 0.52.0", 461 | ] 462 | 463 | [[package]] 464 | name = "scoped-tls" 465 | version = "1.0.1" 466 | source = "registry+https://github.com/rust-lang/crates.io-index" 467 | checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" 468 | 469 | [[package]] 470 | name = "sdl2" 471 | version = "0.35.2" 472 | source = "registry+https://github.com/rust-lang/crates.io-index" 473 | checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a" 474 | dependencies = [ 475 | "bitflags 1.3.2", 476 | "lazy_static", 477 | "libc", 478 | "sdl2-sys", 479 | ] 480 | 481 | [[package]] 482 | name = "sdl2-sys" 483 | version = "0.35.2" 484 | source = "registry+https://github.com/rust-lang/crates.io-index" 485 | checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0" 486 | dependencies = [ 487 | "cfg-if", 488 | "libc", 489 | "version-compare", 490 | ] 491 | 492 | [[package]] 493 | name = "semver" 494 | version = "1.0.23" 495 | source = "registry+https://github.com/rust-lang/crates.io-index" 496 | checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" 497 | 498 | [[package]] 499 | name = "serde" 500 | version = "1.0.214" 501 | source = "registry+https://github.com/rust-lang/crates.io-index" 502 | checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" 503 | dependencies = [ 504 | "serde_derive", 505 | ] 506 | 507 | [[package]] 508 | name = "serde_derive" 509 | version = "1.0.214" 510 | source = "registry+https://github.com/rust-lang/crates.io-index" 511 | checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" 512 | dependencies = [ 513 | "proc-macro2", 514 | "quote", 515 | "syn", 516 | ] 517 | 518 | [[package]] 519 | name = "shlex" 520 | version = "1.3.0" 521 | source = "registry+https://github.com/rust-lang/crates.io-index" 522 | checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" 523 | 524 | [[package]] 525 | name = "slab" 526 | version = "0.4.9" 527 | source = "registry+https://github.com/rust-lang/crates.io-index" 528 | checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" 529 | dependencies = [ 530 | "autocfg", 531 | ] 532 | 533 | [[package]] 534 | name = "smallvec" 535 | version = "1.13.2" 536 | source = "registry+https://github.com/rust-lang/crates.io-index" 537 | checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" 538 | 539 | [[package]] 540 | name = "sw-composite" 541 | version = "0.7.16" 542 | source = "registry+https://github.com/rust-lang/crates.io-index" 543 | checksum = "9ac8fb7895b4afa060ad731a32860db8755da3449a47e796d5ecf758db2671d4" 544 | 545 | [[package]] 546 | name = "syn" 547 | version = "2.0.85" 548 | source = "registry+https://github.com/rust-lang/crates.io-index" 549 | checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56" 550 | dependencies = [ 551 | "proc-macro2", 552 | "quote", 553 | "unicode-ident", 554 | ] 555 | 556 | [[package]] 557 | name = "tempfile" 558 | version = "3.13.0" 559 | source = "registry+https://github.com/rust-lang/crates.io-index" 560 | checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" 561 | dependencies = [ 562 | "cfg-if", 563 | "fastrand", 564 | "once_cell", 565 | "rustix", 566 | "windows-sys 0.59.0", 567 | ] 568 | 569 | [[package]] 570 | name = "typed-arena" 571 | version = "2.0.2" 572 | source = "registry+https://github.com/rust-lang/crates.io-index" 573 | checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" 574 | 575 | [[package]] 576 | name = "unicode-ident" 577 | version = "1.0.13" 578 | source = "registry+https://github.com/rust-lang/crates.io-index" 579 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 580 | 581 | [[package]] 582 | name = "version-compare" 583 | version = "0.1.1" 584 | source = "registry+https://github.com/rust-lang/crates.io-index" 585 | checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" 586 | 587 | [[package]] 588 | name = "wasm-bindgen" 589 | version = "0.2.95" 590 | source = "registry+https://github.com/rust-lang/crates.io-index" 591 | checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" 592 | dependencies = [ 593 | "cfg-if", 594 | "once_cell", 595 | "wasm-bindgen-macro", 596 | ] 597 | 598 | [[package]] 599 | name = "wasm-bindgen-backend" 600 | version = "0.2.95" 601 | source = "registry+https://github.com/rust-lang/crates.io-index" 602 | checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" 603 | dependencies = [ 604 | "bumpalo", 605 | "log", 606 | "once_cell", 607 | "proc-macro2", 608 | "quote", 609 | "syn", 610 | "wasm-bindgen-shared", 611 | ] 612 | 613 | [[package]] 614 | name = "wasm-bindgen-futures" 615 | version = "0.4.45" 616 | source = "registry+https://github.com/rust-lang/crates.io-index" 617 | checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" 618 | dependencies = [ 619 | "cfg-if", 620 | "js-sys", 621 | "wasm-bindgen", 622 | "web-sys", 623 | ] 624 | 625 | [[package]] 626 | name = "wasm-bindgen-macro" 627 | version = "0.2.95" 628 | source = "registry+https://github.com/rust-lang/crates.io-index" 629 | checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" 630 | dependencies = [ 631 | "quote", 632 | "wasm-bindgen-macro-support", 633 | ] 634 | 635 | [[package]] 636 | name = "wasm-bindgen-macro-support" 637 | version = "0.2.95" 638 | source = "registry+https://github.com/rust-lang/crates.io-index" 639 | checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" 640 | dependencies = [ 641 | "proc-macro2", 642 | "quote", 643 | "syn", 644 | "wasm-bindgen-backend", 645 | "wasm-bindgen-shared", 646 | ] 647 | 648 | [[package]] 649 | name = "wasm-bindgen-shared" 650 | version = "0.2.95" 651 | source = "registry+https://github.com/rust-lang/crates.io-index" 652 | checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" 653 | 654 | [[package]] 655 | name = "wayland-client" 656 | version = "0.29.5" 657 | source = "registry+https://github.com/rust-lang/crates.io-index" 658 | checksum = "3f3b068c05a039c9f755f881dc50f01732214f5685e379829759088967c46715" 659 | dependencies = [ 660 | "bitflags 1.3.2", 661 | "downcast-rs", 662 | "libc", 663 | "nix", 664 | "scoped-tls", 665 | "wayland-commons", 666 | "wayland-scanner", 667 | "wayland-sys", 668 | ] 669 | 670 | [[package]] 671 | name = "wayland-commons" 672 | version = "0.29.5" 673 | source = "registry+https://github.com/rust-lang/crates.io-index" 674 | checksum = "8691f134d584a33a6606d9d717b95c4fa20065605f798a3f350d78dced02a902" 675 | dependencies = [ 676 | "nix", 677 | "once_cell", 678 | "smallvec", 679 | "wayland-sys", 680 | ] 681 | 682 | [[package]] 683 | name = "wayland-cursor" 684 | version = "0.29.5" 685 | source = "registry+https://github.com/rust-lang/crates.io-index" 686 | checksum = "6865c6b66f13d6257bef1cd40cbfe8ef2f150fb8ebbdb1e8e873455931377661" 687 | dependencies = [ 688 | "nix", 689 | "wayland-client", 690 | "xcursor", 691 | ] 692 | 693 | [[package]] 694 | name = "wayland-protocols" 695 | version = "0.29.5" 696 | source = "registry+https://github.com/rust-lang/crates.io-index" 697 | checksum = "b950621f9354b322ee817a23474e479b34be96c2e909c14f7bc0100e9a970bc6" 698 | dependencies = [ 699 | "bitflags 1.3.2", 700 | "wayland-client", 701 | "wayland-commons", 702 | "wayland-scanner", 703 | ] 704 | 705 | [[package]] 706 | name = "wayland-scanner" 707 | version = "0.29.5" 708 | source = "registry+https://github.com/rust-lang/crates.io-index" 709 | checksum = "8f4303d8fa22ab852f789e75a967f0a2cdc430a607751c0499bada3e451cbd53" 710 | dependencies = [ 711 | "proc-macro2", 712 | "quote", 713 | "xml-rs", 714 | ] 715 | 716 | [[package]] 717 | name = "wayland-sys" 718 | version = "0.29.5" 719 | source = "registry+https://github.com/rust-lang/crates.io-index" 720 | checksum = "be12ce1a3c39ec7dba25594b97b42cb3195d54953ddb9d3d95a7c3902bc6e9d4" 721 | dependencies = [ 722 | "dlib", 723 | "lazy_static", 724 | "pkg-config", 725 | ] 726 | 727 | [[package]] 728 | name = "web-sys" 729 | version = "0.3.72" 730 | source = "registry+https://github.com/rust-lang/crates.io-index" 731 | checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" 732 | dependencies = [ 733 | "js-sys", 734 | "wasm-bindgen", 735 | ] 736 | 737 | [[package]] 738 | name = "winapi" 739 | version = "0.3.9" 740 | source = "registry+https://github.com/rust-lang/crates.io-index" 741 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 742 | dependencies = [ 743 | "winapi-i686-pc-windows-gnu", 744 | "winapi-x86_64-pc-windows-gnu", 745 | ] 746 | 747 | [[package]] 748 | name = "winapi-i686-pc-windows-gnu" 749 | version = "0.4.0" 750 | source = "registry+https://github.com/rust-lang/crates.io-index" 751 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 752 | 753 | [[package]] 754 | name = "winapi-x86_64-pc-windows-gnu" 755 | version = "0.4.0" 756 | source = "registry+https://github.com/rust-lang/crates.io-index" 757 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 758 | 759 | [[package]] 760 | name = "windows-sys" 761 | version = "0.52.0" 762 | source = "registry+https://github.com/rust-lang/crates.io-index" 763 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 764 | dependencies = [ 765 | "windows-targets", 766 | ] 767 | 768 | [[package]] 769 | name = "windows-sys" 770 | version = "0.59.0" 771 | source = "registry+https://github.com/rust-lang/crates.io-index" 772 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 773 | dependencies = [ 774 | "windows-targets", 775 | ] 776 | 777 | [[package]] 778 | name = "windows-targets" 779 | version = "0.52.6" 780 | source = "registry+https://github.com/rust-lang/crates.io-index" 781 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 782 | dependencies = [ 783 | "windows_aarch64_gnullvm", 784 | "windows_aarch64_msvc", 785 | "windows_i686_gnu", 786 | "windows_i686_gnullvm", 787 | "windows_i686_msvc", 788 | "windows_x86_64_gnu", 789 | "windows_x86_64_gnullvm", 790 | "windows_x86_64_msvc", 791 | ] 792 | 793 | [[package]] 794 | name = "windows_aarch64_gnullvm" 795 | version = "0.52.6" 796 | source = "registry+https://github.com/rust-lang/crates.io-index" 797 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 798 | 799 | [[package]] 800 | name = "windows_aarch64_msvc" 801 | version = "0.52.6" 802 | source = "registry+https://github.com/rust-lang/crates.io-index" 803 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 804 | 805 | [[package]] 806 | name = "windows_i686_gnu" 807 | version = "0.52.6" 808 | source = "registry+https://github.com/rust-lang/crates.io-index" 809 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 810 | 811 | [[package]] 812 | name = "windows_i686_gnullvm" 813 | version = "0.52.6" 814 | source = "registry+https://github.com/rust-lang/crates.io-index" 815 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 816 | 817 | [[package]] 818 | name = "windows_i686_msvc" 819 | version = "0.52.6" 820 | source = "registry+https://github.com/rust-lang/crates.io-index" 821 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 822 | 823 | [[package]] 824 | name = "windows_x86_64_gnu" 825 | version = "0.52.6" 826 | source = "registry+https://github.com/rust-lang/crates.io-index" 827 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 828 | 829 | [[package]] 830 | name = "windows_x86_64_gnullvm" 831 | version = "0.52.6" 832 | source = "registry+https://github.com/rust-lang/crates.io-index" 833 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 834 | 835 | [[package]] 836 | name = "windows_x86_64_msvc" 837 | version = "0.52.6" 838 | source = "registry+https://github.com/rust-lang/crates.io-index" 839 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 840 | 841 | [[package]] 842 | name = "x11-dl" 843 | version = "2.21.0" 844 | source = "registry+https://github.com/rust-lang/crates.io-index" 845 | checksum = "38735924fedd5314a6e548792904ed8c6de6636285cb9fec04d5b1db85c1516f" 846 | dependencies = [ 847 | "libc", 848 | "once_cell", 849 | "pkg-config", 850 | ] 851 | 852 | [[package]] 853 | name = "xcursor" 854 | version = "0.3.8" 855 | source = "registry+https://github.com/rust-lang/crates.io-index" 856 | checksum = "0ef33da6b1660b4ddbfb3aef0ade110c8b8a781a3b6382fa5f2b5b040fd55f61" 857 | 858 | [[package]] 859 | name = "xml-rs" 860 | version = "0.8.22" 861 | source = "registry+https://github.com/rust-lang/crates.io-index" 862 | checksum = "af4e2e2f7cba5a093896c1e150fbfe177d1883e7448200efb81d40b9d339ef26" 863 | -------------------------------------------------------------------------------- /examples/raqote_example/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "raqote_example" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | minifb = { version = "0.27.0", features = ["wayland"] } 8 | motion = "*" 9 | # by default raqote asks for the fontconfig.pc lib installed but in this example it is not necessary. 10 | raqote = { version = "0.8.5", features = [ 11 | "pathfinder_geometry", 12 | ], default-features = false } 13 | -------------------------------------------------------------------------------- /examples/raqote_example/shell.nix: -------------------------------------------------------------------------------- 1 | 2 | 3 | { pkgs ? import {} }: 4 | pkgs.mkShell { 5 | buildInputs = with pkgs; [ 6 | alsa-lib 7 | dbus 8 | fontconfig 9 | libGL 10 | libpulseaudio 11 | libxkbcommon 12 | makeWrapper 13 | mesa 14 | patchelf 15 | speechd 16 | udev 17 | vulkan-loader 18 | xorg.libX11 19 | xorg.libXcursor 20 | xorg.libXext 21 | xorg.libXfixes 22 | xorg.libXi 23 | xorg.libXinerama 24 | xorg.libXrandr 25 | xorg.libXrender 26 | ]; 27 | 28 | shellHook = '' 29 | export LD_LIBRARY_PATH=${pkgs.alsa-lib}/lib:${pkgs.dbus}/lib:${pkgs.fontconfig}/lib:${pkgs.libGL}/lib:${pkgs.libpulseaudio}/lib:${pkgs.libxkbcommon}/lib:${pkgs.mesa}/lib:${pkgs.vulkan-loader}/lib:${pkgs.xorg.libX11}/lib:${pkgs.xorg.libXcursor}/lib:${pkgs.xorg.libXext}/lib:${pkgs.xorg.libXfixes}/lib:${pkgs.xorg.libXi}/lib:${pkgs.xorg.libXinerama}/lib:${pkgs.xorg.libXrandr}/lib:${pkgs.xorg.libXrender}/lib:$LD_LIBRARY_PATH 30 | 31 | ''; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /examples/raqote_example/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::{f32::consts::PI, thread, time::Duration}; 2 | 3 | use minifb::{Window, WindowOptions}; 4 | use motion::{ 5 | collision::{shape::Shape, Collision2d}, 6 | event_loop::EventLoopBuilder, 7 | forces::r#move::{move2d, Direction2d}, 8 | formulas::elastic_collision, 9 | obj::obj_2d::Object2dBuilder, 10 | vec::vec_2d::vec2, 11 | }; 12 | use raqote::{DrawOptions, DrawTarget, PathBuilder, SolidSource, Source}; 13 | 14 | const WIDTH: usize = 800; 15 | const HEIGHT: usize = 500; 16 | 17 | fn sleep(time: Duration) { 18 | thread::sleep(time); 19 | } 20 | 21 | /* 22 | *this is an example of elastic collisions using motion for more information see their wikipedia page: https://en.wikipedia.org/wiki/Elastic_collision 23 | */ 24 | 25 | fn main() { 26 | let mut window = Window::new( 27 | "Motion + raqote", 28 | WIDTH, 29 | HEIGHT, 30 | WindowOptions { 31 | ..WindowOptions::default() 32 | }, 33 | ) 34 | .unwrap(); 35 | 36 | let size = window.get_size(); 37 | let mut dt = DrawTarget::new(size.0 as i32, size.1 as i32); 38 | 39 | let mut obj1 = Object2dBuilder::new() 40 | .position(vec2(10.0, 100.0)) 41 | .radius(20.0) 42 | .mass(1.0) 43 | .density(2.0) 44 | .acceleration(vec2(2000.0, 10.0)) 45 | .shape(Shape::Circle) 46 | .velocity(vec2(2000.0, 10.0)) 47 | .build(); 48 | 49 | let mut obj2 = Object2dBuilder::new() 50 | .position(vec2(300.0, 100.0)) 51 | .radius(20.0) 52 | .mass(1.0) 53 | .density(2.0) 54 | .acceleration(vec2(0.0, 0.0)) 55 | .shape(Shape::Circle) 56 | .velocity(vec2(0.0, 0.0)) 57 | .build(); 58 | 59 | let el = EventLoopBuilder::new().fps(60).build(); 60 | 61 | el.start_mut( 62 | move |config| { 63 | dt.clear(SolidSource::from_unpremultiplied_argb( 64 | 0xff, 0xff, 0xff, 0xff, 65 | )); 66 | let mut pb = PathBuilder::new(); 67 | obj1.apply(&move2d( 68 | Direction2d::X, 69 | config.delta_time, 70 | config.delta_time, 71 | )); 72 | obj2.apply(&move2d( 73 | Direction2d::X, 74 | config.delta_time, 75 | config.delta_time, 76 | )); 77 | let collide = Collision2d::new(obj1, obj2); 78 | 79 | if collide.collider() { 80 | let u = elastic_collision::calculate( 81 | obj1.velocity, 82 | obj1.mass, 83 | obj2.mass, 84 | obj2.velocity, 85 | ); 86 | let u2 = elastic_collision::calculate( 87 | obj2.velocity, 88 | obj2.mass, 89 | obj1.mass, 90 | obj1.velocity, 91 | ); 92 | 93 | obj1.velocity = u; 94 | obj1.acceleration = u; 95 | 96 | obj2.velocity = u2; 97 | obj2.acceleration = u2; 98 | } 99 | 100 | pb.arc(obj1.vec.x, obj1.vec.y, obj1.radius, 0.0, 2.0 * PI); 101 | let path = pb.finish(); 102 | dt.fill( 103 | &path, 104 | &Source::Solid(SolidSource::from_unpremultiplied_argb( 105 | 0xff, 0x00, 0x00, 0x00, 106 | )), 107 | &DrawOptions::new(), 108 | ); 109 | 110 | let mut pb2 = PathBuilder::new(); 111 | 112 | pb2.arc(obj2.vec.x, obj2.vec.y, obj2.radius, 0.0, 2.0 * PI); 113 | let path2 = pb2.finish(); 114 | 115 | dt.fill( 116 | &path2, 117 | &Source::Solid(SolidSource::from_unpremultiplied_argb( 118 | 0xff, 0x00, 0x00, 0x00, 119 | )), 120 | &DrawOptions::new(), 121 | ); 122 | 123 | window 124 | .update_with_buffer(dt.get_data(), size.0, size.1) 125 | .unwrap(); 126 | }, 127 | sleep, 128 | ); 129 | } 130 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "nixpkgs": { 4 | "locked": { 5 | "lastModified": 1728888510, 6 | "narHash": "sha256-nsNdSldaAyu6PE3YUA+YQLqUDJh+gRbBooMMekZJwvI=", 7 | "owner": "nixos", 8 | "repo": "nixpkgs", 9 | "rev": "a3c0b3b21515f74fd2665903d4ce6bc4dc81c77c", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "owner": "nixos", 14 | "ref": "nixos-unstable", 15 | "repo": "nixpkgs", 16 | "type": "github" 17 | } 18 | }, 19 | "root": { 20 | "inputs": { 21 | "nixpkgs": "nixpkgs" 22 | } 23 | } 24 | }, 25 | "root": "root", 26 | "version": 7 27 | } 28 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "Record basic dev flake"; 3 | 4 | inputs = { 5 | nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; 6 | }; 7 | 8 | outputs = { self, nixpkgs }: 9 | let 10 | system = "x86_64-linux"; 11 | pkgs = import nixpkgs { inherit system; }; 12 | in { 13 | devShells = { 14 | ${system} = { 15 | docs = pkgs.mkShell { 16 | buildInputs = with pkgs; [ 17 | openssl 18 | ]; 19 | 20 | PKG_CONFIG_PATH="${pkgs.openssl}/lib"; 21 | }; 22 | }; 23 | }; 24 | }; 25 | } 26 | -------------------------------------------------------------------------------- /release.toml: -------------------------------------------------------------------------------- 1 | allow-branch = ["main", "dev"] 2 | publish = true 3 | pre-release-commit-message = "release: {{crate_name}} version {{version}}" 4 | tag-prefix = "{{crate_name}}/" 5 | tag-name = "{{prefix}}:{{version}}" 6 | -------------------------------------------------------------------------------- /src/collision/mod.rs: -------------------------------------------------------------------------------- 1 | use crate::obj::obj_2d::Object2d; 2 | pub mod shape; 3 | use shape::Shape; 4 | 5 | /// Represents a 2D collision detection between two objects. 6 | pub struct Collision2d { 7 | /// The first object involved in the collision. 8 | pub obj1: Object2d, 9 | /// The second object involved in the collision. 10 | pub obj2: Object2d, 11 | } 12 | 13 | impl Collision2d { 14 | /// Creates a new `Collision2d` instance with the specified objects. 15 | /// 16 | /// # Parameters 17 | /// 18 | /// - `obj1`: The first object involved in the collision. 19 | /// - `obj2`: The second object involved in the collision. 20 | /// 21 | /// # Returns 22 | /// 23 | /// A new `Collision2d` instance. 24 | /// 25 | /// # Examples 26 | /// 27 | /// ``` 28 | /// let obj1 = Object2d::new(/* parameters */); 29 | /// let obj2 = Object2d::new(/* parameters */); 30 | /// let collision = Collision2d::new(obj1, obj2); 31 | /// ``` 32 | #[must_use] 33 | pub fn new(obj1: Object2d, obj2: Object2d) -> Self { 34 | Self { obj1, obj2 } 35 | } 36 | 37 | /// Detects if a collision occurs between the two objects. 38 | /// 39 | /// # Returns 40 | /// 41 | /// `true` if a collision is detected, `false` otherwise. 42 | /// 43 | /// # Examples 44 | /// 45 | /// ``` 46 | /// let obj1 = Object2d::new(/* parameters */); 47 | /// let obj2 = Object2d::new(/* parameters */); 48 | /// let collision = Collision2d::new(obj1, obj2); 49 | /// if collision.collider() { 50 | /// println!("Collision detected!"); 51 | /// } 52 | /// ``` 53 | #[must_use] 54 | pub fn collider(&self) -> bool { 55 | match (&self.obj1.shape, &self.obj2.shape) { 56 | (Shape::Circle, Shape::Circle) => self.circle_collision(), 57 | (Shape::AABB(_, _), Shape::AABB(_, _)) => self.aabb_collision(), 58 | _ => false, 59 | } 60 | } 61 | 62 | /// Detects if a collision occurs between two circular shapes. 63 | /// 64 | /// # Returns 65 | /// 66 | /// `true` if a collision is detected, `false` otherwise. 67 | /// 68 | /// # Panics 69 | /// 70 | /// Panics if either shape is not a circle. 71 | fn circle_collision(&self) -> bool { 72 | if self.obj2.shape != Shape::Circle || self.obj1.shape != Shape::Circle { 73 | return false; 74 | } 75 | let distance = self.obj1.vec.distance(self.obj2.vec); 76 | let radius1 = self.obj1.radius; 77 | let radius2 = self.obj2.radius; 78 | distance <= (radius1 + radius2) 79 | } 80 | 81 | /// Detects if a collision occurs between two axis-aligned bounding boxes (AABBs). 82 | /// 83 | /// # Returns 84 | /// 85 | /// `true` if a collision is detected, `false` otherwise. 86 | fn aabb_collision(&self) -> bool { 87 | let vec1 = self.obj1.shape.get_aabb(); 88 | let vec2 = self.obj2.shape.get_aabb(); 89 | vec1.0.x < vec2.1.x && vec1.1.x > vec2.0.x && vec1.0.y < vec2.1.y && vec1.1.y > vec2.0.y 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/collision/shape.rs: -------------------------------------------------------------------------------- 1 | use crate::vec::vec_2d::Vec2d; 2 | 3 | /// Represents different shapes in a 2D space. 4 | #[derive(Debug, Clone, Copy, PartialEq)] 5 | pub enum Shape { 6 | /// No shape defined. 7 | None, 8 | /// A circular shape. 9 | Circle, 10 | /// An axis-aligned bounding box (AABB) defined by two vectors. 11 | AABB(Vec2d, Vec2d), 12 | } 13 | 14 | impl Shape { 15 | /// Retrieves the AABB (axis-aligned bounding box) vectors if the shape is an AABB. 16 | /// 17 | /// # Examples 18 | /// 19 | /// ``` 20 | /// let shape = Shape::AABB(Vec2d::new(0.0, 0.0), Vec2d::new(1.0, 1.0)); 21 | /// let (v1, v2) = shape.get_aabb(); 22 | /// assert_eq!(v1, Vec2d::new(0.0, 0.0)); 23 | /// assert_eq!(v2, Vec2d::new(1.0, 1.0)); 24 | /// ``` 25 | /// 26 | /// # Returns 27 | /// 28 | /// A tuple containing two `Vec2d` vectors representing the corners of the AABB. 29 | /// 30 | /// # Panics 31 | /// 32 | /// Panics if the shape is not an AABB. 33 | #[must_use] 34 | pub fn get_aabb(&self) -> (Vec2d, Vec2d) { 35 | if let Shape::AABB(v1, v2) = self { 36 | (*v1, *v2) 37 | } else { 38 | panic!("Shape is not an AABB"); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/event_loop/mod.rs: -------------------------------------------------------------------------------- 1 | use core::time::Duration; 2 | 3 | /// Configuration for the event loop, specifying the frames per second (fps). 4 | #[derive(Debug, Clone, Copy)] 5 | pub struct EventLoopConfig { 6 | /// Frames per second for the event loop. 7 | pub fps: u32, 8 | /// Delta time calculated from the fps. 9 | pub delta_time: f32, 10 | } 11 | 12 | /// The main structure for the event loop, holding its configuration. 13 | pub struct EventLoop { 14 | /// Configuration for the event loop. 15 | pub config: EventLoopConfig, 16 | } 17 | 18 | impl EventLoop { 19 | /// Creates a new event loop with the specified configuration. 20 | /// 21 | /// # Examples 22 | /// 23 | /// ``` 24 | /// let config = EventLoopConfig { fps: 60, delta_time: 1.0 / 60.0 }; 25 | /// let event_loop = EventLoop::new(config); 26 | /// ``` 27 | /// 28 | /// # Parameters 29 | /// 30 | /// - `config`: The configuration for the event loop. 31 | /// 32 | /// # Returns 33 | /// 34 | /// A new `EventLoop` instance. 35 | #[must_use] 36 | pub fn new(config: EventLoopConfig) -> Self { 37 | Self { config } 38 | } 39 | 40 | /// Starts the event loop, running the provided code at the specified fps, and sleeping in between. 41 | /// 42 | /// # Examples 43 | /// 44 | /// ``` 45 | /// let config = EventLoopConfig { fps: 60, delta_time: 1.0 / 60.0 }; 46 | /// let event_loop = EventLoop::new(config); 47 | /// 48 | /// event_loop.start(|config| { 49 | /// // Your code here 50 | /// }, |duration| { 51 | /// // Sleep for the duration 52 | /// std::thread::sleep(duration); 53 | /// }); 54 | /// ``` 55 | /// 56 | /// # Parameters 57 | /// 58 | /// - `code`: A closure that takes `EventLoopConfig` and contains the code to run each frame. 59 | /// - `sleep`: A closure that takes `Duration` and handles sleeping between frames. 60 | pub fn start(&self, code: F, sleep: SF) 61 | where 62 | F: Fn(EventLoopConfig), 63 | SF: Fn(Duration), 64 | { 65 | let dt = 1 / self.config.fps; 66 | loop { 67 | code(self.config); 68 | sleep(Duration::from_secs(u64::from(dt))); 69 | } 70 | } 71 | 72 | /// Starts the event loop with a mutable closure, allowing modification of the loop configuration. 73 | /// 74 | /// # Examples 75 | /// 76 | /// ``` 77 | /// let config = EventLoopConfig { fps: 60, delta_time: 1.0 / 60.0 }; 78 | /// let event_loop = EventLoop::new(config); 79 | /// 80 | /// event_loop.start_mut(|config| { 81 | /// // Your code here 82 | /// }, |duration| { 83 | /// // Sleep for the duration 84 | /// std::thread::sleep(duration); 85 | /// }); 86 | /// ``` 87 | /// 88 | /// # Parameters 89 | /// 90 | /// - `code`: A mutable closure that takes `EventLoopConfig` and contains the code to run each frame. 91 | /// - `sleep`: A closure that takes `Duration` and handles sleeping between frames. 92 | pub fn start_mut(&self, mut code: F, sleep: SF) 93 | where 94 | F: FnMut(EventLoopConfig), 95 | SF: Fn(Duration), 96 | { 97 | let dt = 1 / self.config.fps; 98 | loop { 99 | code(self.config); 100 | sleep(Duration::from_secs(u64::from(dt))); 101 | } 102 | } 103 | } 104 | 105 | /// Builder pattern for constructing an `EventLoop`. 106 | #[derive(Debug)] 107 | pub struct EventLoopBuilder { 108 | config: EventLoopConfig, 109 | } 110 | 111 | impl EventLoopBuilder { 112 | /// Creates a new `EventLoopBuilder` with default configuration. 113 | /// 114 | /// # Examples 115 | /// 116 | /// ``` 117 | /// let builder = EventLoopBuilder::new(); 118 | /// ``` 119 | pub fn new() -> Self { 120 | EventLoopBuilder { 121 | config: EventLoopConfig { 122 | fps: 60, 123 | delta_time: 1.0 / 60.0, 124 | }, 125 | } 126 | } 127 | 128 | /// Sets the frames per second (fps) for the event loop. 129 | /// 130 | /// # Parameters 131 | /// 132 | /// - `fps`: The frames per second to set. 133 | /// 134 | /// # Returns 135 | /// 136 | /// The updated `EventLoopBuilder` instance. 137 | pub fn fps(mut self, fps: u32) -> Self { 138 | self.config = EventLoopConfig { 139 | fps, 140 | delta_time: 1.0 / fps as f32, 141 | }; 142 | self 143 | } 144 | 145 | /// Builds the `EventLoop` with the specified configuration. 146 | /// 147 | /// # Returns 148 | /// 149 | /// A new `EventLoop` instance. 150 | pub fn build(&self) -> EventLoop { 151 | EventLoop { 152 | config: self.config, 153 | } 154 | } 155 | } 156 | 157 | impl Default for EventLoopBuilder { 158 | fn default() -> Self { 159 | EventLoopBuilder { 160 | config: EventLoopConfig { 161 | fps: 60, 162 | delta_time: 1.0 / 60.0, 163 | }, 164 | } 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/forces/force.rs: -------------------------------------------------------------------------------- 1 | use crate::obj::obj_2d::Object2d; 2 | 3 | /// A trait for applying forces to 2D objects. 4 | /// 5 | /// # Examples 6 | /// 7 | /// Implementing the `Force` trait for a custom force: 8 | /// 9 | /// ``` 10 | /// struct Gravity; 11 | /// 12 | /// impl Force for Gravity { 13 | /// fn apply_2d(&mut self, obj: &Object2d) { 14 | /// // Apply gravitational force to obj 15 | /// } 16 | /// } 17 | /// 18 | /// ``` 19 | /// 20 | /// # Methods 21 | /// 22 | /// - `apply_2d(&self, obj: &mut Object2d)`: Applies a force to a 2D object. 23 | pub trait Force { 24 | fn apply_2d(&self, obj: &mut Object2d); 25 | } 26 | -------------------------------------------------------------------------------- /src/forces/gravity.rs: -------------------------------------------------------------------------------- 1 | use crate::obj::obj_2d::Object2d; 2 | 3 | use super::force::Force; 4 | 5 | /// Constant for Earth's gravity in meters per second squared. 6 | pub const EARTH_GRAVITY: f32 = -9.807; 7 | 8 | /// Represents the gravitational force applied to an object. 9 | /// 10 | /// # Fields 11 | /// 12 | /// - `force`: The gravitational force. 13 | /// - `time`: The time over which the force is applied. 14 | /// - `delta_time`: The time step for the simulation. 15 | #[derive(Debug)] 16 | pub struct Gravity { 17 | pub force: f32, 18 | pub time: f32, 19 | pub delta_time: f32, 20 | } 21 | 22 | impl Force for Gravity { 23 | /// Applies the gravitational force to a 2D object. 24 | /// 25 | /// # Examples 26 | /// 27 | /// ``` 28 | /// let mut obj = Object2d::new(/* initial parameters */); 29 | /// let gravity = Gravity { 30 | /// force: EARTH_GRAVITY, 31 | /// time: 1.0, 32 | /// delta_time: 0.016, 33 | /// }; 34 | /// gravity.apply_2d(&mut obj); 35 | /// ``` 36 | /// 37 | /// # Parameters 38 | /// 39 | /// - `obj`: The 2D object to which the force is applied. 40 | fn apply_2d(&self, obj: &mut Object2d) { 41 | obj.vec.y += obj.velocity.y * self.delta_time 42 | - 0.5 * self.force * (self.delta_time * self.delta_time); 43 | obj.velocity.y -= self.force * self.delta_time; 44 | } 45 | } 46 | 47 | /// Creates a new `Gravity` instance. 48 | /// 49 | /// # Parameters 50 | /// 51 | /// - `force`: The gravitational force. 52 | /// - `time`: The time over which the force is applied. 53 | /// - `delta_time`: The time step for the simulation. 54 | /// 55 | /// # Returns 56 | /// 57 | /// A new `Gravity` instance. 58 | /// 59 | /// # Examples 60 | /// 61 | /// ``` 62 | /// let gravity = gravity(EARTH_GRAVITY, 1.0, 0.016); 63 | /// ``` 64 | #[inline] 65 | pub fn gravity, T: Into, D: Into>( 66 | force: F, 67 | time: T, 68 | delta_time: D, 69 | ) -> Gravity { 70 | Gravity { 71 | force: force.into(), 72 | time: time.into(), 73 | delta_time: delta_time.into(), 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/forces/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod force; 2 | pub mod gravity; 3 | pub mod r#move; 4 | -------------------------------------------------------------------------------- /src/forces/move.rs: -------------------------------------------------------------------------------- 1 | use super::force::Force; 2 | 3 | /// Represents the direction in a 2D plane. 4 | #[derive(Debug, PartialEq, Eq)] 5 | pub enum Direction2d { 6 | /// Movement along the x-axis. 7 | X, 8 | /// Movement along the y-axis. 9 | Y, 10 | } 11 | 12 | /// Represents a 2D movement with specified direction, delta time, and frames per second. 13 | /// 14 | /// # Fields 15 | /// 16 | /// - `direction`: The direction of the movement (X or Y). 17 | /// - `delta_time`: The time step for the simulation. 18 | /// - `fps`: The frames per second for the simulation. 19 | pub struct Move2d { 20 | pub direction: Direction2d, 21 | pub delta_time: f32, 22 | pub fps: f32, 23 | } 24 | 25 | impl Force for Move2d { 26 | /// Applies the movement force to a 2D object based on the direction. 27 | /// 28 | /// # Examples 29 | /// 30 | /// ``` 31 | /// let mut obj = Object2d::new(/* initial parameters */); 32 | /// let move_force = Move2d { 33 | /// direction: Direction2d::X, 34 | /// delta_time: 0.016, 35 | /// fps: 60.0, 36 | /// }; 37 | /// move_force.apply_2d(&mut obj); 38 | /// ``` 39 | /// 40 | /// # Parameters 41 | /// 42 | /// - `obj`: The 2D object to which the force is applied. 43 | fn apply_2d(&self, obj: &mut crate::obj::obj_2d::Object2d) { 44 | let time = self.fps * self.delta_time; 45 | match self.direction { 46 | Direction2d::Y => { 47 | obj.vec.y = 48 | obj.vec.y + obj.velocity.y * time + 0.5 * obj.acceleration.y * (time * time); 49 | } 50 | Direction2d::X => { 51 | obj.vec.x = 52 | obj.vec.x + obj.velocity.x * time + 0.5 * obj.acceleration.x * (time * time); 53 | } 54 | } 55 | } 56 | } 57 | 58 | /// Creates a new `Move2d` instance. 59 | /// 60 | /// # Parameters 61 | /// 62 | /// - `direction`: The direction of the movement (X or Y). 63 | /// - `delta_time`: The time step for the simulation. 64 | /// - `fps`: The frames per second for the simulation. 65 | /// 66 | /// # Returns 67 | /// 68 | /// A new `Move2d` instance. 69 | /// 70 | /// # Examples 71 | /// 72 | /// ``` 73 | /// let move_force = move2d(Direction2d::X, 0.016, 60.0); 74 | /// ``` 75 | #[inline] 76 | pub fn move2d, F: Into>( 77 | direction: Direction2d, 78 | delta_time: DT, 79 | fps: F, 80 | ) -> Move2d { 81 | Move2d { 82 | direction, 83 | delta_time: delta_time.into(), 84 | fps: fps.into(), 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/formulas/dot.rs: -------------------------------------------------------------------------------- 1 | use crate::vec::vec_2d::Vec2d; 2 | 3 | use super::sqrt::sqrt; 4 | 5 | /// Calculates the dot product of two 2D vectors. 6 | /// 7 | /// # Examples 8 | /// 9 | /// ``` 10 | /// let u = Vec2d::new(3.0, 4.0); 11 | /// let v = Vec2d::new(1.0, 2.0); 12 | /// let result = dot(u, v); 13 | /// assert_eq!(result, 11.0); 14 | /// ``` 15 | /// 16 | /// # Parameters 17 | /// 18 | /// - `u`: The first vector. 19 | /// - `v`: The second vector. 20 | /// 21 | /// # Returns 22 | /// 23 | /// The dot product of the two vectors. 24 | #[must_use] 25 | pub fn dot(u: Vec2d, v: Vec2d) -> f32 { 26 | u.x * v.x + u.y * v.y 27 | } 28 | 29 | /// Calculates the squared length of a 2D vector. 30 | /// 31 | /// # Examples 32 | /// 33 | /// ``` 34 | /// let u = Vec2d::new(3.0, 4.0); 35 | /// let result = length2(u); 36 | /// assert_eq!(result, 25.0); 37 | /// ``` 38 | /// 39 | /// # Parameters 40 | /// 41 | /// - `u`: The vector to calculate the squared length for. 42 | /// 43 | /// # Returns 44 | /// 45 | /// The squared length of the vector. 46 | #[must_use] 47 | pub fn length2(u: Vec2d) -> f32 { 48 | dot(u, u) 49 | } 50 | 51 | /// Calculates the length (magnitude) of a 2D vector. 52 | /// 53 | /// # Examples 54 | /// 55 | /// ``` 56 | /// let u = Vec2d::new(3.0, 4.0); 57 | /// let result = length(u); 58 | /// assert_eq!(result, 5.0); 59 | /// ``` 60 | /// 61 | /// # Parameters 62 | /// 63 | /// - `u`: The vector to calculate the length for. 64 | /// 65 | /// # Returns 66 | /// 67 | /// The length of the vector. 68 | #[must_use] 69 | pub fn length(u: Vec2d) -> f32 { 70 | sqrt(length2(u)) 71 | } 72 | -------------------------------------------------------------------------------- /src/formulas/elastic_collision.rs: -------------------------------------------------------------------------------- 1 | use crate::vec::vec_2d::Vec2d; 2 | 3 | /// Calculates the new velocity of an object after an elastic collision. 4 | /// 5 | /// # Examples 6 | /// 7 | /// ``` 8 | /// let v1 = Vec2d::new(3.0, 4.0); 9 | /// let v2 = Vec2d::new(1.0, 2.0); 10 | /// let m1 = 2.0; 11 | /// let m2 = 3.0; 12 | /// 13 | /// let new_velocity = calculate(v1, m1, m2, v2); 14 | /// ``` 15 | /// 16 | /// # Parameters 17 | /// 18 | /// - `v1`: The velocity of the first object before collision. 19 | /// - `m1`: The mass of the first object. 20 | /// - `m2`: The mass of the second object. 21 | /// - `v2`: The velocity of the second object before collision. 22 | /// 23 | /// # Returns 24 | /// 25 | /// The new velocity of the first object after an elastic collision. 26 | #[must_use] 27 | pub fn calculate(v1: Vec2d, m1: f32, m2: f32, v2: Vec2d) -> Vec2d { 28 | let numerator_x = v1.x * (m1 - m2) + 2.0 * v2.x * m2; 29 | let numerator_y = v1.y * (m1 - m2) + 2.0 * v2.y * m2; 30 | let denominator = m1 + m2; 31 | Vec2d::new(numerator_x / denominator, numerator_y / denominator) 32 | } 33 | -------------------------------------------------------------------------------- /src/formulas/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod dot; 2 | pub mod elastic_collision; 3 | pub mod pitagoras; 4 | pub mod pow; 5 | pub mod sqrt; 6 | -------------------------------------------------------------------------------- /src/formulas/pitagoras.rs: -------------------------------------------------------------------------------- 1 | use super::sqrt::sqrt; 2 | 3 | /// Calculates the hypotenuse of a right-angled triangle using the Pythagorean theorem. 4 | /// 5 | /// # Examples 6 | /// 7 | /// ``` 8 | /// let result = calculate_pt(3.0, 4.0); 9 | /// assert_eq!(result, 5.0); 10 | /// 11 | /// let result = calculate_pt(6.0, 8.0); 12 | /// assert_eq!(result, 10.0); 13 | /// ``` 14 | /// 15 | /// # Parameters 16 | /// 17 | /// - `a`: The length of one side of the right-angled triangle. 18 | /// - `b`: The length of the other side of the right-angled triangle. 19 | /// 20 | /// # Returns 21 | /// 22 | /// The length of the hypotenuse calculated using the Pythagorean theorem. 23 | #[must_use] 24 | pub fn calculate_pt(a: f32, b: f32) -> f32 { 25 | let ab = a * a + b * b; 26 | sqrt(ab) 27 | } 28 | -------------------------------------------------------------------------------- /src/formulas/pow.rs: -------------------------------------------------------------------------------- 1 | /// Calculates the power of a base raised to an exponent. 2 | /// 3 | /// # Examples 4 | /// 5 | /// ``` 6 | /// let result = powf(2.0, 3); 7 | /// assert_eq!(result, 8.0); 8 | /// 9 | /// let result = powf(5.0, -2); 10 | /// assert_eq!(result, 0.04); 11 | /// 12 | /// let result = powf(2.0, 0); 13 | /// assert_eq!(result, 1.0); 14 | /// ``` 15 | /// 16 | /// # Parameters 17 | /// 18 | /// - `base`: The base number to be raised. 19 | /// - `exponent`: The exponent to which the base is raised. Can be positive, negative, or zero. 20 | /// 21 | /// # Returns 22 | /// 23 | /// The result of raising the base to the given exponent. 24 | #[must_use] 25 | pub fn powf(base: f32, exponent: i32) -> f32 { 26 | if exponent == 0 { 27 | return 1.0; 28 | } 29 | let mut result = 1.0; 30 | let mut exp = exponent.abs(); 31 | let mut base_current = base; 32 | while exp > 0 { 33 | if exp & 1 != 0 { 34 | result *= base_current; 35 | } 36 | base_current *= base_current; 37 | exp >>= 1; 38 | } 39 | if exponent < 0 { 40 | return 1.0 / result; 41 | } 42 | result 43 | } 44 | -------------------------------------------------------------------------------- /src/formulas/sqrt.rs: -------------------------------------------------------------------------------- 1 | /// Calculates the square root of a given number. 2 | /// 3 | /// # Examples 4 | /// 5 | /// ``` 6 | /// let result = sqrt(4.0); 7 | /// assert_eq!(result, 2.0); 8 | /// 9 | /// let result = sqrt(9.0); 10 | /// assert_eq!(result, 3.0); 11 | /// 12 | /// let result = sqrt(-1.0); 13 | /// assert!(result.is_nan()); 14 | /// ``` 15 | /// 16 | /// # Parameters 17 | /// 18 | /// - `number`: The number to compute the square root of. If the number is negative, the function will return `NaN`. 19 | /// 20 | /// # Returns 21 | /// 22 | /// The square root of the given number, or `NaN` if the number is negative. 23 | #[must_use] 24 | pub fn sqrt(number: f32) -> f32 { 25 | if number < 0.0 { 26 | return f32::NAN; 27 | } 28 | f32::from_bits((number.to_bits() + 0x3f80_0000) >> 1) 29 | } 30 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![allow(clippy::module_name_repetitions)] 3 | 4 | //! # Motion, Bare Metal physics engine 5 | //! **Motion** is a bare metal physics engine which is created to be easy to use. 6 | //! ## Get started! 7 | //! you can first add the package with 8 | //! ```bash 9 | //! cargo add motion 10 | //! ``` 11 | //! With this you already have record installed in your project, you can start with a simple event loop 12 | //!```rust 13 | //!use std::{thread, time::Duration}; 14 | //! 15 | //!use motion::event_loop::EventLoopBuilder; 16 | //! 17 | // The definition of this function depends on the context in which motion is used 18 | //!fn sleep(duration: Duration) { 19 | //! thread::sleep(duration); 20 | //!} 21 | //! 22 | //!fn main() { 23 | //! let el = EventLoopBuilder::new().fps(1).build(); 24 | //! 25 | //! el.start(|_config| println!("Hello! in the event loop"), sleep); 26 | //!} 27 | //! https://github.com/Juanperias/motion/blob/main/examples/event_loop_example/src/main.rs 28 | //!``` 29 | //! More examples in 30 | //! 31 | 32 | #[cfg(feature = "vec")] 33 | pub mod vec; 34 | 35 | #[cfg(feature = "obj")] 36 | pub mod obj; 37 | 38 | #[cfg(feature = "formulas")] 39 | pub mod formulas; 40 | 41 | #[cfg(feature = "forces")] 42 | pub mod forces; 43 | 44 | #[cfg(feature = "event_loop")] 45 | pub mod event_loop; 46 | 47 | #[cfg(feature = "collision")] 48 | pub mod collision; 49 | -------------------------------------------------------------------------------- /src/obj/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod obj_2d; 2 | -------------------------------------------------------------------------------- /src/obj/obj_2d.rs: -------------------------------------------------------------------------------- 1 | use crate::{ 2 | collision::shape::Shape, 3 | forces::force::Force, 4 | vec::vec_2d::{vec2, Vec2d}, 5 | }; 6 | 7 | /// Represents a 2D object with physical properties and shape. 8 | #[derive(Debug, Clone, Copy)] 9 | pub struct Object2d { 10 | /// The position vector of the object. 11 | pub vec: Vec2d, 12 | /// The density of the object. 13 | pub density: f32, 14 | /// The mass of the object. 15 | pub mass: f32, 16 | /// The velocity vector of the object. 17 | pub velocity: Vec2d, 18 | /// The acceleration vector of the object. 19 | pub acceleration: Vec2d, 20 | /// The radius of the object, if applicable. 21 | pub radius: f32, 22 | /// The shape of the object. 23 | pub shape: Shape, 24 | } 25 | 26 | impl Object2d { 27 | /// Creates a new `Object2d` with the specified properties. 28 | /// 29 | /// # Parameters 30 | /// 31 | /// - `vec`: The position vector of the object. 32 | /// - `density`: The density of the object. 33 | /// - `mass`: The mass of the object. 34 | /// - `velocity`: The velocity vector of the object. 35 | /// - `acceleration`: The acceleration vector of the object. 36 | /// - `radius`: The radius of the object. 37 | /// - `shape`: The shape of the object. 38 | /// 39 | /// # Returns 40 | /// 41 | /// A new `Object2d` instance. 42 | /// 43 | /// # Examples 44 | /// 45 | /// ``` 46 | /// let obj = Object2d::new(vec2(1.0, 2.0), 1.0, 1.0, vec2(0.0, 0.0), vec2(0.0, 0.0), 1.0, Shape::Circle); 47 | /// ``` 48 | #[must_use] 49 | pub fn new( 50 | vec: Vec2d, 51 | density: f32, 52 | mass: f32, 53 | velocity: Vec2d, 54 | acceleration: Vec2d, 55 | radius: f32, 56 | shape: Shape, 57 | ) -> Self { 58 | Object2d { 59 | vec, 60 | density, 61 | mass, 62 | velocity, 63 | acceleration, 64 | radius, 65 | shape, 66 | } 67 | } 68 | 69 | /// Applies a force to the object. 70 | /// 71 | /// # Parameters 72 | /// 73 | /// - `force`: The force to apply. 74 | /// 75 | /// # Examples 76 | /// 77 | /// ``` 78 | /// let mut obj = Object2d::new(vec2(1.0, 2.0), 1.0, 1.0, vec2(0.0, 0.0), vec2(0.0, 0.0), 1.0, Shape::Circle); 79 | /// let gravity = Gravity::new(EARTH_GRAVITY, 1.0, 0.016); 80 | /// obj.apply(gravity); 81 | /// ``` 82 | pub fn apply(&mut self, force: &T) { 83 | force.apply_2d(self); 84 | } 85 | } 86 | 87 | /// Creates a new `Object2d` instance with the specified properties using a more convenient syntax. 88 | /// 89 | /// # Parameters 90 | /// 91 | /// - `vec`: The position vector of the object. 92 | /// - `density`: The density of the object. 93 | /// - `mass`: The mass of the object. 94 | /// - `velocity`: The velocity vector of the object. 95 | /// - `acceleration`: The acceleration vector of the object. 96 | /// - `radius`: The radius of the object. 97 | /// - `shape`: The shape of the object. 98 | /// 99 | /// # Returns 100 | /// 101 | /// A new `Object2d` instance. 102 | /// 103 | /// # Examples 104 | /// 105 | /// ``` 106 | /// let obj = obj2d(vec2(1.0, 2.0), 1.0, 1.0, vec2(0.0, 0.0), vec2(0.0, 0.0), 1.0, Shape::Circle); 107 | /// ``` 108 | #[deprecated(since = "0.1.3", note = "Use the Object2dBuilder instead")] 109 | #[inline] 110 | pub fn obj2d>( 111 | vec: V, 112 | density: f32, 113 | mass: f32, 114 | velocity: V, 115 | acceleration: V, 116 | radius: f32, 117 | shape: Shape, 118 | ) -> Object2d { 119 | Object2d::new( 120 | vec.into(), 121 | density, 122 | mass, 123 | velocity.into(), 124 | acceleration.into(), 125 | radius, 126 | shape, 127 | ) 128 | } 129 | 130 | /// `Object2dBuilder` is a structure used for building `Object2d` instances. 131 | /// 132 | /// This builder pattern allows for a more readable and organized way to create `Object2d` 133 | /// objects by setting various fields in a fluid and chainable manner. 134 | #[derive(Debug)] 135 | pub struct Object2dBuilder { 136 | /// The position vector of the object. 137 | position: Vec2d, 138 | 139 | /// The density of the object. 140 | density: f32, 141 | 142 | /// The mass of the object. 143 | mass: f32, 144 | 145 | /// The velocity vector of the object. 146 | velocity: Vec2d, 147 | 148 | /// The acceleration vector of the object. 149 | acceleration: Vec2d, 150 | 151 | /// The radius of the object. 152 | radius: f32, 153 | 154 | /// The shape of the object. 155 | shape: Shape, 156 | } 157 | 158 | impl Object2dBuilder { 159 | #[must_use] 160 | pub fn new() -> Self { 161 | Object2dBuilder { 162 | position: vec2(0.0, 0.0), 163 | density: 0.0, 164 | mass: 0.0, 165 | velocity: vec2(0.0, 0.0), 166 | acceleration: vec2(0.0, 0.0), 167 | radius: 0.0, 168 | shape: Shape::None, 169 | } 170 | } 171 | #[must_use] 172 | pub fn position(mut self, vec: Vec2d) -> Self { 173 | self.position = vec; 174 | self 175 | } 176 | 177 | #[must_use] 178 | pub fn density(mut self, density: f32) -> Self { 179 | self.density = density; 180 | self 181 | } 182 | 183 | #[must_use] 184 | pub fn mass(mut self, mass: f32) -> Self { 185 | self.mass = mass; 186 | self 187 | } 188 | 189 | #[must_use] 190 | pub fn velocity(mut self, vec: Vec2d) -> Self { 191 | self.velocity = vec; 192 | self 193 | } 194 | 195 | #[must_use] 196 | pub fn acceleration(mut self, vec: Vec2d) -> Self { 197 | self.acceleration = vec; 198 | self 199 | } 200 | 201 | #[must_use] 202 | pub fn radius(mut self, radius: f32) -> Self { 203 | self.radius = radius; 204 | self 205 | } 206 | 207 | #[must_use] 208 | pub fn shape(mut self, shape: Shape) -> Self { 209 | self.shape = shape; 210 | self 211 | } 212 | #[must_use] 213 | pub fn build(self) -> Object2d { 214 | Object2d { 215 | vec: self.position, 216 | density: self.density, 217 | mass: self.mass, 218 | velocity: self.velocity, 219 | acceleration: self.acceleration, 220 | radius: self.radius, 221 | shape: self.shape, 222 | } 223 | } 224 | } 225 | 226 | impl Default for Object2dBuilder { 227 | fn default() -> Self { 228 | Self::new() 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /src/vec/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod vec_2d; 2 | -------------------------------------------------------------------------------- /src/vec/vec_2d.rs: -------------------------------------------------------------------------------- 1 | use crate::formulas::dot::length; 2 | use core::ops::{Add, Div, Mul, Sub}; 3 | 4 | /// `Vec2d` is a simple 2D vector struct used for various vector operations. 5 | /// 6 | /// # Examples 7 | /// 8 | /// ```rust 9 | /// 10 | /// let v1 = Vec2d::new(3.0, 4.0); 11 | /// let v2 = Vec2d::new(6.0, 8.0); 12 | /// 13 | /// assert_eq!(v1.magnitude(), 5.0); 14 | /// assert_eq!(v1.distance(v2), 5.0); 15 | /// ``` 16 | #[derive(Debug, Clone, Copy, PartialEq)] 17 | pub struct Vec2d { 18 | /// The x component of the vector 19 | pub x: f32, 20 | /// The y component of the vector 21 | pub y: f32, 22 | } 23 | 24 | impl Vec2d { 25 | /// Creates a new `Vec2d` with the given x and y components. 26 | /// 27 | /// # Examples 28 | /// 29 | /// ``` 30 | /// let vec = Vec2d::new(1.0, 2.0); 31 | /// assert_eq!(vec.x, 1.0); 32 | /// assert_eq!(vec.y, 2.0); 33 | /// ``` 34 | #[must_use] 35 | pub fn new(x: f32, y: f32) -> Self { 36 | Vec2d { x, y } 37 | } 38 | 39 | /// Computes the component vector from this vector to the target vector. 40 | /// 41 | /// # Examples 42 | /// 43 | /// ``` 44 | /// let v1 = Vec2d::new(1.0, 2.0); 45 | /// let v2 = Vec2d::new(4.0, 6.0); 46 | /// let component = v1.component(v2); 47 | /// assert_eq!(component.x, 3.0); 48 | /// assert_eq!(component.y, 4.0); 49 | /// ``` 50 | #[must_use] 51 | pub fn component(&self, target: Vec2d) -> Vec2d { 52 | Vec2d { 53 | x: (target.x - self.x), 54 | y: (target.y - self.y), 55 | } 56 | } 57 | 58 | /// Computes the magnitude (length) of the vector. 59 | /// 60 | /// # Examples 61 | /// 62 | /// ``` 63 | /// let v = Vec2d::new(3.0, 4.0); 64 | /// assert_eq!(v.magnitude(), 5.0); 65 | /// ``` 66 | #[must_use] 67 | pub fn magnitude(&self) -> f32 { 68 | length(*self) 69 | } 70 | 71 | /// Computes the distance between this vector and the target vector. 72 | /// 73 | /// # Examples 74 | /// 75 | /// ``` 76 | /// let v1 = Vec2d::new(1.0, 2.0); 77 | /// let v2 = Vec2d::new(4.0, 6.0); 78 | /// assert_eq!(v1.distance(v2), 5.0); 79 | /// ``` 80 | #[must_use] 81 | pub fn distance(&self, target: Vec2d) -> f32 { 82 | let ab = self.component(target); 83 | length(ab) 84 | } 85 | } 86 | 87 | /// Implements the addition of two 2D vectors. 88 | /// 89 | /// This implementation allows using the `+` operator to add two `Vec2d` vectors component-wise. 90 | /// For example, `Vec2d { x: 1.0, y: 2.0 } + Vec2d { x: 3.0, y: 4.0 }` results in `Vec2d { x: 4.0, y: 6.0 }`. 91 | impl Add for Vec2d { 92 | type Output = Vec2d; 93 | 94 | /// Adds two `Vec2d` vectors. 95 | /// 96 | /// # Parameters 97 | /// - `self`: The left-hand side vector in the addition. 98 | /// - `rhs`: The right-hand side vector in the addition. 99 | /// 100 | /// # Returns 101 | /// A new `Vec2d` vector where each component is the sum of the corresponding components of the input vectors. 102 | fn add(self, rhs: Self) -> Self::Output { 103 | Vec2d { 104 | x: self.x + rhs.x, 105 | y: self.y + rhs.y, 106 | } 107 | } 108 | } 109 | 110 | /// Implements the subtraction of two 2D vectors. 111 | /// 112 | /// This implementation allows using the `-` operator to subtract one `Vec2d` vector from another component-wise. 113 | /// For example, `Vec2d { x: 5.0, y: 7.0 } - Vec2d { x: 3.0, y: 2.0 }` results in `Vec2d { x: 2.0, y: 5.0 }`. 114 | impl Sub for Vec2d { 115 | type Output = Vec2d; 116 | 117 | /// Subtracts one `Vec2d` vector from another. 118 | /// 119 | /// # Parameters 120 | /// - `self`: The left-hand side vector in the subtraction. 121 | /// - `rhs`: The right-hand side vector in the subtraction. 122 | /// 123 | /// # Returns 124 | /// A new `Vec2d` vector where each component is the difference of the corresponding components of the input vectors. 125 | fn sub(self, rhs: Self) -> Self::Output { 126 | Vec2d { 127 | x: self.x - rhs.x, 128 | y: self.y - rhs.y, 129 | } 130 | } 131 | } 132 | 133 | /// Implements the dot product of two 2D vectors. 134 | /// 135 | /// This implementation allows using the `*` operator to compute the dot product of two `Vec2d` vectors. 136 | /// The result is a scalar value representing the dot product. 137 | /// For example, `Vec2d { x: 1.0, y: 2.0 } * Vec2d { x: 3.0, y: 4.0 }` results in `11.0`. 138 | impl Mul for Vec2d { 139 | type Output = f32; 140 | 141 | /// Computes the dot product of two `Vec2d` vectors. 142 | /// 143 | /// # Parameters 144 | /// - `self`: The left-hand side vector in the multiplication. 145 | /// - `rhs`: The right-hand side vector in the multiplication. 146 | /// 147 | /// # Returns 148 | /// A `f32` scalar value representing the dot product of the two input vectors. 149 | fn mul(self, rhs: Self) -> Self::Output { 150 | self.x * rhs.x + self.y * rhs.y 151 | } 152 | } 153 | 154 | /// Implements scalar division for 2D vectors. 155 | /// 156 | /// This implementation allows using the `/` operator to divide each component of a `Vec2d` vector by a scalar value of type `f32`. 157 | /// For example, `Vec2d { x: 10.0, y: 20.0 } / 2.0` results in `Vec2d { x: 5.0, y: 10.0 }`. 158 | impl Div for Vec2d { 159 | type Output = Vec2d; 160 | 161 | /// Divides each component of a `Vec2d` vector by a scalar value. 162 | /// 163 | /// # Parameters 164 | /// - `self`: The vector to be divided. 165 | /// - `rhs`: The scalar value to divide each component by. 166 | /// 167 | /// # Returns 168 | /// A new `Vec2d` vector where each component is the result of the division of the corresponding component of the input vector by the scalar. 169 | fn div(self, rhs: f32) -> Self::Output { 170 | Vec2d { 171 | x: self.x / rhs, 172 | y: self.y / rhs, 173 | } 174 | } 175 | } 176 | 177 | /// A convenience function to create a new `Vec2d`. 178 | /// 179 | /// # Examples 180 | /// 181 | /// ``` 182 | /// let vec = vec2(1.0, 2.0); 183 | /// assert_eq!(vec.x, 1.0); 184 | /// assert_eq!(vec.y, 2.0); 185 | /// ``` 186 | #[inline] 187 | pub fn vec2, B: Into>(a: A, b: B) -> Vec2d { 188 | Vec2d::new(a.into(), b.into()) 189 | } 190 | --------------------------------------------------------------------------------