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

Fix colliders without RigidBody not working #323

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 7 additions & 6 deletions src/plugins/collision/broad_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl MapEntities for AabbIntervals {
fn update_aabb_intervals(
aabbs: Query<(
&ColliderAabb,
&ColliderParent,
Option<&ColliderParent>,
Option<&CollisionLayers>,
Ref<Position>,
Ref<Rotation>,
Expand All @@ -199,10 +199,11 @@ fn update_aabb_intervals(
aabbs.get(*collider_entity)
{
*aabb = *new_aabb;
*collider_parent = *new_parent;
*collider_parent = new_parent.map_or(ColliderParent(*collider_entity), |p| *p);
*layers = new_layers.map_or(CollisionLayers::default(), |layers| *layers);

let is_static = rbs.get(new_parent.get()).is_ok_and(RigidBody::is_static);
let is_static =
new_parent.is_some_and(|p| rbs.get(p.get()).is_ok_and(RigidBody::is_static));
*is_inactive = is_static || (!position.is_changed() && !rotation.is_changed());

true
Expand All @@ -219,7 +220,7 @@ fn add_new_aabb_intervals(
aabbs: Query<
(
Entity,
&ColliderParent,
Option<&ColliderParent>,
&ColliderAabb,
Option<&RigidBody>,
Option<&CollisionLayers>,
Expand All @@ -231,7 +232,7 @@ fn add_new_aabb_intervals(
let aabbs = aabbs.iter().map(|(ent, parent, aabb, rb, layers)| {
(
ent,
*parent,
parent.map_or(ColliderParent(ent), |p| *p),
*aabb,
// Default to treating collider as immovable/static for filtering unnecessary collision checks
layers.map_or(CollisionLayers::default(), |layers| *layers),
Expand Down Expand Up @@ -295,7 +296,7 @@ fn sweep_and_prune(
/// Sorts a list iteratively using comparisons. In an ascending sort order, when a smaller value is encountered, it is moved lower in the list until it is larger than the item before it.
///
/// This is relatively slow for large lists, but very efficient in cases where the list is already mostly sorted.
fn insertion_sort<T>(items: &mut Vec<T>, comparison: fn(&T, &T) -> bool) {
fn insertion_sort<T>(items: &mut [T], comparison: fn(&T, &T) -> bool) {
for i in 1..items.len() {
let mut j = i;
while j > 0 && comparison(&items[j - 1], &items[j]) {
Expand Down
11 changes: 8 additions & 3 deletions src/plugins/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ fn transform_to_position(
&mut Position,
Option<&AccumulatedTranslation>,
&mut Rotation,
&PreviousRotation,
&CenterOfMass,
Option<&PreviousRotation>,
Option<&CenterOfMass>,
)>,
) {
for (
Expand All @@ -433,7 +433,12 @@ fn transform_to_position(
let previous_transform = previous_transform.compute_transform();
let pos = position.0
+ accumulated_translation.map_or(Vector::ZERO, |t| {
get_pos_translation(t, previous_rotation, &rotation, center_of_mass)
get_pos_translation(
t,
&previous_rotation.copied().unwrap_or_default(),
&rotation,
&center_of_mass.copied().unwrap_or_default(),
)
});

#[cfg(feature = "2d")]
Expand Down