From 754a48b7ff6d8c58b1ee08651e60112900b60455 Mon Sep 17 00:00:00 2001 From: Sébastien Crozet Date: Tue, 25 Aug 2020 22:10:25 +0200 Subject: First public release of Rapier. --- .github/FUNDING.yml | 12 + .gitignore | 8 + Cargo.toml | 12 + LICENSE | 201 +++ README.md | 30 + build/rapier2d/Cargo.toml | 53 + build/rapier3d/Cargo.toml | 53 + build/rapier_testbed2d/Cargo.toml | 45 + build/rapier_testbed3d/Cargo.toml | 47 + examples2d/Cargo.toml | 27 + examples2d/all_examples2.rs | 90 ++ examples2d/balls2.rs | 68 + examples2d/boxes2.rs | 73 + examples2d/capsules2.rs | 76 + examples2d/debug_box_ball2.rs | 44 + examples2d/heightfield2.rs | 72 + examples2d/joints2.rs | 75 + examples2d/kinematic2.rs | 93 ++ examples2d/pyramid2.rs | 60 + examples2d/sensor2.rs | 101 ++ examples2d/stress_joint_ball2.rs | 72 + examples2d/stress_joint_fixed2.rs | 85 + examples2d/stress_joint_prismatic2.rs | 69 + examples3d/Cargo.toml | 27 + examples3d/all_examples3.rs | 110 ++ examples3d/balls3.rs | 29 + examples3d/boxes3.rs | 68 + examples3d/capsules3.rs | 69 + examples3d/debug_boxes3.rs | 47 + examples3d/debug_triangle3.rs | 48 + examples3d/domino3.rs | 87 ++ examples3d/heightfield3.rs | 78 + examples3d/joints3.rs | 272 ++++ examples3d/kinematic3.rs | 97 ++ examples3d/pyramid3.rs | 85 + examples3d/sensor3.rs | 105 ++ examples3d/stacks3.rs | 195 +++ examples3d/stress_joint_ball3.rs | 70 + examples3d/stress_joint_fixed3.rs | 92 ++ examples3d/stress_joint_prismatic3.rs | 81 + examples3d/stress_joint_revolute3.rs | 89 ++ examples3d/stress_keva3.rs | 148 ++ examples3d/trimesh3.rs | 88 ++ src/counters/ccd_counters.rs | 49 + src/counters/collision_detection_counters.rs | 32 + src/counters/mod.rs | 225 +++ src/counters/solver_counters.rs | 67 + src/counters/stages_counters.rs | 48 + src/counters/timer.rs | 53 + src/data/arena.rs | 1159 ++++++++++++++ src/data/graph.rs | 830 ++++++++++ src/data/mod.rs | 4 + src/dynamics/integration_parameters.rs | 207 +++ src/dynamics/joint/ball_joint.rs | 34 + src/dynamics/joint/fixed_joint.rs | 33 + src/dynamics/joint/joint.rs | 112 ++ src/dynamics/joint/joint_set.rs | 218 +++ src/dynamics/joint/mod.rs | 16 + src/dynamics/joint/prismatic_joint.rs | 193 +++ src/dynamics/joint/revolute_joint.rs | 46 + src/dynamics/mass_properties.rs | 206 +++ src/dynamics/mass_properties_ball.rs | 30 + src/dynamics/mass_properties_capsule.rs | 60 + src/dynamics/mass_properties_cuboid.rs | 33 + src/dynamics/mass_properties_polygon.rs | 144 ++ src/dynamics/mod.rs | 30 + src/dynamics/rigid_body.rs | 441 ++++++ src/dynamics/rigid_body_set.rs | 518 ++++++ src/dynamics/solver/categorization.rs | 70 + src/dynamics/solver/delta_vel.rs | 18 + src/dynamics/solver/interaction_groups.rs | 434 +++++ src/dynamics/solver/island_solver.rs | 73 + .../joint_constraint/ball_position_constraint.rs | 165 ++ .../ball_position_constraint_wide.rs | 199 +++ .../joint_constraint/ball_velocity_constraint.rs | 238 +++ .../ball_velocity_constraint_wide.rs | 329 ++++ .../joint_constraint/fixed_position_constraint.rs | 139 ++ .../joint_constraint/fixed_velocity_constraint.rs | 370 +++++ .../fixed_velocity_constraint_wide.rs | 472 ++++++ .../solver/joint_constraint/joint_constraint.rs | 340 ++++ .../joint_constraint/joint_position_constraint.rs | 169 ++ src/dynamics/solver/joint_constraint/mod.rs | 65 + .../prismatic_position_constraint.rs | 170 ++ .../prismatic_velocity_constraint.rs | 558 +++++++ .../prismatic_velocity_constraint_wide.rs | 687 ++++++++ .../revolute_position_constraint.rs | 142 ++ .../revolute_velocity_constraint.rs | 294 ++++ .../revolute_velocity_constraint_wide.rs | 397 +++++ src/dynamics/solver/mod.rs | 56 + src/dynamics/solver/parallel_island_solver.rs | 259 +++ src/dynamics/solver/parallel_position_solver.rs | 582 +++++++ src/dynamics/solver/parallel_velocity_solver.rs | 485 ++++++ src/dynamics/solver/position_constraint.rs | 246 +++ src/dynamics/solver/position_constraint_wide.rs | 217 +++ src/dynamics/solver/position_ground_constraint.rs | 189 +++ .../solver/position_ground_constraint_wide.rs | 199 +++ src/dynamics/solver/position_solver.rs | 451 ++++++ src/dynamics/solver/velocity_constraint.rs | 401 +++++ src/dynamics/solver/velocity_constraint_wide.rs | 344 ++++ src/dynamics/solver/velocity_ground_constraint.rs | 300 ++++ .../solver/velocity_ground_constraint_wide.rs | 300 ++++ src/dynamics/solver/velocity_solver.rs | 405 +++++ src/geometry/ball.rs | 16 + src/geometry/broad_phase.rs | 255 +++ src/geometry/broad_phase_multi_sap.rs | 645 ++++++++ src/geometry/capsule.rs | 168 ++ src/geometry/collider.rs | 373 +++++ src/geometry/collider_set.rs | 139 ++ src/geometry/contact.rs | 485 ++++++ .../ball_ball_contact_generator.rs | 98 ++ .../ball_convex_contact_generator.rs | 85 + .../ball_polygon_contact_generator.rs | 0 .../capsule_capsule_contact_generator.rs | 200 +++ .../contact_generator/contact_dispatcher.rs | 127 ++ .../contact_generator/contact_generator.rs | 222 +++ .../cuboid_capsule_contact_generator.rs | 188 +++ .../cuboid_cuboid_contact_generator.rs | 155 ++ .../cuboid_polygon_contact_generator.rs | 0 .../cuboid_triangle_contact_generator.rs | 171 ++ .../heightfield_shape_contact_generator.rs | 189 +++ src/geometry/contact_generator/mod.rs | 71 + .../polygon_polygon_contact_generator.rs | 298 ++++ .../trimesh_shape_contact_generator.rs | 194 +++ .../voxels_shape_contact_generator.rs | 0 src/geometry/cuboid.rs | 234 +++ src/geometry/cuboid_feature2d.rs | 128 ++ src/geometry/cuboid_feature3d.rs | 516 ++++++ src/geometry/interaction_graph.rs | 259 +++ src/geometry/mod.rs | 80 + src/geometry/narrow_phase.rs | 483 ++++++ src/geometry/polygon.rs | 76 + src/geometry/polygon_intersection.rs | 263 ++++ src/geometry/polyhedron_feature3d.rs | 284 ++++ src/geometry/proximity.rs | 31 + .../ball_ball_proximity_detector.rs | 68 + .../ball_convex_proximity_detector.rs | 53 + .../ball_polygon_proximity_detector.rs | 0 .../cuboid_cuboid_proximity_detector.rs | 79 + .../cuboid_polygon_proximity_detector.rs | 0 .../cuboid_triangle_proximity_detector.rs | 88 ++ src/geometry/proximity_detector/mod.rs | 30 + .../polygon_polygon_proximity_detector.rs | 54 + .../proximity_detector/proximity_detector.rs | 212 +++ .../proximity_detector/proximity_dispatcher.rs | 136 ++ .../trimesh_shape_proximity_detector.rs | 133 ++ .../voxels_shape_proximity_detector.rs | 0 src/geometry/sat.rs | 294 ++++ src/geometry/triangle.rs | 9 + src/geometry/trimesh.rs | 122 ++ src/geometry/waabb.rs | 116 ++ src/geometry/z_order.rs | 70 + src/lib.rs | 255 +++ src/pipeline/collision_pipeline.rs | 111 ++ src/pipeline/event_handler.rs | 51 + src/pipeline/mod.rs | 9 + src/pipeline/physics_pipeline.rs | 332 ++++ src/utils.rs | 1333 ++++++++++++++++ src_testbed/Inconsolata.otf | Bin 0 -> 58464 bytes src_testbed/box2d_backend.rs | 237 +++ src_testbed/engine.rs | 694 ++++++++ src_testbed/lib.rs | 52 + src_testbed/nphysics_backend.rs | 212 +++ src_testbed/objects/ball.rs | 73 + src_testbed/objects/box_node.rs | 73 + src_testbed/objects/capsule.rs | 74 + src_testbed/objects/convex.rs | 77 + src_testbed/objects/heightfield.rs | 120 ++ src_testbed/objects/mesh.rs | 108 ++ src_testbed/objects/mod.rs | 10 + src_testbed/objects/node.rs | 164 ++ src_testbed/objects/plane.rs | 132 ++ src_testbed/objects/polyline.rs | 79 + src_testbed/physx_backend.rs | 604 +++++++ src_testbed/testbed.rs | 1652 ++++++++++++++++++++ src_testbed/ui.rs | 568 +++++++ 175 files changed, 32819 insertions(+) create mode 100644 .github/FUNDING.yml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build/rapier2d/Cargo.toml create mode 100644 build/rapier3d/Cargo.toml create mode 100644 build/rapier_testbed2d/Cargo.toml create mode 100644 build/rapier_testbed3d/Cargo.toml create mode 100644 examples2d/Cargo.toml create mode 100644 examples2d/all_examples2.rs create mode 100644 examples2d/balls2.rs create mode 100644 examples2d/boxes2.rs create mode 100644 examples2d/capsules2.rs create mode 100644 examples2d/debug_box_ball2.rs create mode 100644 examples2d/heightfield2.rs create mode 100644 examples2d/joints2.rs create mode 100644 examples2d/kinematic2.rs create mode 100644 examples2d/pyramid2.rs create mode 100644 examples2d/sensor2.rs create mode 100644 examples2d/stress_joint_ball2.rs create mode 100644 examples2d/stress_joint_fixed2.rs create mode 100644 examples2d/stress_joint_prismatic2.rs create mode 100644 examples3d/Cargo.toml create mode 100644 examples3d/all_examples3.rs create mode 100644 examples3d/balls3.rs create mode 100644 examples3d/boxes3.rs create mode 100644 examples3d/capsules3.rs create mode 100644 examples3d/debug_boxes3.rs create mode 100644 examples3d/debug_triangle3.rs create mode 100644 examples3d/domino3.rs create mode 100644 examples3d/heightfield3.rs create mode 100644 examples3d/joints3.rs create mode 100644 examples3d/kinematic3.rs create mode 100644 examples3d/pyramid3.rs create mode 100644 examples3d/sensor3.rs create mode 100644 examples3d/stacks3.rs create mode 100644 examples3d/stress_joint_ball3.rs create mode 100644 examples3d/stress_joint_fixed3.rs create mode 100644 examples3d/stress_joint_prismatic3.rs create mode 100644 examples3d/stress_joint_revolute3.rs create mode 100644 examples3d/stress_keva3.rs create mode 100644 examples3d/trimesh3.rs create mode 100644 src/counters/ccd_counters.rs create mode 100644 src/counters/collision_detection_counters.rs create mode 100644 src/counters/mod.rs create mode 100644 src/counters/solver_counters.rs create mode 100644 src/counters/stages_counters.rs create mode 100644 src/counters/timer.rs create mode 100644 src/data/arena.rs create mode 100644 src/data/graph.rs create mode 100644 src/data/mod.rs create mode 100644 src/dynamics/integration_parameters.rs create mode 100644 src/dynamics/joint/ball_joint.rs create mode 100644 src/dynamics/joint/fixed_joint.rs create mode 100644 src/dynamics/joint/joint.rs create mode 100644 src/dynamics/joint/joint_set.rs create mode 100644 src/dynamics/joint/mod.rs create mode 100644 src/dynamics/joint/prismatic_joint.rs create mode 100644 src/dynamics/joint/revolute_joint.rs create mode 100644 src/dynamics/mass_properties.rs create mode 100644 src/dynamics/mass_properties_ball.rs create mode 100644 src/dynamics/mass_properties_capsule.rs create mode 100644 src/dynamics/mass_properties_cuboid.rs create mode 100644 src/dynamics/mass_properties_polygon.rs create mode 100644 src/dynamics/mod.rs create mode 100644 src/dynamics/rigid_body.rs create mode 100644 src/dynamics/rigid_body_set.rs create mode 100644 src/dynamics/solver/categorization.rs create mode 100644 src/dynamics/solver/delta_vel.rs create mode 100644 src/dynamics/solver/interaction_groups.rs create mode 100644 src/dynamics/solver/island_solver.rs create mode 100644 src/dynamics/solver/joint_constraint/ball_position_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/ball_position_constraint_wide.rs create mode 100644 src/dynamics/solver/joint_constraint/ball_velocity_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/ball_velocity_constraint_wide.rs create mode 100644 src/dynamics/solver/joint_constraint/fixed_position_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/fixed_velocity_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/fixed_velocity_constraint_wide.rs create mode 100644 src/dynamics/solver/joint_constraint/joint_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/joint_position_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/mod.rs create mode 100644 src/dynamics/solver/joint_constraint/prismatic_position_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/prismatic_velocity_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/prismatic_velocity_constraint_wide.rs create mode 100644 src/dynamics/solver/joint_constraint/revolute_position_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/revolute_velocity_constraint.rs create mode 100644 src/dynamics/solver/joint_constraint/revolute_velocity_constraint_wide.rs create mode 100644 src/dynamics/solver/mod.rs create mode 100644 src/dynamics/solver/parallel_island_solver.rs create mode 100644 src/dynamics/solver/parallel_position_solver.rs create mode 100644 src/dynamics/solver/parallel_velocity_solver.rs create mode 100644 src/dynamics/solver/position_constraint.rs create mode 100644 src/dynamics/solver/position_constraint_wide.rs create mode 100644 src/dynamics/solver/position_ground_constraint.rs create mode 100644 src/dynamics/solver/position_ground_constraint_wide.rs create mode 100644 src/dynamics/solver/position_solver.rs create mode 100644 src/dynamics/solver/velocity_constraint.rs create mode 100644 src/dynamics/solver/velocity_constraint_wide.rs create mode 100644 src/dynamics/solver/velocity_ground_constraint.rs create mode 100644 src/dynamics/solver/velocity_ground_constraint_wide.rs create mode 100644 src/dynamics/solver/velocity_solver.rs create mode 100644 src/geometry/ball.rs create mode 100644 src/geometry/broad_phase.rs create mode 100644 src/geometry/broad_phase_multi_sap.rs create mode 100644 src/geometry/capsule.rs create mode 100644 src/geometry/collider.rs create mode 100644 src/geometry/collider_set.rs create mode 100644 src/geometry/contact.rs create mode 100644 src/geometry/contact_generator/ball_ball_contact_generator.rs create mode 100644 src/geometry/contact_generator/ball_convex_contact_generator.rs create mode 100644 src/geometry/contact_generator/ball_polygon_contact_generator.rs create mode 100644 src/geometry/contact_generator/capsule_capsule_contact_generator.rs create mode 100644 src/geometry/contact_generator/contact_dispatcher.rs create mode 100644 src/geometry/contact_generator/contact_generator.rs create mode 100644 src/geometry/contact_generator/cuboid_capsule_contact_generator.rs create mode 100644 src/geometry/contact_generator/cuboid_cuboid_contact_generator.rs create mode 100644 src/geometry/contact_generator/cuboid_polygon_contact_generator.rs create mode 100644 src/geometry/contact_generator/cuboid_triangle_contact_generator.rs create mode 100644 src/geometry/contact_generator/heightfield_shape_contact_generator.rs create mode 100644 src/geometry/contact_generator/mod.rs create mode 100644 src/geometry/contact_generator/polygon_polygon_contact_generator.rs create mode 100644 src/geometry/contact_generator/trimesh_shape_contact_generator.rs create mode 100644 src/geometry/contact_generator/voxels_shape_contact_generator.rs create mode 100644 src/geometry/cuboid.rs create mode 100644 src/geometry/cuboid_feature2d.rs create mode 100644 src/geometry/cuboid_feature3d.rs create mode 100644 src/geometry/interaction_graph.rs create mode 100644 src/geometry/mod.rs create mode 100644 src/geometry/narrow_phase.rs create mode 100644 src/geometry/polygon.rs create mode 100644 src/geometry/polygon_intersection.rs create mode 100644 src/geometry/polyhedron_feature3d.rs create mode 100644 src/geometry/proximity.rs create mode 100644 src/geometry/proximity_detector/ball_ball_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/ball_convex_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/ball_polygon_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/cuboid_cuboid_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/cuboid_polygon_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/cuboid_triangle_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/mod.rs create mode 100644 src/geometry/proximity_detector/polygon_polygon_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/proximity_detector.rs create mode 100644 src/geometry/proximity_detector/proximity_dispatcher.rs create mode 100644 src/geometry/proximity_detector/trimesh_shape_proximity_detector.rs create mode 100644 src/geometry/proximity_detector/voxels_shape_proximity_detector.rs create mode 100644 src/geometry/sat.rs create mode 100644 src/geometry/triangle.rs create mode 100644 src/geometry/trimesh.rs create mode 100644 src/geometry/waabb.rs create mode 100644 src/geometry/z_order.rs create mode 100644 src/lib.rs create mode 100644 src/pipeline/collision_pipeline.rs create mode 100644 src/pipeline/event_handler.rs create mode 100644 src/pipeline/mod.rs create mode 100644 src/pipeline/physics_pipeline.rs create mode 100644 src/utils.rs create mode 100644 src_testbed/Inconsolata.otf create mode 100644 src_testbed/box2d_backend.rs create mode 100644 src_testbed/engine.rs create mode 100644 src_testbed/lib.rs create mode 100644 src_testbed/nphysics_backend.rs create mode 100644 src_testbed/objects/ball.rs create mode 100644 src_testbed/objects/box_node.rs create mode 100644 src_testbed/objects/capsule.rs create mode 100644 src_testbed/objects/convex.rs create mode 100644 src_testbed/objects/heightfield.rs create mode 100644 src_testbed/objects/mesh.rs create mode 100644 src_testbed/objects/mod.rs create mode 100644 src_testbed/objects/node.rs create mode 100644 src_testbed/objects/plane.rs create mode 100644 src_testbed/objects/polyline.rs create mode 100644 src_testbed/physx_backend.rs create mode 100644 src_testbed/testbed.rs create mode 100644 src_testbed/ui.rs diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..5ee5fec --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,12 @@ +# These are supported funding model platforms + +github: [ "dimforge" ] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: # Replace with a single Patreon username +open_collective: # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8c2e33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +**/*.rs.bk +Cargo.lock +node_modules +target +.idea +.DS_Store +package-lock.json +**/*.csv \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cf89018 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[workspace] +members = [ "build/rapier2d", "build/rapier_testbed2d", "examples2d", "examples2d/wasm", + "build/rapier3d", "build/rapier_testbed3d", "examples3d", "examples3d/wasm",] + +[patch.crates-io] +#wrapped2d = { git = "https://github.com/Bastacyclop/rust_box2d.git" } + +[profile.release] +debug = false +codegen-units = 1 +#opt-level = 1 +#lto = true diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..97f4383 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2020 Sébastien Crozet + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ce06b0 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +

+ crates.io +

+

+ + + + + Build status + + + crates.io + + + + +

+

+ + Website | Documentation +

+ +----- + +

+2D and 3D physics engines +for the Rust programming language. +

+ +----- diff --git a/build/rapier2d/Cargo.toml b/build/rapier2d/Cargo.toml new file mode 100644 index 0000000..8afa443 --- /dev/null +++ b/build/rapier2d/Cargo.toml @@ -0,0 +1,53 @@ +# Name idea: bident for 2D and trident for 3D +[package] +name = "rapier2d" +version = "0.1.0" +authors = [ "Sébastien Crozet " ] +description = "2-dimensional physics engine in Rust." +documentation = "http://rapier.rs/rustdoc/rapier2d/index.html" +homepage = "http://rapier.rs" +repository = "https://github.com/rustsim/rapier" +readme = "README.md" +keywords = [ "physics", "dynamics", "rigid", "real-time", "joints" ] +license = "Apache-2.0" +edition = "2018" + +[features] +default = [ "dim2" ] +dim2 = [ ] +parallel = [ "rayon" ] +simd-stable = [ "simba/wide", "simd-is-enabled" ] +simd-nightly = [ "simba/packed_simd", "simd-is-enabled" ] +# Do not enable this feature directly. It is automatically +# enabled with the "simd-stable" or "simd-nightly" feature. +simd-is-enabled = [ ] +wasm-bindgen = [ "instant/wasm-bindgen" ] +serde-serialize = [ "nalgebra/serde-serialize", "ncollide2d/serde-serialize", "serde", "generational-arena/serde", "bit-vec/serde", "arrayvec/serde" ] +enhanced-determinism = [ "simba/libm_force", "indexmap" ] + +[lib] +name = "rapier2d" +path = "../../src/lib.rs" +required-features = [ "dim2" ] + + +[dependencies] +vec_map = "0.8" +instant = { version = "0.1", features = [ "now" ]} +num-traits = "0.2" +nalgebra = "0.22" +ncollide2d = "0.24" +simba = "0.2" +approx = "0.3" +rayon = { version = "1", optional = true } +crossbeam = "0.7" +generational-arena = "0.2" +arrayvec = "0.5" +bit-vec = "0.6" +rustc-hash = "1" +serde = { version = "1", features = [ "derive" ], optional = true } +indexmap = { version = "1", features = [ "serde-1" ], optional = true } + +[dev-dependencies] +bincode = "1" +serde = { version = "1", features = [ "derive" ] } diff --git a/build/rapier3d/Cargo.toml b/build/rapier3d/Cargo.toml new file mode 100644 index 0000000..00eb77d --- /dev/null +++ b/build/rapier3d/Cargo.toml @@ -0,0 +1,53 @@ +# Name idea: bident for 2D and trident for 3D +[package] +name = "rapier3d" +version = "0.1.0" +authors = [ "Sébastien Crozet " ] +description = "3-dimensional physics engine in Rust." +documentation = "http://rapier.rs/rustdoc/rapier2d/index.html" +homepage = "http://rapier.rs" +repository = "https://github.com/rustsim/rapier" +readme = "README.md" +keywords = [ "physics", "dynamics", "rigid", "real-time", "joints" ] +license = "Apache-2.0" +edition = "2018" + +[features] +default = [ "dim3" ] +dim3 = [ ] +parallel = [ "rayon" ] +simd-stable = [ "simba/wide", "simd-is-enabled" ] +simd-nightly = [ "simba/packed_simd", "simd-is-enabled" ] +# Do not enable this feature directly. It is automatically +# enabled with the "simd-stable" or "simd-nightly" feature. +simd-is-enabled = [ ] +wasm-bindgen = [ "instant/wasm-bindgen" ] +serde-serialize = [ "nalgebra/serde-serialize", "ncollide3d/serde-serialize", "serde", "generational-arena/serde", "bit-vec/serde" ] +enhanced-determinism = [ "simba/libm_force", "indexmap" ] + +[lib] +name = "rapier3d" +path = "../../src/lib.rs" +required-features = [ "dim3" ] + + +[dependencies] +vec_map = "0.8" +instant = { version = "0.1", features = [ "now" ]} +num-traits = "0.2" +nalgebra = "0.22" +ncollide3d = "0.24" +simba = "0.2" +approx = "0.3" +rayon = { version = "1", optional = true } +crossbeam = "0.7" +generational-arena = "0.2" +arrayvec = "0.5" +bit-vec = "0.6" +rustc-hash = "1" +serde = { version = "1", features = [ "derive" ], optional = true } +indexmap = { version = "1", features = [ "serde-1" ], optional = true } + +[dev-dependencies] +bincode = "1" +serde = { version = "1", features = [ "derive" ] } diff --git a/build/rapier_testbed2d/Cargo.toml b/build/rapier_testbed2d/Cargo.toml new file mode 100644 index 0000000..b4e82a2 --- /dev/null +++ b/build/rapier_testbed2d/Cargo.toml @@ -0,0 +1,45 @@ +[package] +name = "rapier_testbed2d" +version = "0.1.0" +authors = [ "Sébastien Crozet " ] +description = "Testbed for the 2-dimensional physics engine in Rust." +homepage = "http://rapier.org" +repository = "https://github.com/rustsim/rapier" +keywords = [ "physics", "dynamics", "rigid", "real-time", "joints" ] +license = "Apache-2.0" +edition = "2018" + + +[lib] +name = "rapier_testbed2d" +path = "../../src_testbed/lib.rs" +required-features = [ "dim2" ] + +[features] +default = [ "dim2" ] +dim2 = [ ] +parallel = [ "rapier2d/parallel", "num_cpus" ] +other-backends = [ "wrapped2d", "nphysics2d" ] + + +[dependencies] +nalgebra = "0.22" +kiss3d = { version = "0.25", features = [ "conrod" ] } +rand = "0.7" +rand_pcg = "0.2" +instant = { version = "0.1", features = [ "web-sys", "now" ]} +bitflags = "1" +num_cpus = { version = "1", optional = true } +wrapped2d = { version = "0.4", optional = true } +ncollide2d = "0.24" +nphysics2d = { version = "0.17", optional = true } +crossbeam = "0.7" +bincode = "1" +flexbuffers = "0.1" +md5 = "0.7" + + +[dependencies.rapier2d] +path = "../rapier2d" +version = "0.1" +features = [ "serde-serialize" ] diff --git a/build/rapier_testbed3d/Cargo.toml b/build/rapier_testbed3d/Cargo.toml new file mode 100644 index 0000000..31cdbb9 --- /dev/null +++ b/build/rapier_testbed3d/Cargo.toml @@ -0,0 +1,47 @@ +[package] +name = "rapier_testbed3d" +version = "0.1.0" +authors = [ "Sébastien Crozet " ] +description = "Testbed for the 3-dimensional physics engine in Rust." +homepage = "http://rapier.org" +repository = "https://github.com/rustsim/rapier" +keywords = [ "physics", "dynamics", "rigid", "real-time", "joints" ] +license = "Apache-2.0" +edition = "2018" + + +[lib] +name = "rapier_testbed3d" +path = "../../src_testbed/lib.rs" +required-features = [ "dim3" ] + +[features] +default = [ "dim3" ] +dim3 = [ ] +parallel = [ "rapier3d/parallel", "num_cpus" ] +other-backends = [ "physx", "physx-sys", "glam", "nphysics3d" ] + +[dependencies] +nalgebra = "0.22" +kiss3d = { version = "0.25", features = [ "conrod" ] } +rand = "0.7" +rand_pcg = "0.2" +instant = { version = "0.1", features = [ "web-sys", "now" ]} +bitflags = "1" +glam = { version = "0.8", optional = true } +num_cpus = { version = "1", optional = true } +ncollide3d = "0.24" +nphysics3d = { version = "0.17", optional = true } +physx = { version = "0.6", optional = true } +physx-sys = { version = "0.4", optional = true } +crossbeam = "0.7" +bincode = "1" +flexbuffers = "0.1" +serde_cbor = "0.11" +md5 = "0.7" +serde = { version = "1", features = [ "derive" ] } + +[dependencies.rapier3d] +path = "../rapier3d" +version = "0.1" +features = [ "serde-serialize" ] diff --git a/examples2d/Cargo.toml b/examples2d/Cargo.toml new file mode 100644 index 0000000..7e97948 --- /dev/null +++ b/examples2d/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "nphysics-examples-2d" +version = "0.1.0" +authors = [ "Sébastien Crozet " ] +edition = "2018" + +[features] +parallel = [ "rapier2d/parallel", "rapier_testbed2d/parallel" ] +simd-stable = [ "rapier2d/simd-stable" ] +simd-nightly = [ "rapier2d/simd-nightly" ] +other-backends = [ "rapier_testbed2d/other-backends" ] +enhanced-determinism = [ "rapier2d/enhanced-determinism" ] + +[dependencies] +rand = "0.7" +Inflector = "0.11" +nalgebra = "0.22" + +[dependencies.rapier_testbed2d] +path = "../build/rapier_testbed2d" + +[dependencies.rapier2d] +path = "../build/rapier2d" + +[[bin]] +name = "all_examples2" +path = "./all_examples2.rs" \ No newline at end of file diff --git a/examples2d/all_examples2.rs b/examples2d/all_examples2.rs new file mode 100644 index 0000000..7b8bf09 --- /dev/null +++ b/examples2d/all_examples2.rs @@ -0,0 +1,90 @@ +#![allow(dead_code)] + +extern crate nalgebra as na; + +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::prelude::*; + +use inflector::Inflector; + +use rapier_testbed2d::Testbed; +use std::cmp::Ordering; + +mod balls2; +mod boxes2; +mod capsules2; +mod debug_box_ball2; +mod heightfield2; +mod joints2; +mod kinematic2; +mod pyramid2; +mod sensor2; +mod stress_joint_ball2; +mod stress_joint_fixed2; +mod stress_joint_prismatic2; + +fn demo_name_from_command_line() -> Option { + let mut args = std::env::args(); + + while let Some(arg) = args.next() { + if &arg[..] == "--example" { + return args.next(); + } + } + + None +} + +#[cfg(any(target_arch = "wasm32", target_arch = "asmjs"))] +fn demo_name_from_url() -> Option { + None + // let window = stdweb::web::window(); + // let hash = window.location()?.search().ok()?; + // Some(hash[1..].to_string()) +} + +#[cfg(not(any(target_arch = "wasm32", target_arch = "asmjs")))] +fn demo_name_from_url() -> Option { + None +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))] +pub fn main() { + let demo = demo_name_from_command_line() + .or_else(|| demo_name_from_url()) + .unwrap_or(String::new()) + .to_camel_case(); + + let mut builders: Vec<(_, fn(&mut Testbed))> = vec![ + ("Balls", balls2::init_world), + ("Boxes", boxes2::init_world), + ("Capsules", capsules2::init_world), + ("Heightfield", heightfield2::init_world), + ("Joints", joints2::init_world), + ("Kinematic", kinematic2::init_world), + ("Pyramid", pyramid2::init_world), + ("Sensor", sensor2::init_world), + ("(Debug) box ball", debug_box_ball2::init_world), + ("(Stress test) joint ball", stress_joint_ball2::init_world), + ("(Stress test) joint fixed", stress_joint_fixed2::init_world), + ( + "(Stress test) joint prismatic", + stress_joint_prismatic2::init_world, + ), + ]; + + // Lexicographic sort, with stress tests moved at the end of the list. + builders.sort_by(|a, b| match (a.0.starts_with("("), b.0.starts_with("(")) { + (true, true) | (false, false) => a.0.cmp(b.0), + (true, false) => Ordering::Greater, + (false, true) => Ordering::Less, + }); + + let i = builders + .iter() + .position(|builder| builder.0.to_camel_case().as_str() == demo.as_str()) + .unwrap_or(0); + let testbed = Testbed::from_builders(i, builders); + + testbed.run() +} diff --git a/examples2d/balls2.rs b/examples2d/balls2.rs new file mode 100644 index 0000000..4c85d86 --- /dev/null +++ b/examples2d/balls2.rs @@ -0,0 +1,68 @@ +use na::Point2; +use rapier2d::dynamics::{BodyStatus, JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + /* + * Ground + */ + let _ground_size = 25.0; + + /* + let ground_shape = ShapeHandle::new(Cuboid::new(Vector2::new(ground_size, 1.0))); + + let ground_handle = bodies.insert(Ground::new()); + let co = ColliderDesc::new(ground_shape) + .translation(-Vector2::y()) + .build(BodyPartHandle(ground_handle, 0)); + colliders.insert(co); + */ + + /* + * Create the balls + */ + let num = 50; + let rad = 1.0; + + let shiftx = rad * 2.5; + let shifty = rad * 2.0; + let centerx = shiftx * (num as f32) / 2.0; + let centery = shifty / 2.0; + + for i in 0..num { + for j in 0usize..num * 5 { + let x = i as f32 * shiftx - centerx; + let y = j as f32 * shifty + centery; + + let status = if j == 0 { + BodyStatus::Static + } else { + BodyStatus::Dynamic + }; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new(status).translation(x, y).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::ball(rad).density(1.0).build(); + colliders.insert(collider, handle, &mut bodies); + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 2.5), 5.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]); + testbed.run() +} diff --git a/examples2d/boxes2.rs b/examples2d/boxes2.rs new file mode 100644 index 0000000..8b09def --- /dev/null +++ b/examples2d/boxes2.rs @@ -0,0 +1,73 @@ +use na::Point2; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground + */ + let ground_size = 25.0; + + let rigid_body = RigidBodyBuilder::new_static().build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + let rigid_body = RigidBodyBuilder::new_static() + .rotation(std::f32::consts::FRAC_PI_2) + .translation(ground_size, ground_size * 2.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size * 2.0, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + let rigid_body = RigidBodyBuilder::new_static() + .rotation(std::f32::consts::FRAC_PI_2) + .translation(-ground_size, ground_size * 2.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size * 2.0, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Create the cubes + */ + let num = 26; + let rad = 0.5; + + let shift = rad * 2.0; + let centerx = shift * (num as f32) / 2.0; + let centery = shift / 2.0; + + for i in 0..num { + for j in 0usize..num * 5 { + let x = i as f32 * shift - centerx; + let y = j as f32 * shift + centery + 2.0; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build(); + colliders.insert(collider, handle, &mut bodies); + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 50.0), 10.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]); + testbed.run() +} diff --git a/examples2d/capsules2.rs b/examples2d/capsules2.rs new file mode 100644 index 0000000..041edf5 --- /dev/null +++ b/examples2d/capsules2.rs @@ -0,0 +1,76 @@ +use na::Point2; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground + */ + let ground_size = 25.0; + + let rigid_body = RigidBodyBuilder::new_static().build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + let rigid_body = RigidBodyBuilder::new_static() + .rotation(std::f32::consts::FRAC_PI_2) + .translation(ground_size, ground_size * 4.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size * 4.0, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + let rigid_body = RigidBodyBuilder::new_static() + .rotation(std::f32::consts::FRAC_PI_2) + .translation(-ground_size, ground_size * 4.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size * 4.0, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Create the cubes + */ + let num = 26; + let rad = 0.5; + + let shift = rad * 2.0; + let shifty = rad * 5.0; + let centerx = shift * (num as f32) / 2.0; + let centery = shift / 2.0; + + for i in 0..num { + for j in 0usize..num * 5 { + let x = i as f32 * shift - centerx; + let y = j as f32 * shifty + centery + 3.0; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::capsule_y(rad * 1.5, rad) + .density(1.0) + .build(); + colliders.insert(collider, handle, &mut bodies); + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 50.0), 10.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]); + testbed.run() +} diff --git a/examples2d/debug_box_ball2.rs b/examples2d/debug_box_ball2.rs new file mode 100644 index 0000000..2cc5e45 --- /dev/null +++ b/examples2d/debug_box_ball2.rs @@ -0,0 +1,44 @@ +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground + */ + let rad = 1.0; + let rigid_body = RigidBodyBuilder::new_static() + .translation(0.0, -rad) + .rotation(std::f32::consts::PI / 4.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(rad, rad).build(); + colliders.insert(collider, handle, &mut bodies); + + // Build the dynamic box rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(0.0, 3.0 * rad) + .can_sleep(false) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::ball(rad).density(1.0).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + // testbed.look_at(Point2::new(10.0, 10.0, 10.0), Point2::origin()); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Boxes", init_world)]); + testbed.run() +} diff --git a/examples2d/heightfield2.rs b/examples2d/heightfield2.rs new file mode 100644 index 0000000..b03afe3 --- /dev/null +++ b/examples2d/heightfield2.rs @@ -0,0 +1,72 @@ +use na::{DVector, Point2, Vector2}; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground + */ + let ground_size = Vector2::new(50.0, 1.0); + let nsubdivs = 2000; + + let heights = DVector::from_fn(nsubdivs + 1, |i, _| { + if i == 0 || i == nsubdivs { + 80.0 + } else { + (i as f32 * ground_size.x / (nsubdivs as f32)).cos() * 2.0 + } + }); + + let rigid_body = RigidBodyBuilder::new_static().build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::heightfield(heights, ground_size).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Create the cubes + */ + let num = 26; + let rad = 0.5; + + let shift = rad * 2.0; + let centerx = shift * (num / 2) as f32; + let centery = shift / 2.0; + + for i in 0..num { + for j in 0usize..num * 5 { + let x = i as f32 * shift - centerx; + let y = j as f32 * shift + centery + 3.0; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let handle = bodies.insert(rigid_body); + + if j % 2 == 0 { + let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build(); + colliders.insert(collider, handle, &mut bodies); + } else { + let collider = ColliderBuilder::ball(rad).density(1.0).build(); + colliders.insert(collider, handle, &mut bodies); + } + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 50.0), 10.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Heightfield", init_world)]); + testbed.run() +} diff --git a/examples2d/joints2.rs b/examples2d/joints2.rs new file mode 100644 index 0000000..c3df921 --- /dev/null +++ b/examples2d/joints2.rs @@ -0,0 +1,75 @@ +use na::Point2; +use rapier2d::dynamics::{BallJoint, BodyStatus, JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let mut joints = JointSet::new(); + + /* + * Create the balls + */ + // Build the rigid body. + // NOTE: a smaller radius (e.g. 0.1) breaks Box2D so + // in order to be able to compare rapier with Box2D, + // we set it to 0.4. + let rad = 0.4; + let numi = 100; // Num vertical nodes. + let numk = 100; // Num horizontal nodes. + let shift = 1.0; + + let mut body_handles = Vec::new(); + + for k in 0..numk { + for i in 0..numi { + let fk = k as f32; + let fi = i as f32; + + let status = if i == 0 && (k % 4 == 0 || k == numk - 1) { + BodyStatus::Static + } else { + BodyStatus::Dynamic + }; + + let rigid_body = RigidBodyBuilder::new(status) + .translation(fk * shift, -fi * shift) + .build(); + let child_handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::ball(rad).density(1.0).build(); + colliders.insert(collider, child_handle, &mut bodies); + + // Vertical joint. + if i > 0 { + let parent_handle = *body_handles.last().unwrap(); + let joint = BallJoint::new(Point2::origin(), Point2::new(0.0, shift)); + joints.insert(&mut bodies, parent_handle, child_handle, joint); + } + + // Horizontal joint. + if k > 0 { + let parent_index = body_handles.len() - numi; + let parent_handle = body_handles[parent_index]; + let joint = BallJoint::new(Point2::origin(), Point2::new(-shift, 0.0)); + joints.insert(&mut bodies, parent_handle, child_handle, joint); + } + + body_handles.push(child_handle); + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(numk as f32 * rad, numi as f32 * -rad), 5.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Joints", init_world)]); + testbed.run() +} diff --git a/examples2d/kinematic2.rs b/examples2d/kinematic2.rs new file mode 100644 index 0000000..d979cd0 --- /dev/null +++ b/examples2d/kinematic2.rs @@ -0,0 +1,93 @@ +use na::Point2; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground. + */ + let ground_size = 10.0; + let ground_height = 0.1; + + let rigid_body = RigidBodyBuilder::new_static() + .translation(0.0, -ground_height) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, ground_height).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Create the boxes + */ + let num = 6; + let rad = 0.2; + + let shift = rad * 2.0; + let centerx = shift * num as f32 / 2.0; + let centery = shift / 2.0 + 3.04; + + for i in 0usize..num { + for j in 0usize..num * 50 { + let x = i as f32 * shift - centerx; + let y = j as f32 * shift + centery; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build(); + colliders.insert(collider, handle, &mut bodies); + } + } + + /* + * Setup a kinematic rigid body. + */ + let platform_body = RigidBodyBuilder::new_kinematic() + .translation(-10.0 * rad, 1.5 + 0.8) + .build(); + let platform_handle = bodies.insert(platform_body); + let collider = ColliderBuilder::cuboid(rad * 10.0, rad) + .density(1.0) + .build(); + colliders.insert(collider, platform_handle, &mut bodies); + + /* + * Setup a callback to control the platform. + */ + testbed.add_callback(move |bodies, _, _, _, time| { + let mut platform = bodies.get_mut(platform_handle).unwrap(); + let mut next_pos = platform.position; + + let dt = 0.016; + next_pos.translation.vector.y += (time * 5.0).sin() * dt; + next_pos.translation.vector.x += time.sin() * 5.0 * dt; + + if next_pos.translation.vector.x >= rad * 10.0 { + next_pos.translation.vector.x -= dt; + } + if next_pos.translation.vector.x <= -rad * 10.0 { + next_pos.translation.vector.x += dt; + } + + platform.set_next_kinematic_position(next_pos); + }); + + /* + * Run the simulation. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 1.0), 40.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Kinematic body", init_world)]); + testbed.run() +} diff --git a/examples2d/pyramid2.rs b/examples2d/pyramid2.rs new file mode 100644 index 0000000..4d6fe07 --- /dev/null +++ b/examples2d/pyramid2.rs @@ -0,0 +1,60 @@ +use na::Point2; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground + */ + let ground_size = 100.0; + let ground_thickness = 1.0; + + let rigid_body = RigidBodyBuilder::new_static().build(); + let ground_handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, ground_thickness).build(); + colliders.insert(collider, ground_handle, &mut bodies); + + /* + * Create the cubes + */ + let num = 100; + let rad = 0.5; + + let shift = rad * 2.0; + let centerx = shift * (num as f32) / 2.0; + let centery = shift / 2.0 + ground_thickness + rad * 1.5; + + for i in 0usize..num { + for j in i..num { + let fj = j as f32; + let fi = i as f32; + let x = (fi * shift / 2.0) + (fj - fi) * shift - centerx; + let y = fi * shift + centery; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build(); + colliders.insert(collider, handle, &mut bodies); + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 2.5), 5.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]); + testbed.run() +} diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs new file mode 100644 index 0000000..75634a8 --- /dev/null +++ b/examples2d/sensor2.rs @@ -0,0 +1,101 @@ +use na::{Point2, Point3}; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet, Proximity}; +use rapier_testbed2d::Testbed; + +pub fn