avian3d/dynamics/rigid_body/mass_properties/components/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
//! Mass property components.
use crate::prelude::*;
use bevy::{
ecs::{component::ComponentId, world::DeferredWorld},
prelude::*,
};
#[cfg(feature = "3d")]
use bevy_heavy::AngularInertiaTensor;
use derive_more::From;
mod collider;
pub use collider::*;
mod computed;
pub use computed::*;
/// An error returned for an invalid mass.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MassError {
/// The mass is negative.
Negative,
/// The mass is NaN.
NaN,
}
/// The [mass] of an entity, representing resistance to linear acceleration.
/// A higher mass requires more force for the same acceleration.
///
/// If [`Mass`] is not present, but the entity has a [`Collider`], its [`ColliderMassProperties`]
/// computed based on the shape and [`ColliderDensity`] will be used instead.
///
/// The [`Mass`] component does *not* take into account the masses of child entities, and it is never modified
/// by the engine. The total mass of a dynamic [rigid body] that *does* consider child entities and colliders
/// is stored in the [`ComputedMass`] component. It is updated automatically when mass properties are changed,
/// or when colliders are added or removed.
///
/// A total mass of zero is a special case, and is interpreted as infinite mass, meaning the rigid body
/// will not be affected by any forces.
///
/// [mass]: https://en.wikipedia.org/wiki/Mass
/// [rigid body]: RigidBody
///
/// # Usage
///
/// The [`Mass`] component can be used to define the mass of a [rigid body] entity or its descendants:
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// Mass(5.0),
/// ));
/// # }
/// ```
///
/// If no [`Mass`] is present, the [`ComputedMass`] will be computed from the collider
/// based on its shape and [`ColliderDensity`].
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Note: `ColliderDensity` is optional, and defaults to `1.0` if not present.
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// ColliderDensity(2.0),
/// ));
/// # }
/// ```
///
/// If the rigid body has child colliders, their masses will be added to the total [`ComputedMass`].
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Total mass: 10.0 + 5.0 = 15.0
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// Mass(10.0),
/// ))
#[cfg_attr(
feature = "2d",
doc = ".with_child((Collider::circle(1.0), Mass(5.0)));"
)]
#[cfg_attr(
feature = "3d",
doc = ".with_child((Collider::sphere(1.0), Mass(5.0)));"
)]
/// # }
/// ```
///
/// To prevent masses of child entities from contributing to the total [`ComputedMass`],
/// add the [`NoAutoMass`] component. This can be useful when full control over mass is desired.
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Total mass: 10.0
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// Mass(10.0),
/// NoAutoMass,
/// ))
#[cfg_attr(
feature = "2d",
doc = ".with_child((Collider::circle(1.0), Mass(5.0)));"
)]
#[cfg_attr(
feature = "3d",
doc = ".with_child((Collider::sphere(1.0), Mass(5.0)));"
)]
/// # }
/// ```
///
/// # Mass Updates
///
/// The [`Mass`] component is never modified by the engine, so you can safely update it at any time.
///
/// The total [`ComputedMass`] is updated automatically whenever the mass properties of a [rigid body]
/// or its descendants change, or when colliders are added or removed. This update is triggered by adding
/// the [`RecomputeMassProperties`] component, which is removed after the update is performed
/// in [`MassPropertySystems::UpdateComputedMassProperties`](super::MassPropertySystems::UpdateComputedMassProperties).
///
/// To immediately perform a manual update of the total mass properties for a specific rigid body entity,
/// you can call [`MassPropertyHelper::update_mass_properties`] in a system.
///
/// # Related Types
///
/// - [`ComputedMass`] stores the total mass of a dynamic [rigid body] that considers child entities and colliders.
/// - [`NoAutoMass`] disables masses of child entities being taken into account for the [`ComputedMass`].
/// - [`AngularInertia`] is the rotational equivalent of mass, representing resistance to angular acceleration.
/// - [`CenterOfMass`] is the local point where the mass is concentrated. Applying forces at this point produces no torque.
/// - [`MassPropertiesBundle`] is a bundle containing mass properties.
/// - [`MassPropertyHelper`] is a [`SystemParam`] with utilities for computing and updating mass properties.
///
/// [`SystemParam`]: bevy::ecs::system::SystemParam
#[derive(Reflect, Clone, Copy, Component, Debug, Default, Deref, DerefMut, PartialEq, From)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, Default, PartialEq)]
pub struct Mass(pub f32);
impl Mass {
/// A mass of `0.0`.
pub const ZERO: Self = Self(0.0);
/// Computes the [`Mass`] of the given shape using the given density.
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// // Compute the mass from a collider with a density of `2.0`.
#[cfg_attr(
feature = "2d",
doc = "let mass = Mass::from_shape(&Collider::circle(1.0), 2.0);"
)]
#[cfg_attr(
feature = "3d",
doc = "let mass = Mass::from_shape(&Collider::sphere(1.0), 2.0);"
)]
///
/// // Bevy's primitive shapes can also be used.
#[cfg_attr(
feature = "2d",
doc = "let mass = Mass::from_shape(&Circle::new(1.0), 2.0);"
)]
#[cfg_attr(
feature = "3d",
doc = "let mass = Mass::from_shape(&Sphere::new(1.0), 2.0);"
)]
/// ```
#[cfg(all(
feature = "default-collider",
any(feature = "parry-f32", feature = "parry-f64")
))]
#[inline]
pub fn from_shape<T: ComputeMassProperties>(shape: &T, density: f32) -> Self {
Self(shape.mass(density))
}
}
// TODO: Add errors for asymmetric and non-positive definite matrices in 3D.
/// An error returned for an invalid angular inertia.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AngularInertiaError {
/// The angular inertia is negative.
Negative,
/// The angular inertia is NaN.
NaN,
}
/// The [angular inertia] of an entity, representing resistance to angular acceleration.
/// A higher angular inertia requires more torque for the same acceleration.
///
/// If [`AngularInertia`] is not present, but the entity has a [`Collider`], its [`ColliderMassProperties`]
/// computed based on the shape and will be used instead. Angular inertia scales with mass,
/// so a higher [`Mass`] or [`ColliderDensity`] will result in higher inertia.
///
/// The [`AngularInertia`] component does *not* take into account the inertia of child entities, and it is never modified
/// by the engine. The total angular inertia of a dynamic [rigid body] that *does* consider child entities and colliders
/// is stored in the [`ComputedAngularInertia`] component. It is updated automatically when mass properties are changed,
/// or when colliders are added or removed.
///
/// A total angular inertia of zero is a special case, and is interpreted as infinite inertia, meaning the rigid body
/// will not be affected by any torque.
///
/// [angular inertia]: https://en.wikipedia.org/wiki/Moment_of_inertia
/// [rigid body]: RigidBody
///
/// # Usage
///
/// The [`AngularInertia`] component can be used to define the angular inertia of a [rigid body]
/// entity or its descendants:
///
/// ```
/// # use avian2d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// AngularInertia(2.0),
/// ));
/// # }
/// ```
///
/// If no [`AngularInertia`] is present, the [`ComputedAngularInertia`] will be computed from the collider
/// based on its mass and shape.
///
/// ```
/// # use avian2d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Note: `ColliderDensity` is optional, and defaults to `1.0` if not present.
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// ColliderDensity(2.0),
/// ));
/// # }
/// ```
///
/// If the rigid body has child colliders, their angular inertia will be added to the total [`ComputedAngularInertia`].
///
/// ```
/// # use avian2d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Total angular inertia: 3.0 + 2.0 = 5.0
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// AngularInertia(3.0),
/// ))
/// .with_child((Collider::circle(1.0), AngularInertia(2.0)));
/// # }
/// ```
///
/// To prevent angular inertia of child entities from contributing to the total [`ComputedAngularInertia`],
/// add the [`NoAutoAngularInertia`] component. This can be useful when full control over inertia is desired.
///
/// ```
/// # use avian2d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Total angular inertia: 3.0
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// AngularInertia(3.0),
/// NoAutoAngularInertia,
/// ))
/// .with_child((Collider::circle(1.0), AngularInertia(2.0)));
/// # }
/// ```
///
/// # Angular Inertia Updates
///
/// The [`AngularInertia`] component is never modified by the engine, so you can safely update it at any time.
///
/// The total [`ComputedAngularInertia`] is updated automatically whenever the mass properties of a [rigid body]
/// or its descendants change, or when colliders are added or removed. This update is triggered by adding
/// the [`RecomputeMassProperties`] component, which is removed after the update is performed
/// in [`MassPropertySystems::UpdateComputedMassProperties`](super::MassPropertySystems::UpdateComputedMassProperties).
///
/// To immediately perform a manual update of the total mass properties for a specific rigid body entity,
/// you can call [`MassPropertyHelper::update_mass_properties`] in a system.
///
/// # Related Types
///
/// - [`ComputedAngularInertia`] stores the total angular inertia of a dynamic [rigid body] that considers child entities and colliders.
/// - [`NoAutoAngularInertia`] disables the mass properties of child entities being taken into account for the [`ComputedAngularInertia`].
/// - [`Mass`] is the linear equivalent of angular inertia, representing resistance to linear acceleration.
/// - [`CenterOfMass`] is the local point where the mass is concentrated. Applying forces at this point produces no torque.
/// - [`MassPropertiesBundle`] is a bundle containing mass properties.
/// - [`MassPropertyHelper`] is a [`SystemParam`] with utilities for computing and updating mass properties.
///
/// [`SystemParam`]: bevy::ecs::system::SystemParam
#[cfg(feature = "2d")]
#[derive(Reflect, Clone, Copy, Component, Debug, Default, Deref, DerefMut, PartialEq, From)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, Default, PartialEq)]
#[doc(alias = "MomentOfInertia")]
pub struct AngularInertia(pub f32);
#[cfg(feature = "2d")]
impl AngularInertia {
/// An angular inertia of `0.0`.
pub const ZERO: Self = Self(0.0);
/// Computes the [`AngularInertia`] of the given shape using the given mass.
///
/// ```
/// # use avian2d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// // Compute the angular inertia from a collider with a mass of `2.0`.
/// let inertia = AngularInertia::from_shape(&Collider::circle(1.0), 2.0);
///
/// // Bevy's primitive shapes can also be used.
/// let inertia = AngularInertia::from_shape(&Circle::new(1.0), 2.0);
/// ```
#[cfg(all(
feature = "default-collider",
any(feature = "parry-f32", feature = "parry-f64")
))]
#[inline]
pub fn from_shape<T: ComputeMassProperties>(shape: &T, mass: f32) -> Self {
Self(shape.angular_inertia(mass))
}
/// Computes the angular inertia shifted by the given offset, taking into account the given mass.
#[inline]
pub fn shifted(&self, mass: f32, offset: Vec2) -> f32 {
if mass > 0.0 && mass.is_finite() && offset != Vec2::ZERO {
self.0 + offset.length_squared() * mass
} else {
self.0
}
}
}
/// The local [angular inertia] of an entity, representing resistance to angular acceleration.
/// A higher angular inertia requires more torque for the same acceleration.
///
/// If [`AngularInertia`] is not present, but the entity has a [`Collider`], its [`ColliderMassProperties`]
/// computed based on the shape and [mass] will be used instead. Angular inertia scales with mass,
/// so a higher [`Mass`] or [`ColliderDensity`] will result in higher inertia.
///
/// The [`AngularInertia`] component does *not* take into account the inertia of child entities, and it is never modified
/// by the engine. The total angular inertia of a dynamic [rigid body] that *does* consider child entities and colliders
/// is stored in the [`ComputedAngularInertia`] component. It is updated automatically when mass properties are changed,
/// or when colliders are added or removed.
///
/// A total angular inertia of zero is a special case, and is interpreted as infinite inertia, meaning the rigid body
/// will not be affected by any torques.
///
/// See the [module-level documentation] for more general information on angular inertia.
///
/// [angular inertia]: https://en.wikipedia.org/wiki/Moment_of_inertia
/// [rigid body]: RigidBody
/// [mass]: super#mass
/// [module-level documentation]: super#angular-inertia
///
/// # Usage
///
/// [`AngularInertia`] is defined by the moment of inertia along the *principal axes* (a [`Vec3`])
/// defined by the *local inertial frame* (a [`Quat`]). Most entities will have a local inertial frame
/// of [`Quat::IDENTITY`], which means that the principal axes are aligned with the entity's local axes.
///
/// ```
/// # use avian3d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # #[cfg(feature = "f32")]
/// # fn setup(mut commands: Commands) {
/// // Principal angular inertia: `2.0` for the local X and Z axes, and `5.0` for the Y axis.
/// let inertia1 = AngularInertia::new(Vec3::new(2.0, 5.0, 2.0));
///
/// // A local inertial frame rotated by 90 degrees about the X axis.
/// let inertia2 = AngularInertia::new_with_local_frame(Vec3::new(2.0, 5.0, 2.0), Quat::from_rotation_x(PI / 2.0));
/// # }
/// # #[cfg(not(feature = "f32"))]
/// # fn main() {}
///
/// ```
///
/// [`AngularInertia`] can also be created from a symmetric 3x3 matrix known as the *[angular inertia tensor]*.
/// It summarizes all moments of inertia of an object in a single matrix, and can be used to perform
/// computations with angular inertia, but is often not constructed manually.
///
/// [angular inertia tensor]: AngularInertiaTensor
///
/// ```
/// # use avian3d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # #[cfg(feature = "f32")]
/// # fn setup(mut commands: Commands) {
/// // For simplicity, we use the same principal angular inertia as before, with an identity local frame.
/// let inertia1 = AngularInertia::from_tensor(Mat3::from_diagonal(Vec3::new(2.0, 5.0, 2.0)));
///
/// // The angular inertia tensor can be retrieved back from the principal angular inertia.
/// let tensor = inertia1.tensor();
/// # }
/// # #[cfg(not(feature = "f32"))]
/// # fn main() {}
/// ```
///
/// The [`AngularInertia`] component can be used to define the angular inertia of a [rigid body]
/// entity or its descendants:
///
/// ```
/// # use avian3d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # #[cfg(feature = "f32")]
/// # fn setup(mut commands: Commands) {
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// AngularInertia::new(Vec3::new(2.0, 5.0, 2.0)),
/// ));
/// # }
/// # #[cfg(not(feature = "f32"))]
/// # fn main() {}
/// ```
///
/// If no [`AngularInertia`] is present, the [`ComputedAngularInertia`] will be computed from the collider
/// based on its mass and shape.
///
/// ```
/// # use avian3d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Note: `ColliderDensity` is optional, and defaults to `1.0` if not present.
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// ColliderDensity(2.0),
/// ));
/// # }
/// ```
///
/// If the rigid body has child colliders, their angular inertia will be added to the total [`ComputedAngularInertia`].
/// Here both entities are at the origin, but offsets relative to the center of mass would also impact the total inertia.
///
/// ```
/// # use avian3d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # #[cfg(feature = "f32")]
/// # fn setup(mut commands: Commands) {
/// // Total angular inertia: [2.0, 5.0, 2.0] + [1.0, 2.0, 1.0] = [3.0, 7.0, 3.0]
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// AngularInertia::new(Vec3::new(2.0, 5.0, 2.0)),
/// ))
/// .with_child((Collider::sphere(1.0), AngularInertia::new(Vec3::new(1.0, 2.0, 1.0))));
/// # }
/// # #[cfg(not(feature = "f32"))]
/// # fn main() {}
/// ```
///
/// To prevent angular inertia of child entities from contributing to the total [`ComputedAngularInertia`],
/// add the [`NoAutoAngularInertia`] component. This can be useful when full control over inertia is desired.
///
/// ```
/// # use avian3d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// # #[cfg(feature = "f32")]
/// # fn setup(mut commands: Commands) {
/// // Total angular inertia: [2.0, 5.0, 2.0]
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// AngularInertia::new(Vec3::new(2.0, 5.0, 2.0)),
/// NoAutoAngularInertia,
/// ))
/// .with_child((Collider::sphere(1.0), AngularInertia::new(Vec3::new(1.0, 2.0, 1.0))));
/// # }
/// # #[cfg(not(feature = "f32"))]
/// # fn main() {}
/// ```
///
/// # Angular Inertia Updates
///
/// The [`AngularInertia`] component is never modified by the engine, so you can safely update it at any time.
///
/// The total [`ComputedAngularInertia`] is updated automatically whenever the mass properties of a [rigid body]
/// or its descendants change, or when colliders are added or removed. This update is triggered by adding
/// the [`RecomputeMassProperties`] component, which is removed after the update is performed
/// in [`MassPropertySystems::UpdateComputedMassProperties`](super::MassPropertySystems::UpdateComputedMassProperties).
///
/// To immediately perform a manual update of the total mass properties for a specific rigid body entity,
/// you can call [`MassPropertyHelper::update_mass_properties`] in a system.
///
/// # Related Types
///
/// - [`ComputedAngularInertia`] stores the total angular inertia of a dynamic [rigid body] that considers child entities and colliders.
/// - [`NoAutoAngularInertia`] disables the mass properties of child entities being taken into account for the [`ComputedAngularInertia`].
/// - [`AngularInertiaTensor`] is the symmetric 3x3 matrix representation of angular inertia.
/// - [`Mass`] is the linear equivalent of angular inertia, representing resistance to linear acceleration.
/// - [`CenterOfMass`] is the local point where the mass is concentrated. Applying forces at this point produces no torque.
/// - [`MassPropertiesBundle`] is a bundle containing mass properties.
/// - [`MassPropertyHelper`] is a [`SystemParam`] with utilities for computing and updating mass properties.
///
/// [`SystemParam`]: bevy::ecs::system::SystemParam
#[cfg(feature = "3d")]
#[derive(Reflect, Clone, Copy, Component, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, PartialEq)]
#[doc(alias = "MomentOfInertia")]
pub struct AngularInertia {
/// The principal angular inertia, representing resistance to angular acceleration
/// about the local coordinate axes defined by the `local_frame`.
pub principal: Vec3,
/// The orientation of the local inertial frame.
pub local_frame: Quat,
}
#[cfg(feature = "3d")]
impl AngularInertia {
/// An angular inertia of `0.0` for all axes, with an identity local frame.
pub const ZERO: Self = Self {
principal: Vec3::ZERO,
local_frame: Quat::IDENTITY,
};
/// Creates a new [`AngularInertia`] from the given principal angular inertia.
///
/// The principal angular inertia represents resistance to angular acceleration
/// about the local coordinate axes.
///
/// To specify the orientation of the local inertial frame, consider using [`AngularInertia::new_with_local_frame`].
///
/// # Panics
///
/// Panics if any component of the principal angular inertia is negative or NaN when `debug_assertions` are enabled.
#[inline]
#[doc(alias = "from_principal_angular_inertia")]
pub fn new(principal_angular_inertia: Vec3) -> Self {
debug_assert!(
principal_angular_inertia.cmpge(Vec3::ZERO).all()
&& !principal_angular_inertia.is_nan(),
"principal angular inertia must be positive or zero for all axes"
);
Self {
principal: principal_angular_inertia,
local_frame: Quat::IDENTITY,
}
}
/// Tries to create a new [`AngularInertia`] from the given principal angular inertia.
///
/// The principal angular inertia represents resistance to angular acceleration
/// about the local coordinate axes. To specify the orientation of the local inertial frame,
/// consider using [`AngularInertia::try_new_with_local_frame`].
///
/// # Errors
///
/// Returns [`Err(AngularInertiaError)`](AngularInertiaError) if any component of the principal angular inertia is negative or NaN.
#[inline]
pub fn try_new(principal_angular_inertia: Vec3) -> Result<Self, AngularInertiaError> {
if principal_angular_inertia.is_nan() {
Err(AngularInertiaError::NaN)
} else if !principal_angular_inertia.cmpge(Vec3::ZERO).all() {
Err(AngularInertiaError::Negative)
} else {
Ok(Self {
principal: principal_angular_inertia,
local_frame: Quat::IDENTITY,
})
}
}
/// Creates a new [`AngularInertia`] from the given principal angular inertia
/// and the orientation of the local inertial frame.
///
/// The principal angular inertia represents resistance to angular acceleration
/// about the local coordinate axes defined by the given `local_frame`.
///
/// # Panics
///
/// Panics if any component of the principal angular inertia is negative or NaN when `debug_assertions` are enabled.
#[inline]
#[doc(alias = "from_principal_angular_inertia_with_local_frame")]
pub fn new_with_local_frame(principal_angular_inertia: Vec3, local_frame: Quat) -> Self {
debug_assert!(
principal_angular_inertia.cmpge(Vec3::ZERO).all()
&& !principal_angular_inertia.is_nan(),
"principal angular inertia must be positive or zero for all axes"
);
Self {
principal: principal_angular_inertia,
local_frame,
}
}
/// Tries to create a new [`AngularInertia`] from the given principal angular inertia
/// and the orientation of the local inertial frame.
///
/// The principal angular inertia represents resistance to angular acceleration
/// about the local coordinate axes defined by the given `local_frame`.
///
/// # Errors
///
/// Returns [`Err(AngularInertiaError)`](AngularInertiaError) if any component of the principal angular inertia is negative or NaN.
#[inline]
pub fn try_new_with_local_frame(
principal_angular_inertia: Vec3,
local_frame: Quat,
) -> Result<Self, AngularInertiaError> {
if principal_angular_inertia.is_nan() {
Err(AngularInertiaError::NaN)
} else if !principal_angular_inertia.cmpge(Vec3::ZERO).all() {
Err(AngularInertiaError::Negative)
} else {
Ok(Self {
principal: principal_angular_inertia,
local_frame,
})
}
}
/// Creates a new [`AngularInertia`] from the given [angular inertia tensor].
///
/// The tensor should be symmetric and positive definite.
///
/// [angular inertia tensor]: AngularInertiaTensor
#[inline]
#[doc(alias = "from_mat3")]
pub fn from_tensor(tensor: impl Into<AngularInertiaTensor>) -> Self {
let tensor = tensor.into();
let (principal, local_frame) = tensor.principal_angular_inertia_with_local_frame();
Self {
principal,
local_frame,
}
}
/// Computes the [`AngularInertia`] of the given shape using the given mass.
///
/// ```
/// # use avian3d::prelude::*;
/// # use bevy::prelude::*;
/// #
/// // Compute the angular inertia from collider with a mass of `2.0`.
/// let inertia = AngularInertia::from_shape(&Collider::sphere(1.0), 2.0);
///
/// // Bevy's primitive shapes can also be used.
/// let inertia = AngularInertia::from_shape(&Sphere::new(1.0), 2.0);
/// ```
#[cfg(all(
feature = "default-collider",
any(feature = "parry-f32", feature = "parry-f64")
))]
#[inline]
pub fn from_shape<T: ComputeMassProperties>(shape: &T, mass: f32) -> Self {
let principal = shape.principal_angular_inertia(mass);
let local_frame = shape.local_inertial_frame();
Self::new_with_local_frame(principal, local_frame)
}
/// Returns the [`AngularInertiaTensor`] represented by this principal [`AngularInertia`]
/// and local inertial frame.
#[inline]
pub fn tensor(self) -> AngularInertiaTensor {
AngularInertiaTensor::new_with_local_frame(self.principal, self.local_frame)
}
/// Returns `true` if the principal angular inertia and inertial local frame are neither infinite nor NaN.
#[inline]
pub fn is_finite(self) -> bool {
self.principal.is_finite() && self.local_frame.is_finite()
}
/// Returns `true` if the principal angular inertia or inertial local frame is NaN.
#[inline]
pub fn is_nan(self) -> bool {
self.principal.is_nan() || self.local_frame.is_nan()
}
}
#[cfg(feature = "3d")]
impl From<Mat3> for AngularInertia {
fn from(tensor: Mat3) -> Self {
Self::from_tensor(tensor)
}
}
#[cfg(feature = "3d")]
impl From<AngularInertiaTensor> for AngularInertia {
fn from(tensor: AngularInertiaTensor) -> Self {
Self::from_tensor(tensor)
}
}
#[cfg(feature = "3d")]
impl From<AngularInertia> for AngularInertiaTensor {
fn from(inertia: AngularInertia) -> Self {
inertia.tensor()
}
}
/// The local [center of mass] of an entity, representing the average position of mass
/// in the object. Applying forces at this point produces no torque.
///
/// If [`CenterOfMass`] is not present, but the entity has a [`Collider`], its [`ColliderMassProperties`]
/// computed based on the shape will be used instead.
///
/// The [`CenterOfMass`] component does *not* take into account child entities, and it is never modified
/// by the engine. The total center of mass of a dynamic [rigid body] that *does* consider child entities and colliders
/// is stored in the [`ComputedCenterOfMass`] component. It is updated automatically when mass properties are changed,
/// or when colliders are added or removed.
///
/// [center of mass]: https://en.wikipedia.org/wiki/Center_of_mass
/// [rigid body]: RigidBody
///
/// # Usage
///
/// The [`CenterOfMass`] component can be used to define the center of mass of a [rigid body] entity or its descendants:
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // An offset of `-0.5` along the Y axis.
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
#[cfg_attr(feature = "2d", doc = " CenterOfMass::new(0.0, -0.5),")]
#[cfg_attr(feature = "3d", doc = " CenterOfMass::new(0.0, -0.5, 0.0),")]
/// ));
/// # }
/// ```
///
/// If no [`CenterOfMass`] is present, the [`ComputedCenterOfMass`] will be computed from the collider
/// based on its shape.
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // For a capsule, the center of mass is at the local origin.
/// commands.spawn((RigidBody::Dynamic, Collider::capsule(0.5, 1.5)));
/// # }
/// ```
///
/// If the rigid body has child colliders, they will contribute to the total [`ComputedCenterOfMass`]
/// based on weighted average of their global centers of mass.
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
#[cfg_attr(
feature = "2d",
doc = "// Total center of mass: (10.0 * [0.0, -0.5] + 5.0 * [0.0, 4.0]) / (10.0 + 5.0) = [0.0, 1.0]"
)]
#[cfg_attr(
feature = "3d",
doc = "// Total center of mass: (10.0 * [0.0, -0.5, 0.0] + 5.0 * [0.0, 4.0, 0.0]) / (10.0 + 5.0) = [0.0, 1.0, 0.0]"
)]
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
/// Mass(10.0),
#[cfg_attr(feature = "2d", doc = " CenterOfMass::new(0.0, -0.5),")]
#[cfg_attr(feature = "3d", doc = " CenterOfMass::new(0.0, -0.5, 0.0),")]
/// Transform::default(),
/// ))
/// .with_child((
#[cfg_attr(feature = "2d", doc = " Collider::circle(1.0),")]
#[cfg_attr(feature = "3d", doc = " Collider::sphere(1.0),")]
/// Mass(5.0),
/// Transform::from_xyz(0.0, 4.0, 0.0),
/// ));
/// # }
/// ```
///
/// To prevent the centers of mass of child entities from contributing to the total [`ComputedCenterOfMass`],
/// add the [`NoAutoCenterOfMass`], component. This can be useful when full control over the center of mass is desired.
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
#[cfg_attr(feature = "2d", doc = "// Total center of mass: [0.0, -0.5]")]
#[cfg_attr(feature = "3d", doc = "// Total center of mass: [0.0, -0.5, 0.0]")]
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::capsule(0.5, 1.5),
#[cfg_attr(feature = "2d", doc = " CenterOfMass::new(0.0, -0.5),")]
#[cfg_attr(feature = "3d", doc = " CenterOfMass::new(0.0, -0.5, 0.0),")]
/// Transform::default(),
/// ))
/// .with_child((
#[cfg_attr(feature = "2d", doc = " Collider::circle(1.0),")]
#[cfg_attr(feature = "3d", doc = " Collider::sphere(1.0),")]
/// Mass(5.0),
/// Transform::from_xyz(0.0, 4.0, 0.0),
/// ));
/// # }
/// ```
///
/// # Center of Mass Updates
///
/// The [`CenterOfMass`] component is never modified by the engine, so you can safely update it at any time.
///
/// The total [`ComputedCenterOfMass`] is updated automatically whenever the mass properties of a [rigid body]
/// or its descendants change, or when colliders are added, removed, or transformed. This update is triggered by adding
/// the [`RecomputeMassProperties`] component, which is removed after the update is performed
/// in [`MassPropertySystems::UpdateComputedMassProperties`](super::MassPropertySystems::UpdateComputedMassProperties).
///
/// To immediately perform a manual update of the total mass properties for a specific rigid body entity,
/// you can call [`MassPropertyHelper::update_mass_properties`] in a system.
///
/// # Related Types
///
/// - [`ComputedCenterOfMass`] stores the total center of mass of a dynamic [rigid body] that considers child entities and colliders.
/// - [`NoAutoCenterOfMass`] disables the centers of mass of child entities being taken into account for the [`ComputedCenterOfMass`].
/// - [`Mass`] represents resistance to linear acceleration.
/// - [`AngularInertia`] is the rotational equivalent of mass, representing resistance to angular acceleration.
/// - [`MassPropertiesBundle`] is a bundle containing mass properties.
/// - [`MassPropertyHelper`] is a [`SystemParam`] with utilities for computing and updating mass properties.
///
/// [`SystemParam`]: bevy::ecs::system::SystemParam
#[derive(Reflect, Clone, Copy, Component, Debug, Default, Deref, DerefMut, PartialEq, From)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, Default, PartialEq)]
pub struct CenterOfMass(pub VectorF32);
impl CenterOfMass {
/// A center of mass set at the local origin.
pub const ZERO: Self = Self(VectorF32::ZERO);
/// Creates a new [`CenterOfMass`] at the given local position.
#[inline]
#[cfg(feature = "2d")]
pub const fn new(x: f32, y: f32) -> Self {
Self(Vec2::new(x, y))
}
/// Creates a new [`CenterOfMass`] at the given local position.
#[inline]
#[cfg(feature = "3d")]
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self(Vec3::new(x, y, z))
}
/// Computes the [`CenterOfMass`] of the given shape.
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// // Compute the center of mass from a collider.
#[cfg_attr(
feature = "2d",
doc = "let center_of_mass = CenterOfMass::from_shape(&Collider::circle(1.0));"
)]
#[cfg_attr(
feature = "3d",
doc = "let center_of_mass = CenterOfMass::from_shape(&Collider::sphere(1.0));"
)]
///
/// // Bevy's primitive shapes can also be used.
#[cfg_attr(
feature = "2d",
doc = "let center_of_mass = CenterOfMass::from_shape(&Circle::new(1.0));"
)]
#[cfg_attr(
feature = "3d",
doc = "let center_of_mass = CenterOfMass::from_shape(&Sphere::new(1.0));"
)]
/// ```
#[cfg(all(
feature = "default-collider",
any(feature = "parry-f32", feature = "parry-f64")
))]
#[inline]
pub fn from_shape<T: ComputeMassProperties>(shape: &T) -> Self {
Self(shape.center_of_mass())
}
}
/// A marker component that prevents descendants or attached colliders
/// from contributing to the total [`ComputedMass`] of a [rigid body].
///
/// Only the [`Mass`] component of the rigid body entity itself will be considered.
/// This is useful when full control over mass is desired.
///
/// [rigid body]: RigidBody
#[derive(Reflect, Clone, Copy, Component, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, Default, PartialEq)]
#[require(RecomputeMassProperties)]
#[component(on_remove = on_remove_no_auto_mass_property)]
pub struct NoAutoMass;
/// A marker component that prevents descendants or attached colliders
/// from contributing to the total [`ComputedAngularInertia`] of a [rigid body].
///
/// Only the [`AngularInertia`] component of the rigid body entity itself will be considered.
/// This is useful when full control over inertia is desired.
///
/// [rigid body]: RigidBody
#[derive(Reflect, Clone, Copy, Component, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, Default, PartialEq)]
#[require(RecomputeMassProperties)]
#[component(on_remove = on_remove_no_auto_mass_property)]
pub struct NoAutoAngularInertia;
/// A marker component that prevents descendants or attached colliders
/// from contributing to the total [`ComputedCenterOfMass`] of a [rigid body].
///
/// Only the [`CenterOfMass`] component of the rigid body entity itself will be considered.
/// This is useful when full control over the center of mass is desired.
///
/// [rigid body]: RigidBody
#[derive(Reflect, Clone, Copy, Component, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, Default, PartialEq)]
#[require(RecomputeMassProperties)]
#[component(on_remove = on_remove_no_auto_mass_property)]
pub struct NoAutoCenterOfMass;
/// Triggers the recomputation of mass properties for rigid bodies when automatic computation is re-enabled.
fn on_remove_no_auto_mass_property(mut world: DeferredWorld, entity: Entity, _id: ComponentId) {
world
.commands()
.entity(entity)
.insert(RecomputeMassProperties);
}
/// A marker component that forces the recomputation of [`ComputedMass`], [`ComputedAngularInertia`]
/// and [`ComputedCenterOfMass`] for a [rigid body].
///
/// This is added automatically when the mass properties of a [rigid body] change, or when colliders are added or removed,
/// and is removed after the recomputation in [`MassPropertySystems::UpdateComputedMassProperties`].
///
/// [rigid body]: RigidBody
/// [`MassPropertySystems::UpdateComputedMassProperties`]: super::MassPropertySystems::UpdateComputedMassProperties
#[derive(Reflect, Clone, Copy, Component, Debug, Default, PartialEq)]
#[component(storage = "SparseSet")]
pub struct RecomputeMassProperties;
/// A bundle containing [mass properties].
///
/// [mass properties]: super
#[allow(missing_docs)]
#[derive(Bundle, Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct MassPropertiesBundle {
pub mass: Mass,
pub angular_inertia: AngularInertia,
pub center_of_mass: CenterOfMass,
}
impl MassPropertiesBundle {
/// Computes the mass properties for a [`Collider`] based on its shape and a given density.
///
/// ```
#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
/// # use bevy::prelude::*;
/// #
/// # fn setup(mut commands: Commands) {
/// // Compute mass properties from a collider with a density of `2.0`.
/// commands.spawn((
/// RigidBody::Dynamic,
#[cfg_attr(
feature = "2d",
doc = " MassPropertiesBundle::from_shape(&Collider::circle(0.5), 2.0),"
)]
#[cfg_attr(
feature = "3d",
doc = " MassPropertiesBundle::from_shape(&Collider::sphere(0.5), 2.0),"
)]
/// ));
///
/// // Bevy's primitive shapes can also be used.
/// commands.spawn((
/// RigidBody::Dynamic,
#[cfg_attr(
feature = "2d",
doc = " MassPropertiesBundle::from_shape(&Circle::new(0.5), 2.0),"
)]
#[cfg_attr(
feature = "3d",
doc = " MassPropertiesBundle::from_shape(&Sphere::new(0.5), 2.0),"
)]
/// ));
/// # }
/// ```
#[cfg(all(
feature = "default-collider",
any(feature = "parry-f32", feature = "parry-f64")
))]
#[inline]
pub fn from_shape<T: ComputeMassProperties>(shape: &T, density: f32) -> Self {
shape.mass_properties(density).to_bundle()
}
/// Computes the mass properties for a [`Collider`] based on its shape and a given density.
#[cfg(all(
feature = "default-collider",
any(feature = "parry-f32", feature = "parry-f64")
))]
#[inline]
#[deprecated(since = "0.2.0", note = "Use `from_shape` instead")]
pub fn new_computed(collider: &Collider, density: f32) -> Self {
Self::from_shape(collider, density)
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "3d")]
use super::*;
#[cfg(feature = "3d")]
use approx::assert_relative_eq;
#[test]
#[cfg(feature = "3d")]
fn angular_inertia_creation() {
use bevy_heavy::AngularInertiaTensor;
let angular_inertia = AngularInertia::new(Vec3::new(10.0, 20.0, 30.0));
assert_eq!(angular_inertia.principal, Vec3::new(10.0, 20.0, 30.0));
assert_eq!(angular_inertia.local_frame, Quat::IDENTITY);
assert_relative_eq!(
angular_inertia.tensor(),
AngularInertiaTensor::new(Vec3::new(10.0, 20.0, 30.0))
);
}
#[test]
#[should_panic]
#[cfg(feature = "3d")]
fn negative_angular_inertia_panics() {
AngularInertia::new(Vec3::new(-1.0, 2.0, 3.0));
}
#[test]
#[cfg(feature = "3d")]
fn negative_angular_inertia_error() {
assert_eq!(
AngularInertia::try_new(Vec3::new(-1.0, 2.0, 3.0)),
Err(AngularInertiaError::Negative),
"negative angular inertia should return an error"
);
}
#[test]
#[cfg(feature = "3d")]
fn nan_angular_inertia_error() {
assert_eq!(
AngularInertia::try_new(Vec3::new(f32::NAN, 2.0, 3.0)),
Err(AngularInertiaError::NaN),
"NaN angular inertia should return an error"
);
}
}