avian3d/dynamics/ccd/
mod.rs

1//! Contains components and functionality for Continuous Collision Detection.
2//!
3//! # What Is CCD?
4//!
5//! Physics simulation is typically done in a discrete manner.
6//! At the beginning of each physics frame, the simulation checks for collisions,
7//! and if none are found for a given rigid body, it is free to move according to
8//! its velocity and the size of the timestep.
9//!
10//! This generally works well for large or slowly moving objects, but fast and small
11//! objects can pass through thin geometry such as walls and triangle meshes.
12//! This phenomenon is often called **tunneling**.
13//!
14//! <svg width="300" height="350" viewBox="0 0 300 350" fill="none" xmlns="http://www.w3.org/2000/svg">
15//!     <rect x="141" y="1" width="18" height="298" fill="#64C850" stroke="black" stroke-width="2"/>
16//!     <circle cx="275" cy="150" r="24" stroke="#5064C8" stroke-width="2" stroke-dasharray="4 4"/>
17//!     <circle cx="25" cy="150" r="24" fill="#5064C8" stroke="black" stroke-width="2"/>
18//!     <path d="M275.707 150.707C276.098 150.317 276.098 149.683 275.707 149.293L269.343 142.929C268.953 142.538 268.319 142.538 267.929 142.929C267.538 143.319 267.538 143.953 267.929 144.343L273.586 150L267.929 155.657C267.538 156.047 267.538 156.681 267.929 157.071C268.319 157.462 268.953 157.462 269.343 157.071L275.707 150.707ZM25 151L275 151L275 149L25 149L25 151Z" fill="#E19664"/>
19//!     <text x="150" y="325" style="fill: #b4b4b4; font: 18px monospace; text-anchor: middle;">Discrete</text>
20//! </svg>
21//!
22//! **Continuous Collision Detection** (CCD) aims to prevent tunneling.
23//! Currently, two approaches are supported: [Speculative Collision](#speculative-collision) and [Swept CCD](#swept-ccd).
24//! Speculative collision is enabled by default, but swept CCD is opt-in with the [`SweptCcd`] component.
25//! The two approaches can also be used together.
26//!
27//! ## Speculative Collision
28//!
29//! **Speculative collision** is a form of Continuous Collision Detection
30//! where contacts are predicted before they happen. The contacts are only
31//! solved if the entities are expected to come into contact within the next frame.
32//!
33//! To determine whether two bodies may come into contact, their [AABBs](ColliderAabb) are expanded
34//! based on their velocities. Additionally, a **speculative margin** is used to determine
35//! the maximum distance at which a collision pair can generate speculative contacts.
36//!
37//! <svg width="335" height="400" viewBox="0 0 335 400" fill="none" xmlns="http://www.w3.org/2000/svg">
38//!     <rect x="36" y="266" width="298" height="18" fill="#64C850" stroke="black" stroke-width="2"/>
39//!     <circle cx="210" cy="375" r="24" stroke="#5064C8" stroke-width="2" stroke-dasharray="4 4"/>
40//!     <circle cx="210" cy="240" r="24" stroke="#5064C8" stroke-width="2" stroke-dasharray="4 4"/>
41//!     <circle cx="160" cy="160" r="24" fill="#5064C8" stroke="black" stroke-width="2"/>
42//!     <path d="M209.471 375.849C209.94 376.141 210.557 375.997 210.849 375.529L215.606 367.888C215.898 367.42 215.754 366.803 215.286 366.511C214.817 366.219 214.2 366.363 213.908 366.831L209.68 373.623L202.888 369.394C202.42 369.102 201.803 369.246 201.511 369.714C201.219 370.183 201.363 370.8 201.831 371.092L209.471 375.849ZM159.026 160.227L159.472 162.146L161.42 161.693L160.974 159.773L159.026 160.227ZM160.365 165.985L161.258 169.825L163.206 169.372L162.313 165.532L160.365 165.985ZM162.151 173.664L163.044 177.503L164.992 177.05L164.099 173.211L162.151 173.664ZM163.937 181.343L164.83 185.182L166.778 184.729L165.885 180.89L163.937 181.343ZM165.722 189.021L166.615 192.86L168.563 192.407L167.67 188.568L165.722 189.021ZM167.508 196.7L168.401 200.539L170.349 200.086L169.456 196.247L167.508 196.7ZM169.294 204.378L170.187 208.218L172.135 207.765L171.242 203.925L169.294 204.378ZM171.08 212.057L171.972 215.896L173.92 215.443L173.028 211.604L171.08 212.057ZM172.865 219.735L173.758 223.575L175.706 223.122L174.813 219.282L172.865 219.735ZM174.651 227.414L175.544 231.253L177.492 230.8L176.599 226.961L174.651 227.414ZM176.437 235.093L177.33 238.932L179.278 238.479L178.385 234.64L176.437 235.093ZM178.222 242.771L179.115 246.61L181.063 246.157L180.17 242.318L178.222 242.771ZM180.008 250.45L180.901 254.289L182.849 253.836L181.956 249.997L180.008 250.45ZM181.794 258.128L182.687 261.968L184.635 261.515L183.742 257.675L181.794 258.128ZM183.58 265.807L184.472 269.646L186.42 269.193L185.528 265.354L183.58 265.807ZM185.365 273.485L186.258 277.325L188.206 276.872L187.313 273.032L185.365 273.485ZM187.151 281.164L188.044 285.003L189.992 284.55L189.099 280.711L187.151 281.164ZM188.937 288.843L189.83 292.682L191.778 292.229L190.885 288.39L188.937 288.843ZM190.722 296.521L191.615 300.36L193.563 299.907L192.67 296.068L190.722 296.521ZM192.508 304.2L193.401 308.039L195.349 307.586L194.456 303.747L192.508 304.2ZM194.294 311.878L195.187 315.718L197.135 315.265L196.242 311.425L194.294 311.878ZM196.08 319.557L196.972 323.396L198.92 322.943L198.028 319.104L196.08 319.557ZM197.865 327.235L198.758 331.075L200.706 330.622L199.813 326.782L197.865 327.235ZM199.651 334.914L200.544 338.753L202.492 338.3L201.599 334.461L199.651 334.914ZM201.437 342.593L202.33 346.432L204.278 345.979L203.385 342.14L201.437 342.593ZM203.222 350.271L204.115 354.111L206.063 353.657L205.17 349.818L203.222 350.271ZM205.008 357.95L205.901 361.789L207.849 361.336L206.956 357.497L205.008 357.95ZM206.794 365.628L207.687 369.468L209.635 369.015L208.742 365.175L206.794 365.628ZM208.58 373.307L209.026 375.226L210.974 374.773L210.528 372.854L208.58 373.307ZM209.471 375.849C209.94 376.141 210.557 375.997 210.849 375.529L215.606 367.888C215.898 367.42 215.754 366.803 215.286 366.511C214.817 366.219 214.2 366.363 213.908 366.831L209.68 373.623L202.888 369.394C202.42 369.102 201.803 369.246 201.511 369.714C201.219 370.183 201.363 370.8 201.831 371.092L209.471 375.849ZM159.026 160.227L159.472 162.146L161.42 161.693L160.974 159.773L159.026 160.227ZM160.365 165.985L161.258 169.825L163.206 169.372L162.313 165.532L160.365 165.985ZM162.151 173.664L163.044 177.503L164.992 177.05L164.099 173.211L162.151 173.664ZM163.937 181.343L164.83 185.182L166.778 184.729L165.885 180.89L163.937 181.343ZM165.722 189.021L166.615 192.86L168.563 192.407L167.67 188.568L165.722 189.021ZM167.508 196.7L168.401 200.539L170.349 200.086L169.456 196.247L167.508 196.7ZM169.294 204.378L170.187 208.218L172.135 207.765L171.242 203.925L169.294 204.378ZM171.08 212.057L171.972 215.896L173.92 215.443L173.028 211.604L171.08 212.057ZM172.865 219.735L173.758 223.575L175.706 223.122L174.813 219.282L172.865 219.735ZM174.651 227.414L175.544 231.253L177.492 230.8L176.599 226.961L174.651 227.414ZM176.437 235.093L177.33 238.932L179.278 238.479L178.385 234.64L176.437 235.093ZM178.222 242.771L179.115 246.61L181.063 246.157L180.17 242.318L178.222 242.771ZM180.008 250.45L180.901 254.289L182.849 253.836L181.956 249.997L180.008 250.45ZM181.794 258.128L182.687 261.968L184.635 261.515L183.742 257.675L181.794 258.128ZM183.58 265.807L184.472 269.646L186.42 269.193L185.528 265.354L183.58 265.807ZM185.365 273.485L186.258 277.325L188.206 276.872L187.313 273.032L185.365 273.485ZM187.151 281.164L188.044 285.003L189.992 284.55L189.099 280.711L187.151 281.164ZM188.937 288.843L189.83 292.682L191.778 292.229L190.885 288.39L188.937 288.843ZM190.722 296.521L191.615 300.36L193.563 299.907L192.67 296.068L190.722 296.521ZM192.508 304.2L193.401 308.039L195.349 307.586L194.456 303.747L192.508 304.2ZM194.294 311.878L195.187 315.718L197.135 315.265L196.242 311.425L194.294 311.878ZM196.08 319.557L196.972 323.396L198.92 322.943L198.028 319.104L196.08 319.557ZM197.865 327.235L198.758 331.075L200.706 330.622L199.813 326.782L197.865 327.235ZM199.651 334.914L200.544 338.753L202.492 338.3L201.599 334.461L199.651 334.914ZM201.437 342.593L202.33 346.432L204.278 345.979L203.385 342.14L201.437 342.593ZM203.222 350.271L204.115 354.111L206.063 353.657L205.17 349.818L203.222 350.271ZM205.008 357.95L205.901 361.789L207.849 361.336L206.956 357.497L205.008 357.95ZM206.794 365.628L207.687 369.468L209.635 369.015L208.742 365.175L206.794 365.628ZM208.58 373.307L209.026 375.226L210.974 374.773L210.528 372.854L208.58 373.307Z" fill="#AF644B"/>
43//!     <path d="M209.775 240.974C210.313 241.099 210.85 240.763 210.974 240.225L212.998 231.455C213.122 230.917 212.787 230.38 212.249 230.256C211.71 230.132 211.174 230.467 211.049 231.006L209.25 238.801L201.455 237.002C200.917 236.878 200.38 237.213 200.256 237.751C200.132 238.29 200.467 238.826 201.006 238.951L209.775 240.974ZM159.152 160.53L209.152 240.53L210.848 239.47L160.848 159.47L159.152 160.53Z" fill="#E19664"/>
44//!     <circle cx="160" cy="265" r="4" fill="#a874d8"/>
45//!     <path d="M160 160V265" stroke="#a874d8" stroke-width="2" stroke-dasharray="5 5"/>
46//!     <rect x="135" y="135" width="99" height="264" stroke="#db9010" stroke-width="1"/>
47//!     <circle cx="160" cy="160" r="159" stroke="#eb4a5a" stroke-width="1"/>
48//!     <text x="184" y="125" style="fill: #db9010; font: 16px monospace; text-anchor: middle;">Collision AABB</text>
49//!     <text x="160" y="40" style="fill: #eb4a5a; font: 16px monospace; text-anchor: middle;">Speculative Margin</text>
50//!     <text x="210" y="340" style="fill: #b4b4b4; font: 16px monospace; text-anchor: start;">Unconstrained</text>
51//!     <text x="210" y="205" style="fill: #b4b4b4; font: 16px monospace; text-anchor: start;">Constrained</text>
52//!     <text y="240" style="fill: #a874d8; font: 16px monospace; font-weight: bold; text-anchor: end;">
53//!         <tspan x="155">Speculative</tspan><tspan x="155" dy="18">Contact</tspan>
54//!     </text>
55//! </svg>
56//!
57//! The current "effective" speculative margin for a body is determined by its velocity
58//! clamped by the specified maximum bound. By default, the maximum bound is infinite,
59//! but it can be configured for all entities using the [`NarrowPhaseConfig`] resource,
60//! and for individual entities using the [`SpeculativeMargin`] component.
61//!
62//! ```
63#![cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
64#![cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
65//! use bevy::prelude::*;
66//!
67//! fn setup(mut commands: Commands) {
68//!     // Spawn a rigid body with a maximum bound for the speculative margin.
69//!     commands.spawn((
70//!         RigidBody::Dynamic,
71//!         Collider::capsule(0.5, 2.0),
72//!         SpeculativeMargin(2.0),
73//!     ));
74//! }
75//! ```
76//!
77//! Speculative collisions are an efficient and generally robust approach
78//! to Continuous Collision Detection. They are enabled for all bodies by default,
79//! provided that the default naximum speculative margin is greater than zero.
80//!
81//! However, speculative collisions aren't entirely without issues,
82//! and there are some cases where large speculative margins can cause undesired behavior.
83//!
84//! ### Caveats of Speculative Collision
85//!
86//! Speculative contacts are approximations. They typically have good enough accuracy,
87//! but when bodies are moving past each other at high speeds, the prediction can sometimes
88//! fail and lead to **ghost collisions**. This happens because contact surfaces are treated
89//! like infinite planes from the point of view of the solver. Ghost collisions typically manifest
90//! as objects bumping into seemingly invisible walls.
91//!
92//! <svg width="475" height="400" viewBox="0 0 475 400" fill="none" xmlns="http://www.w3.org/2000/svg">
93//!     <rect x="36" y="266" width="148" height="18" fill="#64C850" stroke="black" stroke-width="2"/>
94//!     <circle cx="345" cy="375" r="24" stroke="#5064C8" stroke-width="2" stroke-dasharray="4 4"/>
95//!     <circle cx="345" cy="236" r="24" stroke="#5064C8" stroke-width="2" stroke-dasharray="4 4"/>
96//!     <circle cx="160" cy="160" r="24" fill="#5064C8" stroke="black" stroke-width="2"/>
97//!     <path d="M344.925 375.997C345.476 376.038 345.956 375.626 345.997 375.075L346.67 366.1C346.712 365.549 346.299 365.069 345.748 365.028C345.197 364.987 344.717 365.4 344.676 365.95L344.078 373.928L336.1 373.33C335.549 373.288 335.069 373.701 335.028 374.252C334.987 374.803 335.4 375.283 335.95 375.324L344.925 375.997ZM159.242 160.652L160.563 162.188L162.079 160.883L160.758 159.348L159.242 160.652ZM163.206 165.259L165.849 168.331L167.365 167.026L164.722 163.955L163.206 165.259ZM168.492 171.402L171.135 174.474L172.651 173.169L170.008 170.098L168.492 171.402ZM173.778 177.545L176.421 180.617L177.937 179.312L175.294 176.241L173.778 177.545ZM179.063 183.688L181.706 186.759L183.222 185.455L180.579 182.383L179.063 183.688ZM184.349 189.831L186.992 192.902L188.508 191.598L185.865 188.526L184.349 189.831ZM189.635 195.974L192.278 199.045L193.794 197.741L191.151 194.669L189.635 195.974ZM194.921 202.117L197.563 205.188L199.079 203.883L196.437 200.812L194.921 202.117ZM200.206 208.259L202.849 211.331L204.365 210.026L201.722 206.955L200.206 208.259ZM205.492 214.402L208.135 217.474L209.651 216.169L207.008 213.098L205.492 214.402ZM210.778 220.545L213.421 223.617L214.937 222.312L212.294 219.241L210.778 220.545ZM216.063 226.688L218.706 229.759L220.222 228.455L217.579 225.383L216.063 226.688ZM221.349 232.831L223.992 235.902L225.508 234.598L222.865 231.526L221.349 232.831ZM226.635 238.974L229.278 242.045L230.794 240.741L228.151 237.669L226.635 238.974ZM231.921 245.117L234.563 248.188L236.079 246.883L233.437 243.812L231.921 245.117ZM237.206 251.259L239.849 254.331L241.365 253.026L238.722 249.955L237.206 251.259ZM242.492 257.402L245.135 260.474L246.651 259.169L244.008 256.098L242.492 257.402ZM247.778 263.545L250.421 266.617L251.937 265.312L249.294 262.241L247.778 263.545ZM253.063 269.688L255.706 272.759L257.222 271.455L254.579 268.383L253.063 269.688ZM258.349 275.831L260.992 278.902L262.508 277.598L259.865 274.526L258.349 275.831ZM263.635 281.974L266.278 285.045L267.794 283.741L265.151 280.669L263.635 281.974ZM268.921 288.116L271.563 291.188L273.079 289.883L270.437 286.812L268.921 288.116ZM274.206 294.259L276.849 297.331L278.365 296.026L275.722 292.955L274.206 294.259ZM279.492 300.402L282.135 303.474L283.651 302.169L281.008 299.098L279.492 300.402ZM284.778 306.545L287.421 309.616L288.937 308.312L286.294 305.241L284.778 306.545ZM290.063 312.688L292.706 315.759L294.222 314.455L291.579 311.383L290.063 312.688ZM295.349 318.831L297.992 321.902L299.508 320.598L296.865 317.526L295.349 318.831ZM300.635 324.974L303.278 328.045L304.794 326.741L302.151 323.669L300.635 324.974ZM305.921 331.116L308.563 334.188L310.079 332.883L307.437 329.812L305.921 331.116ZM311.206 337.259L313.849 340.331L315.365 339.026L312.722 335.955L311.206 337.259ZM316.492 343.402L319.135 346.474L320.651 345.169L318.008 342.098L316.492 343.402ZM321.778 349.545L324.421 352.616L325.937 351.312L323.294 348.241L321.778 349.545ZM327.063 355.688L329.706 358.759L331.222 357.455L328.579 354.383L327.063 355.688ZM332.349 361.831L334.992 364.902L336.508 363.598L333.865 360.526L332.349 361.831ZM337.635 367.974L340.278 371.045L341.794 369.741L339.151 366.669L337.635 367.974ZM342.921 374.117L344.242 375.652L345.758 374.348L344.437 372.812L342.921 374.117ZM344.925 375.997C345.476 376.038 345.956 375.626 345.997 375.075L346.67 366.1C346.712 365.549 346.299 365.069 345.748 365.028C345.197 364.987 344.717 365.4 344.676 365.95L344.078 373.928L336.1 373.33C335.549 373.288 335.069 373.701 335.028 374.252C334.987 374.803 335.4 375.283 335.95 375.324L344.925 375.997ZM159.242 160.652L160.563 162.188L162.079 160.883L160.758 159.348L159.242 160.652ZM163.206 165.259L165.849 168.331L167.365 167.026L164.722 163.955L163.206 165.259ZM168.492 171.402L171.135 174.474L172.651 173.169L170.008 170.098L168.492 171.402ZM173.778 177.545L176.421 180.617L177.937 179.312L175.294 176.241L173.778 177.545ZM179.063 183.688L181.706 186.759L183.222 185.455L180.579 182.383L179.063 183.688ZM184.349 189.831L186.992 192.902L188.508 191.598L185.865 188.526L184.349 189.831ZM189.635 195.974L192.278 199.045L193.794 197.741L191.151 194.669L189.635 195.974ZM194.921 202.117L197.563 205.188L199.079 203.883L196.437 200.812L194.921 202.117ZM200.206 208.259L202.849 211.331L204.365 210.026L201.722 206.955L200.206 208.259ZM205.492 214.402L208.135 217.474L209.651 216.169L207.008 213.098L205.492 214.402ZM210.778 220.545L213.421 223.617L214.937 222.312L212.294 219.241L210.778 220.545ZM216.063 226.688L218.706 229.759L220.222 228.455L217.579 225.383L216.063 226.688ZM221.349 232.831L223.992 235.902L225.508 234.598L222.865 231.526L221.349 232.831ZM226.635 238.974L229.278 242.045L230.794 240.741L228.151 237.669L226.635 238.974ZM231.921 245.117L234.563 248.188L236.079 246.883L233.437 243.812L231.921 245.117ZM237.206 251.259L239.849 254.331L241.365 253.026L238.722 249.955L237.206 251.259ZM242.492 257.402L245.135 260.474L246.651 259.169L244.008 256.098L242.492 257.402ZM247.778 263.545L250.421 266.617L251.937 265.312L249.294 262.241L247.778 263.545ZM253.063 269.688L255.706 272.759L257.222 271.455L254.579 268.383L253.063 269.688ZM258.349 275.831L260.992 278.902L262.508 277.598L259.865 274.526L258.349 275.831ZM263.635 281.974L266.278 285.045L267.794 283.741L265.151 280.669L263.635 281.974ZM268.921 288.116L271.563 291.188L273.079 289.883L270.437 286.812L268.921 288.116ZM274.206 294.259L276.849 297.331L278.365 296.026L275.722 292.955L274.206 294.259ZM279.492 300.402L282.135 303.474L283.651 302.169L281.008 299.098L279.492 300.402ZM284.778 306.545L287.421 309.616L288.937 308.312L286.294 305.241L284.778 306.545ZM290.063 312.688L292.706 315.759L294.222 314.455L291.579 311.383L290.063 312.688ZM295.349 318.831L297.992 321.902L299.508 320.598L296.865 317.526L295.349 318.831ZM300.635 324.974L303.278 328.045L304.794 326.741L302.151 323.669L300.635 324.974ZM305.921 331.116L308.563 334.188L310.079 332.883L307.437 329.812L305.921 331.116ZM311.206 337.259L313.849 340.331L315.365 339.026L312.722 335.955L311.206 337.259ZM316.492 343.402L319.135 346.474L320.651 345.169L318.008 342.098L316.492 343.402ZM321.778 349.545L324.421 352.616L325.937 351.312L323.294 348.241L321.778 349.545ZM327.063 355.688L329.706 358.759L331.222 357.455L328.579 354.383L327.063 355.688ZM332.349 361.831L334.992 364.902L336.508 363.598L333.865 360.526L332.349 361.831ZM337.635 367.974L340.278 371.045L341.794 369.741L339.151 366.669L337.635 367.974ZM342.921 374.117L344.242 375.652L345.758 374.348L344.437 372.812L342.921 374.117Z" fill="#AF644B"/>
98//!     <path d="M345.385 236.923C345.895 236.71 346.136 236.124 345.923 235.615L342.454 227.31C342.242 226.8 341.656 226.56 341.146 226.772C340.637 226.985 340.396 227.571 340.609 228.08L343.692 235.463L336.31 238.546C335.8 238.758 335.56 239.344 335.772 239.854C335.985 240.363 336.571 240.604 337.08 240.391L345.385 236.923ZM159.62 160.925L344.62 236.925L345.38 235.075L160.38 159.075L159.62 160.925Z" fill="#E19664"/>
99//!     <circle cx="160" cy="265" r="4" fill="#a874d8"/>
100//!     <path d="M160 160V265" stroke="#a874d8" stroke-width="2" stroke-dasharray="5 5"/>
101//!     <rect x="135" y="135" width="235" height="264" stroke="#db9010" stroke-width="1"/>
102//!     <circle cx="160" cy="160" r="159" stroke="#eb4a5a" stroke-width="1"/>
103//!     <line x1="160" y1="264" x2="400" y2="264" stroke="#a874d8" stroke-width="2" stroke-dasharray="6 6"/>
104//!     <text x="184" y="125" style="fill: #db9010; font: 16px monospace; text-anchor: middle;">Collision AABB</text>
105//!     <text x="160" y="40" style="fill: #eb4a5a; font: 16px monospace; text-anchor: middle;">Speculative Margin</text>
106//!     <text x="345" y="340" style="fill: #b4b4b4; font: 16px monospace; text-anchor: start;">Unconstrained</text>
107//!     <text x="345" y="205" style="fill: #b4b4b4; font: 16px monospace; text-anchor: start;">Constrained</text>
108//!     <text x="190" y="280" style="fill: #a874d8; font: 16px monospace; font-weight: bold; text-anchor: start;">Ghost Collision Plane</text>
109//! </svg>
110//!
111//! Ghost collisions can be mitigated by using a smaller [`SpeculativeMargin`]
112//! or a higher [`Physics`] timestep rate.
113//!
114//! Another caveat of speculative collisions is that they can still occasionally miss contacts,
115//! especially for thin objects spinning at very high speeds. This is typically quite rare however,
116//! and speculative collision should work fine for the majority of cases.
117//!
118//! Speculative collisions can also absorb some energy in contacts, causing even perfectly elastic
119//! objects to lose kinetic energy over several bounces.
120//!
121//! For an approach that is more expensive but doesn't suffer from ghost collisions,
122//! missed collisions, or inaccurate restitution, consider using swept CCD,
123//! which is described in the following section.
124//!
125//! ## Swept CCD
126//!
127//! *Note: Swept CCD currently only supports the built-in `Collider`.*
128//!
129//! **Swept CCD** is a form of Continuous Collision Detection that sweeps potentially colliding objects
130//! from their previous positions to their current positions, and if a collision is found, moves the bodies
131//! back to the time of impact. This way, the normal collision algorithms will be able to detect and handle
132//! the collision during the next frame.
133//!
134//! There are two variants of swept CCD: [`Linear`](SweepMode::Linear)
135//! and [`NonLinear`](SweepMode::Linear). The difference between the two
136//! is that [`Linear`](SweepMode::Linear) only considers translational motion,
137//! so bodies can still pass through objects that are spinning at high speeds,
138//! while [`NonLinear`](SweepMode::NonLinear) also considers rotational motion,
139//! but is more expensive.
140//!
141//! <svg width="300" height="350" viewBox="0 0 300 350" fill="none" xmlns="http://www.w3.org/2000/svg">
142//!     <rect x="141" y="1" width="18" height="298" fill="#64C850" stroke="black" stroke-width="2"/>
143//!     <circle cx="115" cy="150" r="24" stroke="#5064C8" stroke-width="2" stroke-dasharray="4 4"/>
144//!     <circle cx="25" cy="150" r="24" fill="#5064C8" stroke="black" stroke-width="2"/>
145//!     <path d="M115.707 150.707C116.098 150.317 116.098 149.683 115.707 149.293L109.343 142.929C108.953 142.538 108.319 142.538 107.929 142.929C107.538 143.319 107.538 143.953 107.929 144.343L113.586 150L107.929 155.657C107.538 156.047 107.538 156.681 107.929 157.071C108.319 157.462 108.953 157.462 109.343 157.071L115.707 150.707ZM25 151H115V149H25V151Z" fill="#E19664"/>
146//!     <text x="150" y="325" style="fill: #b4b4b4; font: 18px monospace; text-anchor: middle;">Linear / NonLinear</text>
147//! </svg>
148//!
149//! <svg width="600" height="350" viewBox="0 0 600 350" fill="none" xmlns="http://www.w3.org/2000/svg">
150//!     <rect x="438.95" y="25.3488" width="18" height="298" transform="rotate(27.5 438.95 25.3488)" stroke="#64C850" stroke-width="2" stroke-dasharray="4 4"/>
151//!     <rect x="301" y="1" width="18" height="298" fill="#64C850" stroke="black" stroke-width="2"/>
152//!     <circle cx="425" cy="150" r="24" fill="#5064C8" stroke="black" stroke-width="2"/>
153//!     <text x="150" y="325" style="fill: #b4b4b4; font: 18px monospace; text-anchor: middle;">Linear</text>
154//!     <circle cx="310" cy="290" r="5" fill="#D9D9D9" stroke="black" stroke-width="2"/>
155//!     <path d="M322.96 148.816L323.331 149.745L323.331 149.745L322.96 148.816ZM361.786 156.786L361.078 157.493L361.078 157.493L361.786 156.786ZM365 161C365.552 161 366 160.552 366 160L366 151C366 150.448 365.552 150 365 150C364.448 150 364 150.448 364 151L364 159L356 159C355.448 159 355 159.448 355 160C355 160.552 355.448 161 356 161L365 161ZM320.371 150.928L323.331 149.745L322.588 147.888L319.629 149.072L320.371 150.928ZM361.078 157.493L364.293 160.707L365.707 159.293L362.493 156.078L361.078 157.493ZM323.331 149.745C336.331 144.545 351.178 147.592 361.078 157.493L362.493 156.078C352.027 145.612 336.331 142.391 322.588 147.888L323.331 149.745Z" fill="#E19664"/>
156//!     <rect x="259.442" y="133.366" width="18" height="298" transform="rotate(60 259.442 133.366)" stroke="#64C850" stroke-width="2" stroke-dasharray="4 4"/>
157//!     <rect x="1" y="1" width="18" height="298" fill="#64C850" stroke="black" stroke-width="2"/>
158//!     <circle cx="125" cy="150" r="24" fill="#5064C8" stroke="black" stroke-width="2"/>
159//!     <text x="450" y="325" style="fill: #b4b4b4; font: 18px monospace; text-anchor: middle;">NonLinear</text>
160//!     <circle cx="10" cy="290" r="5" fill="#D9D9D9" stroke="black" stroke-width="2"/>
161//!     <path d="M54.0561 245.357L53.1144 245.694L53.1144 245.694L54.0561 245.357ZM54.5719 248.904C55.071 249.14 55.6673 248.927 55.9037 248.428L59.7565 240.294C59.9929 239.795 59.7799 239.199 59.2808 238.963C58.7817 238.726 58.1854 238.939 57.949 239.438L54.5243 246.668L47.2944 243.243C46.7953 243.007 46.199 243.22 45.9626 243.719C45.7261 244.218 45.9391 244.815 46.4382 245.051L54.5719 248.904ZM53.1144 245.694L54.0582 248.336L55.9417 247.664L54.9978 245.021L53.1144 245.694ZM20.3714 230.928C33.4979 225.678 48.3594 232.379 53.1144 245.694L54.9978 245.021C49.8615 230.639 33.808 223.4 19.6286 229.071L20.3714 230.928Z" fill="#E19664"/>
162//! </svg>
163//!
164//! To enable swept CCD for a rigid body, simply add the [`SweptCcd`] component
165//! and make sure that the [`CcdPlugin`] is enabled. The plugin is included
166//! in the [`PhysicsPlugins`] plugin group.
167//!
168//! ```
169#![cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
170#![cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
171//! use bevy::prelude::*;
172//!
173//! fn setup(mut commands: Commands) {
174//!     // Spawn a rigid body with swept CCD enabled.
175//!     // `SweepMode::NonLinear` is used by default.
176//!     commands.spawn((
177//!         RigidBody::Dynamic,
178//!         Collider::capsule(0.5, 2.0),
179//!         SweptCcd::default(),
180//!     ));
181//! }
182//! ```
183//!
184//! See the [documentation](SweptCcd) of the component for more details
185//! and configuration options.
186//!
187//! Swept CCD is more expensive than [Speculative Collision](#speculative-collision),
188//! but it doesn't have the same issues with ghost collisions or missed collisions.
189//! It is primarily intended as a safety net when it is crucial that a body never
190//! tunnels through geometry. Swept CCD should only be used when necessary,
191//! as it also has its own set of problems.
192//!
193//! ### Caveats of Swept CCD
194//!
195//! The [`Linear`](SweepMode::Linear) and [`NonLinear`](SweepMode::Linear)
196//! approaches can lead to *time loss* or *time stealing*, where bodies appear to momentarily
197//! move slower when Swept CCD is active. This happens because they are essentially moved
198//! backwards in time to avoid missing the collision.
199//!
200//! Swept CCD might also not account for chain reactions properly, so if a fast-moving body
201//! bumps into another body, making it a fast-moving body that potentially bumps into
202//! even more bodies, the collisions might not propagate accurately.
203//! However, speculative contacts do detect secondary collisions quite well,
204//! so using the two together can help mitigate the issue.
205//!
206//! Time loss and chain reactions could also be better accounted for with a substepped
207//! time-of-impact solver, but that would be much more expensive and complex,
208//! so it is not yet supported.
209//!
210//! ## Other Ways to Avoid Tunneling
211//!
212//! CCD is one way to prevent objects from tunneling through each other,
213//! but it should only be used when necessary. There are several other approaches
214//! worth considering to help avoid the issue.
215//!
216//! The most obvious way is to simply avoid small or thin geometry such as triangle meshes,
217//! and to make colliders for objects like walls slightly thicker than necessary.
218//! This is of course typically not possible everywhere, but it is good to keep in mind
219//! when authoring levels.
220//!
221//! Triangle mesh colliders are especially prone to tunneling for dynamic rigid bodies.
222//! For shapes that are intended to be solid from the inside, it is recommended
223//! to use convex decomposition instead.
224//!
225//! If you must use triangle mesh colliders and are having stability issues, consider
226//! giving them a small amount of extra thickness using the [`CollisionMargin`] component.
227//! This helps prevent objects from passing through the surface while also reducing
228//! numerical errors and improving performance.
229//!
230//! Finally, making the [physics timestep](Physics) smaller can also help.
231//! However, this comes at the cost of worse performance for the entire simulation.
232
233use crate::prelude::*;
234#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
235use bevy::ecs::query::QueryData;
236use bevy::prelude::*;
237use derive_more::From;
238#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
239use dynamics::solver::SolverDiagnostics;
240#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
241use parry::query::{
242    cast_shapes, cast_shapes_nonlinear, NonlinearRigidMotion, ShapeCastHit, ShapeCastOptions,
243};
244
245/// A plugin for [Continuous Collision Detection](self).
246pub struct CcdPlugin;
247
248impl Plugin for CcdPlugin {
249    fn build(&self, app: &mut App) {
250        app.register_type::<SweptCcd>().register_type::<SweepMode>();
251
252        // Get the `PhysicsSchedule`, and panic if it doesn't exist.
253        let physics = app
254            .get_schedule_mut(PhysicsSchedule)
255            .expect("add PhysicsSchedule first");
256
257        physics.configure_sets(SweptCcdSet.in_set(SolverSet::PostSubstep));
258
259        #[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
260        physics.add_systems(solve_swept_ccd.in_set(SweptCcdSet));
261    }
262}
263
264/// A system set for [Continuous Collision Detection](self) systems.
265#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)]
266pub struct SweptCcdSet;
267
268/// The maximum distance at which [speculative contacts](self#speculative-collision)
269/// are generated for this entity. A value greater than zero helps prevent missing
270/// contacts for moderately fast-moving and thin objects.
271///
272/// In general, this component should not be needed. The default speculative margin
273/// for all objects is defined in the [`NarrowPhaseConfig`] resource, and it is unbounded
274/// by default, meaning that the margin can extend infinitely. The margin can be bounded
275/// for individual entities using this component in case speculative contacts are causing
276/// issues like the ones outlined [here](self#caveats-of-speculative-collision).
277///
278/// See the [module-level documentation](self) for information about
279/// Speculative Conllision and Continuous Collision Detection in general.
280///
281/// # Example
282///
283/// ```
284#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
285#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
286/// use bevy::prelude::*;
287///
288/// fn setup(mut commands: Commands) {
289///     // Spawn a rigid body with an unbounded speculative margin.
290///     commands.spawn((
291///         RigidBody::Dynamic,
292///         Collider::capsule(0.5, 2.0),
293///         SpeculativeMargin::MAX,
294///     ));
295/// }
296/// ```
297#[derive(Component, Clone, Copy, Debug, Deref, DerefMut, PartialEq, Reflect, From)]
298#[reflect(Component)]
299#[doc(alias = "SweptCcdPredictionDistance")]
300pub struct SpeculativeMargin(pub Scalar);
301
302impl SpeculativeMargin {
303    /// A zero speculative margin. Disables speculative collision for this entity.
304    pub const ZERO: Self = Self(0.0);
305
306    /// An unbounded speculative margin.
307    pub const MAX: Self = Self(Scalar::MAX);
308}
309
310/// A component that enables sweep-based [Continuous Collision Detection](self) (CCD)
311/// for a [`RigidBody`]. This helps prevent missed collisions for small and fast-moving objects.
312///
313/// By default, swept CCD considers both translational and rotational motion, and is used
314/// against all types of rigid bodies and colliders. This behavior can be modified
315/// by changing [`SweptCcd::mode`] and other properties.
316///
317/// CCD is generally not useful for large or slow objects, as they are less likely
318/// to miss collisions. It is recommended to only use swept CCD when necessary,
319/// as it is more expensive than [speculative collision](self#speculative-collision) and discrete collision detection.
320///
321/// Read the [module-level documentation](self) for more information about what CCD is,
322/// what it is used for, and what limitations and tradeoffs it can have.
323///
324/// # Example
325///
326/// ```
327#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
328#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
329/// use bevy::prelude::*;
330///
331/// # #[cfg(feature = "f32")]
332/// fn setup(mut commands: Commands) {
333///     // Spawn a dynamic rigid body with swept CCD, travelling towards the right at a high speed.
334///     // The default CCD configuration considers both translational and rotational motion.
335///     commands.spawn((
336///         RigidBody::Dynamic,
337///         SweptCcd::default(),
338#[cfg_attr(feature = "2d", doc = "        LinearVelocity(Vec2::X * 100.0),")]
339#[cfg_attr(feature = "3d", doc = "        LinearVelocity(Vec3::X * 100.0),")]
340#[cfg_attr(feature = "2d", doc = "        Collider::circle(0.1),")]
341#[cfg_attr(feature = "3d", doc = "        Collider::sphere(0.1),")]
342///         Transform::from_xyz(-10.0, 3.0, 0.0),
343///     ));
344///
345///     // Spawn another dynamic rigid body with swept CCD, but this time only considering
346///     // linear motion and not rotation.
347///     commands.spawn((
348///         RigidBody::Dynamic,
349///         SweptCcd::LINEAR, // or `SweptCcd::new_with_mode(SweepMode::Linear)`
350#[cfg_attr(feature = "2d", doc = "        LinearVelocity(Vec2::X * 100.0),")]
351#[cfg_attr(feature = "3d", doc = "        LinearVelocity(Vec3::X * 100.0),")]
352#[cfg_attr(feature = "2d", doc = "        Collider::circle(0.1),")]
353#[cfg_attr(feature = "3d", doc = "        Collider::sphere(0.1),")]
354///         Transform::from_xyz(-10.0, -3.0, 0.0),
355///     ));
356///
357///     // Spawn a thin, long object rotating at a high speed.
358///     // The first ball should hit it, but the second one does not consider
359///     // rotational motion, and most likely passes through.
360///     commands.spawn((
361///         RigidBody::Dynamic,
362///         LockedAxes::TRANSLATION_LOCKED,
363#[cfg_attr(feature = "2d", doc = "        AngularVelocity(100.0),")]
364#[cfg_attr(feature = "3d", doc = "        AngularVelocity(Vec3::Z * 100.0),")]
365#[cfg_attr(feature = "2d", doc = "        Collider::rectangle(0.2, 10.0),")]
366#[cfg_attr(feature = "3d", doc = "        Collider::cuboid(0.2, 10.0, 10.0),")]
367///     ));
368///
369///     // Spawn another thin, long object, this time not rotating.
370///     // The second ball should now hit this.
371///     commands.spawn((
372///         RigidBody::Static,
373#[cfg_attr(feature = "2d", doc = "        Collider::rectangle(0.2, 10.0),")]
374#[cfg_attr(feature = "3d", doc = "        Collider::cuboid(0.2, 10.0, 10.0),")]
375///         Transform::from_xyz(15.0, 0.0, 0.0),
376///     ));
377/// }
378/// ```
379#[derive(Component, Clone, Copy, Debug, PartialEq, Reflect)]
380#[reflect(Component)]
381pub struct SweptCcd {
382    /// The type of sweep used for swept CCD.
383    ///
384    /// If two entities with different sweep modes collide, [`SweepMode::NonLinear`]
385    /// is preferred.
386    ///
387    /// The default is [`SweepMode::NonLinear`], which considers both translational
388    /// and rotational motion.
389    pub mode: SweepMode,
390    /// If `true`, swept CCD is performed against dynamic rigid bodies.
391    /// Otherwise, it is only performed against static geometry and kinematic bodies.
392    ///
393    /// The default is `true`.
394    pub include_dynamic: bool,
395    /// Determines how fast two bodies must be moving relative to each other
396    /// for swept CCD to be activated for them.
397    ///
398    /// If the linear velocity is below this threshold, CCD is skipped,
399    /// unless the angular velocity exceeds the `angular_threshold`.
400    ///
401    /// The default is `0.0`, meaning that CCD is performed regardless of the relative velocity.
402    pub linear_threshold: Scalar,
403    /// Determines how fast two bodies must be rotating relative to each other
404    /// for swept CCD to be activated for them.
405    ///
406    /// If the angular velocity is below this threshold, CCD is skipped,
407    /// unless the linear velocity exceeds the `linear_threshold`.
408    ///
409    /// The default is `0.0`, meaning that CCD is performed regardless of the relative velocity.
410    pub angular_threshold: Scalar,
411}
412
413impl Default for SweptCcd {
414    fn default() -> Self {
415        Self::NON_LINEAR
416    }
417}
418
419impl SweptCcd {
420    /// Continuous Collision Detection with [`SweepMode::Linear`].
421    ///
422    /// This only takes into account translational motion, and can lead to tunneling
423    /// against thin, fast-spinning objects.
424    pub const LINEAR: Self = Self::new_with_mode(SweepMode::Linear);
425
426    /// Continuous Collision Detection with [`SweepMode::NonLinear`].
427    ///
428    /// This takes into account both translational and rotational motion.
429    pub const NON_LINEAR: Self = Self::new_with_mode(SweepMode::NonLinear);
430
431    /// Creates a new [`SweptCcd`] configuration with the given [`SweepMode`].
432    #[inline]
433    pub const fn new_with_mode(mode: SweepMode) -> Self {
434        Self {
435            mode,
436            include_dynamic: true,
437            linear_threshold: 0.0,
438            angular_threshold: 0.0,
439        }
440    }
441
442    /// Sets the linear and angular velocity thresholds in `self`,
443    /// determining how fast two bodies must be moving relative to each other
444    /// for swept CCD to be activated for them.
445    ///
446    /// CCD will be active if either of the two thresholds is exceeded.
447    #[inline]
448    pub const fn with_velocity_threshold(mut self, linear: Scalar, angular: Scalar) -> Self {
449        self.linear_threshold = linear;
450        self.angular_threshold = angular;
451        self
452    }
453
454    /// Sets whether swept CCD is performed against dynamic rigid bodies.
455    /// If `false`, it is only performed against static geometry and kinematic bodies.
456    #[inline]
457    pub const fn include_dynamic(mut self, should_include: bool) -> Self {
458        self.include_dynamic = should_include;
459        self
460    }
461}
462
463/// The algorithm used for [Swept Continuous Collision Detection](self#swept-ccd).
464///
465/// If two entities with different sweep modes collide, [`SweepMode::NonLinear`]
466/// is preferred.
467///
468/// The default is [`SweepMode::NonLinear`], which considers both translational
469/// and rotational motion.
470#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Reflect)]
471pub enum SweepMode {
472    /// [Swept CCD](self#swept-ccd) is performed using linear time-of-impact queries
473    /// from the previous positions of [`SweptCcd`] bodies to their current positions,
474    /// stopping the bodies at the first time of impact.
475    ///
476    /// This mode only considers translational motion, and can lead to tunneling
477    /// against thin, fast-spinning objects. For the more expensive version
478    /// that also considers rotational motion, consider using [`SweepMode::NonLinear`].
479    Linear,
480
481    /// [Swept CCD](self#swept-ccd) is performed using non-linear time-of-impact queries
482    /// from the previous positions of [`SweptCcd`] bodies to their current positions,
483    /// stopping the bodies at the first time of impact.
484    ///
485    /// This mode considers both translational and rotational motion.
486    /// For the cheaper version that only considers translational motion,
487    /// consider using [`SweepMode::Linear`].
488    #[default]
489    NonLinear,
490}
491
492#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
493#[derive(QueryData)]
494#[query_data(mutable)]
495struct SweptCcdBodyQuery {
496    entity: Entity,
497    rb: &'static RigidBody,
498    pos: &'static Position,
499    translation: Option<&'static mut AccumulatedTranslation>,
500    rot: &'static mut Rotation,
501    prev_rot: Option<&'static mut PreviousRotation>,
502    lin_vel: Option<&'static LinearVelocity>,
503    ang_vel: Option<&'static AngularVelocity>,
504    ccd: Option<&'static SweptCcd>,
505    collider: &'static Collider,
506    com: &'static ComputedCenterOfMass,
507}
508
509/// Performs [sweep-based mContinuous Collision Detection](self#swept-ccd)
510/// by performing time-of-impact queries from the previous positions of [`SweptCcd`] bodies
511/// to their current positions, stopping the bodies at the first time of impact.
512///
513/// This approach can lead to "time loss" or "time stealing", because the bodies
514/// are essentially moved back in time, making them appear to momentarily move slower.
515/// Secondary contacts are also not accounted for.
516#[allow(clippy::useless_conversion)]
517#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
518fn solve_swept_ccd(
519    ccd_query: Query<Entity, With<SweptCcd>>,
520    bodies: Query<SweptCcdBodyQuery>,
521    colliders: Query<(&Collider, &ColliderOf)>,
522    time: Res<Time>,
523    contact_graph: Res<ContactGraph>,
524    narrow_phase_config: Res<NarrowPhaseConfig>,
525    mut diagnostics: ResMut<SolverDiagnostics>,
526) {
527    let start = crate::utils::Instant::now();
528
529    let delta_secs = time.delta_seconds_adjusted();
530
531    // TODO: Parallelize.
532    for entity in &ccd_query {
533        // Get the CCD body.
534        let Ok(SweptCcdBodyQueryItem {
535            pos: pos1,
536            translation: Some(mut translation1),
537            rot: mut rot1,
538            prev_rot: Some(prev_rot),
539            lin_vel: Some(lin_vel1),
540            ang_vel: Some(ang_vel1),
541            ccd: Some(ccd1),
542            collider: collider1,
543            com: com1,
544            ..
545        }) = (
546            // Safety: `get_unchecked` is only unsafe if there are multiple mutable references
547            //         to the same component, and this is ensured not to happen when iterating
548            //         over the AABB intersections below.
549            unsafe { bodies.get_unchecked(entity) }
550        )
551        else {
552            continue;
553        };
554
555        // The smallest time of impact found. Starts at the largest value,
556        // how long a body moves during a single frame due to position integration.
557        let (mut min_toi, mut min_toi_entity) = (delta_secs, None);
558
559        // Iterate through colliders intersecting the AABB of the CCD body.
560        let intersecting_entities = contact_graph.entities_colliding_with(entity);
561        for (collider2, &ColliderOf { body: entity2 }) in colliders.iter_many(intersecting_entities)
562        {
563            debug_assert_ne!(entity, entity2, "collider AABB cannot intersect itself");
564
565            // Get the body associated with the collider.
566            // Safety: `AabbIntersections` should never contain the entity of a collider
567            //         attached to the first body, and the entities are also ensured to be different above.
568            if let Ok(body2) = unsafe { bodies.get_unchecked(entity2) } {
569                if !ccd1.include_dynamic && body2.rb.is_dynamic() {
570                    continue;
571                }
572
573                let lin_vel2 = body2.lin_vel.copied().unwrap_or_default().0;
574                let ang_vel2 = body2.ang_vel.copied().unwrap_or_default().0;
575
576                #[cfg(feature = "2d")]
577                let ang_vel_below_threshold =
578                    (ang_vel1.0 - ang_vel2).abs() < ccd1.angular_threshold;
579                #[cfg(feature = "3d")]
580                let ang_vel_below_threshold =
581                    (ang_vel1.0 - ang_vel2).length_squared() < ccd1.angular_threshold.powi(2);
582
583                // If both the relative linear and relative angular velocity
584                // are below the defined thresholds, skip this body.
585                if ang_vel_below_threshold
586                    && (lin_vel1.0 - lin_vel2).length_squared() < ccd1.linear_threshold.powi(2)
587                {
588                    continue;
589                }
590
591                let iso1 = make_isometry(pos1.0, prev_rot.0);
592                let iso2 = make_isometry(
593                    body2.pos.0,
594                    body2.prev_rot.as_ref().map_or(*body2.rot, |rot| rot.0),
595                );
596
597                // TODO: Support child colliders
598                let motion1 = NonlinearRigidMotion::new(
599                    iso1,
600                    com1.0.into(),
601                    lin_vel1.0.into(),
602                    ang_vel1.0.into(),
603                );
604                let motion2 = NonlinearRigidMotion::new(
605                    iso2,
606                    body2.com.0.into(),
607                    lin_vel2.into(),
608                    ang_vel2.into(),
609                );
610
611                let sweep_mode = if ccd1.mode == SweepMode::Linear
612                    && body2.ccd.is_none_or(|ccd| ccd.mode == SweepMode::Linear)
613                {
614                    SweepMode::Linear
615                } else {
616                    SweepMode::NonLinear
617                };
618
619                if let Some(toi) = compute_ccd_toi(
620                    sweep_mode,
621                    &motion1,
622                    collider1,
623                    &motion2,
624                    collider2,
625                    min_toi,
626                    narrow_phase_config.default_speculative_margin,
627                ) {
628                    min_toi = toi;
629                    min_toi_entity = Some(entity2);
630                }
631            }
632        }
633
634        // Advance the bodies from the previous poses to the first time of impact.
635        if let Some(Ok(mut body2)) =
636            min_toi_entity.map(|entity| unsafe { bodies.get_unchecked(entity) })
637        {
638            let collider_lin_vel = body2.lin_vel.copied().unwrap_or_default().0;
639            let collider_ang_vel = body2.ang_vel.copied().unwrap_or_default().0;
640
641            // Overshoot slightly to make sure the bodies advance and don't get stuck.
642            let min_toi = min_toi * 1.0001;
643
644            translation1.0 = min_toi * lin_vel1.0;
645
646            // TODO: Abstract the integration logic to reuse it here
647            #[cfg(feature = "2d")]
648            {
649                *rot1 = prev_rot.0 * Rotation::radians(ang_vel1.0 * min_toi);
650            }
651            #[cfg(feature = "3d")]
652            {
653                let delta_rot = Quaternion::from_scaled_axis(ang_vel1.0 * min_toi);
654                rot1.0 = delta_rot * prev_rot.0 .0;
655                *rot1 = rot1.fast_renormalize();
656            }
657
658            if let Some(mut collider_translation) = body2.translation {
659                collider_translation.0 = min_toi * collider_lin_vel;
660            }
661            if let Some(ref collider_prev_rot) = body2.prev_rot {
662                #[cfg(feature = "2d")]
663                {
664                    *body2.rot =
665                        collider_prev_rot.0 * Rotation::radians(collider_ang_vel * min_toi);
666                }
667                #[cfg(feature = "3d")]
668                {
669                    let delta_rot = Quaternion::from_scaled_axis(collider_ang_vel * min_toi);
670
671                    body2.rot.0 = delta_rot * collider_prev_rot.0 .0;
672                    *body2.rot = body2.rot.fast_renormalize();
673                }
674            }
675        }
676    }
677
678    diagnostics.swept_ccd += start.elapsed();
679}
680
681/// Computes the time of impact for the motion of two objects for Continuous Collision Detection.
682/// If the TOI is larger than `min_toi` or the shapes never touch, `None` is returned.
683#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
684fn compute_ccd_toi(
685    mode: SweepMode,
686    motion1: &NonlinearRigidMotion,
687    collider1: &Collider,
688    motion2: &NonlinearRigidMotion,
689    collider2: &Collider,
690    min_toi: Scalar,
691    prediction_distance: Scalar,
692) -> Option<Scalar> {
693    if mode == SweepMode::Linear {
694        if let Ok(Some(ShapeCastHit {
695            time_of_impact: toi,
696            ..
697        })) = cast_shapes(
698            &motion1.start,
699            &motion1.linvel,
700            collider1.shape_scaled().as_ref(),
701            &motion2.start,
702            &motion2.linvel,
703            collider2.shape_scaled().as_ref(),
704            ShapeCastOptions {
705                max_time_of_impact: min_toi,
706                stop_at_penetration: false,
707                ..default()
708            },
709        ) {
710            if toi > 0.0 && toi < min_toi {
711                // New smaller time of impact found
712                return Some(toi);
713            } else if toi == 0.0 {
714                // If the time of impact is zero, fall back to computing the TOI of a small circle
715                // around the centroid of the first body.
716                if let Ok(Some(ShapeCastHit {
717                    time_of_impact: toi,
718                    ..
719                })) = cast_shapes(
720                    &motion1.start,
721                    &motion1.linvel,
722                    collider1.shape_scaled().as_ref(),
723                    &motion2.start,
724                    &motion2.linvel,
725                    &parry::shape::Ball::new(prediction_distance),
726                    ShapeCastOptions {
727                        max_time_of_impact: min_toi,
728                        stop_at_penetration: false,
729                        ..default()
730                    },
731                ) {
732                    if toi > 0.0 && toi < min_toi {
733                        // New smaller time of impact found
734                        return Some(toi);
735                    }
736                }
737            }
738        }
739    } else if let Ok(Some(ShapeCastHit {
740        time_of_impact: toi,
741        ..
742    })) = cast_shapes_nonlinear(
743        motion1,
744        collider1.shape_scaled().as_ref(),
745        motion2,
746        collider2.shape_scaled().as_ref(),
747        0.0,
748        min_toi,
749        false,
750    ) {
751        if toi > 0.0 && toi < min_toi {
752            // New smaller time of impact found
753            return Some(toi);
754        } else if toi == 0.0 {
755            // If the time of impact is zero, fall back to computing the TOI of a small circle
756            // around the centroid of the first body.
757            if let Ok(Some(ShapeCastHit {
758                time_of_impact: toi,
759                ..
760            })) = cast_shapes_nonlinear(
761                motion1,
762                collider1.shape_scaled().as_ref(),
763                motion2,
764                &parry::shape::Ball::new(prediction_distance),
765                0.0,
766                min_toi,
767                false,
768            ) {
769                if toi > 0.0 && toi < min_toi {
770                    // New smaller time of impact found
771                    return Some(toi);
772                }
773            }
774        }
775    }
776
777    None
778}