This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jonathan Cornaz <jonathan.cornaz@gmail.com>
- Loading branch information
Showing
8 changed files
with
299 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use bevy::prelude::*; | ||
use rapier::{ | ||
dynamics::RigidBody, | ||
math::{Real, Vector}, | ||
}; | ||
|
||
use heron_core::{utils::NearZero, Acceleration}; | ||
|
||
use crate::convert::IntoRapier; | ||
use crate::rapier::dynamics::RigidBodySet; | ||
use crate::rapier::math::AngVector; | ||
use crate::BodyHandle; | ||
|
||
pub(crate) fn update_rapier_force_and_torque( | ||
mut bodies: ResMut<'_, RigidBodySet>, | ||
accelerations: Query<'_, (&BodyHandle, &Acceleration)>, | ||
) { | ||
for (handle, acceleration) in accelerations.iter() { | ||
if let Some(body) = bodies.get_mut(handle.rigid_body) { | ||
update_acceleration(body, acceleration) | ||
} | ||
} | ||
} | ||
|
||
fn update_acceleration(body: &mut RigidBody, acceleration: &Acceleration) { | ||
let wake_up = !acceleration.is_near_zero(); | ||
let linear_acceleration: Vector<Real> = acceleration.linear.into_rapier(); | ||
let angular_acceleration: AngVector<f32> = acceleration.angular.into_rapier(); | ||
let inertia = { | ||
#[cfg(feature = "3d")] | ||
{ | ||
body.mass_properties().reconstruct_inertia_matrix() | ||
} | ||
#[cfg(feature = "2d")] | ||
{ | ||
let val = body.mass_properties().inv_principal_inertia_sqrt; | ||
if val == 0.0 { | ||
0.0 | ||
} else { | ||
(1.0 / val).powi(2) | ||
} | ||
} | ||
}; | ||
body.apply_force(linear_acceleration * body.mass(), wake_up); | ||
body.apply_torque(inertia * angular_acceleration, wake_up) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#![cfg(all( | ||
any(feature = "2d", feature = "3d"), | ||
not(all(feature = "2d", feature = "3d")), | ||
))] | ||
|
||
use bevy::core::CorePlugin; | ||
use bevy::prelude::*; | ||
use bevy::prelude::{GlobalTransform, Transform}; | ||
use bevy::reflect::TypeRegistryArc; | ||
use heron_core::{Acceleration, AxisAngle, Body}; | ||
use heron_rapier::convert::IntoBevy; | ||
|
||
#[cfg(feature = "3d")] | ||
use heron_rapier::rapier::math::Vector; | ||
use heron_rapier::{ | ||
rapier::dynamics::{IntegrationParameters, RigidBodySet}, | ||
BodyHandle, RapierPlugin, | ||
}; | ||
|
||
fn test_app() -> App { | ||
let mut builder = App::build(); | ||
builder | ||
.init_resource::<TypeRegistryArc>() | ||
.add_plugin(CorePlugin) | ||
.add_plugin(RapierPlugin { | ||
step_per_second: None, | ||
parameters: { | ||
let mut params = IntegrationParameters::default(); | ||
params.dt = 1.0; | ||
params | ||
}, | ||
}); | ||
builder.app | ||
} | ||
|
||
#[test] | ||
fn body_is_created_with_acceleration() { | ||
let mut app = test_app(); | ||
|
||
#[cfg(feature = "3d")] | ||
let linear = Vec3::new(1.0, 2.0, 3.0); | ||
#[cfg(feature = "2d")] | ||
let linear = Vec3::new(1.0, 2.0, 0.0); | ||
|
||
let angular = AxisAngle::new(Vec3::unit_z(), 1.0); | ||
|
||
let entity = app.world.spawn(( | ||
Transform::default(), | ||
GlobalTransform::default(), | ||
Body::Sphere { radius: 1.0 }, | ||
Acceleration { linear, angular }, | ||
)); | ||
|
||
app.update(); | ||
|
||
{ | ||
let bodies = app.resources.get::<RigidBodySet>().unwrap(); | ||
|
||
let body = bodies | ||
.get(app.world.get::<BodyHandle>(entity).unwrap().rigid_body()) | ||
.unwrap(); | ||
|
||
println!("{:?}", body); | ||
assert_eq!(body.linvel().into_bevy(), Vec3::zero()); | ||
assert_eq_angular(body.angvel(), AxisAngle::from(Vec3::zero())); | ||
} | ||
|
||
app.update(); | ||
|
||
let bodies = app.resources.get::<RigidBodySet>().unwrap(); | ||
|
||
let body = bodies | ||
.get(app.world.get::<BodyHandle>(entity).unwrap().rigid_body()) | ||
.unwrap(); | ||
|
||
println!("{:?}", body); | ||
assert_eq!(body.linvel().into_bevy(), linear); | ||
assert_eq_angular(body.angvel(), angular); | ||
} | ||
|
||
#[test] | ||
fn acceleration_may_be_added_after_creating_the_body() { | ||
let mut app = test_app(); | ||
|
||
let entity = app.world.spawn(( | ||
Transform::default(), | ||
GlobalTransform::default(), | ||
Body::Sphere { radius: 1.0 }, | ||
)); | ||
|
||
app.update(); | ||
|
||
#[cfg(feature = "3d")] | ||
let linear = Vec3::new(1.0, 2.0, 3.0); | ||
#[cfg(feature = "2d")] | ||
let linear = Vec3::new(1.0, 2.0, 0.0); | ||
|
||
let angular = AxisAngle::new(Vec3::unit_z(), 2.0); | ||
|
||
app.world | ||
.insert_one(entity, Acceleration { linear, angular }) | ||
.unwrap(); | ||
|
||
app.update(); | ||
|
||
let bodies = app.resources.get::<RigidBodySet>().unwrap(); | ||
|
||
let body = bodies | ||
.get(app.world.get::<BodyHandle>(entity).unwrap().rigid_body()) | ||
.unwrap(); | ||
|
||
assert_eq!(body.linvel().into_bevy(), linear); | ||
assert_eq_angular(body.angvel(), angular); | ||
} | ||
|
||
#[cfg(feature = "3d")] | ||
fn assert_eq_angular(actual: &Vector<f32>, expected: AxisAngle) { | ||
assert_eq!(actual.into_bevy(), expected.into()); | ||
} | ||
|
||
#[cfg(feature = "2d")] | ||
fn assert_eq_angular(expected: f32, actual: AxisAngle) { | ||
assert!( | ||
(expected - actual.angle()).abs() < 0.00001, | ||
"actual rapier angle ({}) doesn't match expected axis-angle: {:?}", | ||
expected, | ||
actual | ||
); | ||
} |
Oops, something went wrong.