Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mass Property Rework #574

Merged
merged 37 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7d11cd2
Extract mass property logic into `MassPropertyPlugin`
Jondolf Oct 13, 2024
ef47f35
Refactor more logic to use `MassPropertyPlugin`
Jondolf Oct 13, 2024
7fde7bf
Improve docs
Jondolf Oct 13, 2024
db191ef
Fix tests
Jondolf Oct 13, 2024
73e7ecf
WIP
Jondolf Nov 25, 2024
7b2f8a1
Fix things
Jondolf Nov 26, 2024
5831599
Fix test
Jondolf Nov 26, 2024
e7f5e7f
Merge branch 'mass-property-plugin-wip' into mass-property-rework-2
Jondolf Nov 26, 2024
55cb078
Fix things
Jondolf Nov 26, 2024
394b74b
Separate computed mass properties
Jondolf Nov 28, 2024
f6b2db8
Add 3x3 eigensolver and helpers for diagonalizing inertia tensors
Jondolf Nov 28, 2024
5e00e1a
Merge branch 'main' into mass-property-rework-2
Jondolf Nov 29, 2024
2f4b947
Fix lints
Jondolf Nov 29, 2024
cef954d
Use `bevy_heavy`, add `MassHelper`, rework mass property updates
Jondolf Nov 30, 2024
859aeed
Remove unnecessary mass type registrations from `PhysicsTypeRegistrat…
Jondolf Nov 30, 2024
c40a99d
Merge branch 'main' into mass-property-rework-2
Jondolf Nov 30, 2024
182f0ab
Implement `ComputeMassProperties` for `Collider`
Jondolf Nov 30, 2024
06e40d3
Rename modules and fix docs
Jondolf Nov 30, 2024
1c2a5d0
Fix math types and clean up
Jondolf Dec 1, 2024
5985900
Fix tests
Jondolf Dec 1, 2024
6c861ce
Improve docs for `Mass`, `AngularInertia`, and `CenterOfMass`, add `f…
Jondolf Dec 1, 2024
e772d35
Improve docs of computed mass property components
Jondolf Dec 1, 2024
1d45c81
Add more doc links
Jondolf Dec 1, 2024
1e22045
Improve docs for collider mass components, make `ColliderMassProperti…
Jondolf Dec 1, 2024
36c70c2
Change `from_collider` to generic `from_shape`, improve docs and stru…
Jondolf Dec 2, 2024
b85ef64
Rename system sets, improve docs of `NoAutoFoo` markers, fix things
Jondolf Dec 2, 2024
ebd24c9
Improve `RigidBody` docs
Jondolf Dec 2, 2024
c475f9b
Fix total mass property computation and scheduling issue, add tests a…
Jondolf Dec 2, 2024
cefd25c
Remove debug prints (whoops)
Jondolf Dec 2, 2024
8ca569b
Fix behavior for adding and removing `NoAutoFoo`, add regression test
Jondolf Dec 2, 2024
d37f03c
Change `from_translation` to `from_xyz`
Jondolf Dec 3, 2024
7fdaec6
Rename `ColliderMassProperties::from_collider` to `from_shape`
Jondolf Dec 4, 2024
a3bdb3d
Remove collider density clamping
Jondolf Dec 4, 2024
73c9288
Use `bevy_heavy` 0.1
Jondolf Dec 4, 2024
5432893
Fix doc example
Jondolf Dec 4, 2024
cbf3a8d
Simplify docs cfg_attr
Jondolf Dec 4, 2024
fe5073d
Merge branch 'main' into mass-property-rework-2
Jondolf Dec 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn setup(
Collider::cuboid(1.0, 1.0, 1.0),
AngularVelocity(Vec3::new(2.5, 3.5, 1.5)),
PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
mesh: meshes.add(Cuboid::from_length(1.0)),
material: materials.add(Color::srgb_u8(124, 144, 255)),
transform: Transform::from_xyz(0.0, 4.0, 0.0),
..default()
Expand Down
3 changes: 3 additions & 0 deletions crates/avian2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enhanced-determinism = [
"parry2d?/enhanced-determinism",
"parry2d-f64?/enhanced-determinism",
"bevy_math/libm",
"bevy_heavy/libm",
]

default-collider = ["dep:nalgebra"]
Expand Down Expand Up @@ -61,6 +62,7 @@ bench = false
avian_derive = { path = "../avian_derive", version = "0.1" }
bevy = { version = "0.15", default-features = false }
bevy_math = { version = "0.15" }
bevy_heavy = { version = "0.1" }
libm = { version = "0.2", optional = true }
parry2d = { version = "0.17", optional = true }
parry2d-f64 = { version = "0.17", optional = true }
Expand All @@ -76,6 +78,7 @@ bitflags = "2.5.0"
examples_common_2d = { path = "../examples_common_2d" }
benches_common_2d = { path = "../benches_common_2d" }
bevy_math = { version = "0.15", features = ["approx"] }
bevy_heavy = { version = "0.1", features = ["approx"] }
glam = { version = "0.29", features = ["bytemuck"] }
approx = "0.5"
bytemuck = "1.19"
Expand Down
2 changes: 1 addition & 1 deletion crates/avian2d/examples/chain_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn setup(
let current_particle = commands
.spawn((
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::circle(particle_radius), 1.0),
MassPropertiesBundle::from_shape(&Circle::new(particle_radius as f32), 1.0),
Mesh2d(particle_mesh.clone()),
MeshMaterial2d(particle_material.clone()),
Transform::from_xyz(0.0, -i as f32 * (particle_radius as f32 * 2.0 + 1.0), 0.0),
Expand Down
36 changes: 22 additions & 14 deletions crates/avian2d/examples/custom_collider.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! An example demonstrating how to make a custom collider and use it for collision detection.

#![allow(clippy::unnecessary_cast)]

use avian2d::{math::*, prelude::*};
use bevy::prelude::*;
use examples_common_2d::ExampleCommonPlugin;
Expand Down Expand Up @@ -55,21 +57,8 @@ impl AnyCollider for CircleCollider {
ColliderAabb::new(position, Vector::splat(self.radius))
}

fn mass_properties(&self, density: Scalar) -> ColliderMassProperties {
// In 2D, the Z length is assumed to be 1.0, so volume = area
let volume = PI * self.radius.powi(2);
let mass = density * volume;
let angular_inertia = mass * self.radius.powi(2) / 2.0;

ColliderMassProperties {
mass,
angular_inertia,
center_of_mass: default(),
}
}

// This is the actual collision detection part.
// It compute all contacts between two colliders at the given positions.
// It computes all contacts between two colliders at the given positions.
fn contact_manifolds(
&self,
other: &Self,
Expand Down Expand Up @@ -122,6 +111,25 @@ impl AnyCollider for CircleCollider {
}
}

// Implement mass computation for the collider shape.
// This is needed for physics to behave correctly.
impl ComputeMassProperties2d for CircleCollider {
fn mass(&self, density: f32) -> f32 {
// In 2D, the Z length is assumed to be `1.0`, so volume == area.
let volume = std::f32::consts::PI * self.radius.powi(2) as f32;
density * volume
}

fn unit_angular_inertia(&self) -> f32 {
// Angular inertia for a circle, assuming a mass of `1.0`.
self.radius.powi(2) as f32 / 2.0
}

fn center_of_mass(&self) -> Vec2 {
Vec2::ZERO
}
}

// Note: This circle collider only supports uniform scaling.
impl ScalableCollider for CircleCollider {
fn scale(&self) -> Vector {
Expand Down
2 changes: 1 addition & 1 deletion crates/avian2d/examples/distance_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn setup(mut commands: Commands) {
square_sprite,
Transform::from_xyz(100.0, 0.0, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
MassPropertiesBundle::from_shape(&Rectangle::from_length(50.0), 1.0),
))
.id();

Expand Down
2 changes: 1 addition & 1 deletion crates/avian2d/examples/fixed_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn setup(mut commands: Commands) {
square_sprite,
Transform::from_xyz(100.0, 0.0, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
MassPropertiesBundle::from_shape(&Rectangle::from_length(50.0), 1.0),
))
.id();

Expand Down
2 changes: 1 addition & 1 deletion crates/avian2d/examples/prismatic_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn setup(mut commands: Commands) {
square_sprite,
Transform::from_xyz(100.0, 0.0, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
MassPropertiesBundle::from_shape(&Rectangle::from_length(50.0), 1.0),
))
.id();

Expand Down
2 changes: 1 addition & 1 deletion crates/avian2d/examples/revolute_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn setup(mut commands: Commands) {
square_sprite,
Transform::from_xyz(0.0, -100.0, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
MassPropertiesBundle::from_shape(&Rectangle::from_length(50.0), 1.0),
))
.id();

Expand Down
8 changes: 6 additions & 2 deletions crates/avian3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enhanced-determinism = [
"parry3d?/enhanced-determinism",
"parry3d-f64?/enhanced-determinism",
"bevy_math/libm",
"bevy_heavy/libm",
]

default-collider = ["dep:nalgebra"]
Expand All @@ -48,6 +49,7 @@ bevy_picking = ["bevy/bevy_picking"]
serialize = [
"dep:serde",
"bevy/serialize",
"bevy_heavy/serialize",
"parry3d?/serde-serialize",
"parry3d-f64?/serde-serialize",
"bitflags/serde",
Expand All @@ -63,6 +65,7 @@ bench = false
avian_derive = { path = "../avian_derive", version = "0.1" }
bevy = { version = "0.15", default-features = false }
bevy_math = { version = "0.15" }
bevy_heavy = { version = "0.1" }
libm = { version = "0.2", optional = true }
parry3d = { version = "0.17", optional = true }
parry3d-f64 = { version = "0.17", optional = true }
Expand All @@ -75,13 +78,14 @@ itertools = "0.13"
bitflags = "2.5.0"

[dev-dependencies]
examples_common_3d = { path = "../examples_common_3d" }
benches_common_3d = { path = "../benches_common_3d" }
bevy = { version = "0.15", default-features = false, features = [
"bevy_gltf",
"animation",
] }
examples_common_3d = { path = "../examples_common_3d" }
benches_common_3d = { path = "../benches_common_3d" }
bevy_math = { version = "0.15", features = ["approx"] }
bevy_heavy = { version = "0.1", features = ["approx"] }
approx = "0.5"
criterion = { version = "0.5", features = ["html_reports"] }
bevy_mod_debugdump = { git = "https://github.com/jakobhellermann/bevy_mod_debugdump" }
Expand Down
2 changes: 1 addition & 1 deletion crates/avian3d/examples/chain_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn setup(
let current_particle = commands
.spawn((
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::sphere(particle_radius), 1.0),
MassPropertiesBundle::from_shape(&Sphere::new(particle_radius as f32), 1.0),
Mesh3d(particle_mesh.clone()),
MeshMaterial3d(particle_material.clone()),
Transform::from_xyz(0.0, -i as f32 * particle_radius as f32 * 2.2, 0.0),
Expand Down
2 changes: 1 addition & 1 deletion crates/avian3d/examples/custom_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn setup(
MeshMaterial3d(cube_material),
Transform::from_xyz(3.0, 3.5, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(1.0, 1.0, 1.0), 1.0),
MassPropertiesBundle::from_shape(&Cuboid::from_length(1.0), 1.0),
))
.id();

Expand Down
1 change: 0 additions & 1 deletion crates/avian3d/examples/distance_joint_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ fn setup(
Transform::from_xyz(-2.0, -0.5, 0.0),
RigidBody::Dynamic,
Collider::cuboid(1., 1., 1.),
MassPropertiesBundle::new_computed(&Collider::cuboid(1.0, 1.0, 1.0), 1.0),
))
.id();

Expand Down
2 changes: 1 addition & 1 deletion crates/avian3d/examples/fixed_joint_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(
MeshMaterial3d(cube_material),
Transform::from_xyz(1.5, 0.0, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(1.0, 1.0, 1.0), 1.0),
MassPropertiesBundle::from_shape(&Cuboid::from_length(1.0), 1.0),
))
.id();

Expand Down
2 changes: 1 addition & 1 deletion crates/avian3d/examples/prismatic_joint_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(
MeshMaterial3d(cube_material),
Transform::from_xyz(1.5, 0.0, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(1.0, 1.0, 1.0), 1.0),
MassPropertiesBundle::from_shape(&Cuboid::from_length(1.0), 1.0),
))
.id();

Expand Down
2 changes: 1 addition & 1 deletion crates/avian3d/examples/revolute_joint_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(
MeshMaterial3d(cube_material),
Transform::from_xyz(0.0, -2.0, 0.0),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(1.0, 1.0, 1.0), 1.0),
MassPropertiesBundle::from_shape(&Cuboid::from_length(1.0), 1.0),
))
.id();

Expand Down
Loading
Loading