Skip to content

Commit

Permalink
fix: Events not being fired for kinematic bodies (jcornaz#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarssilva authored Oct 24, 2021
1 parent 7911692 commit 2b10f0a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is inspired from [Keep a Changelog], and this project adheres to [Sem
## Fixed

* `KinematicVelocityBased` bodies not being moved by velocity (#148).
* Collision events not being fired for kinematic bodies (#142).


## [0.12.0] - 2021-10-24
Expand Down
6 changes: 4 additions & 2 deletions rapier/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use heron_core::{CollisionLayers, CollisionShape, PhysicMaterial, RigidBody, Sen
use crate::convert::IntoRapier;
use crate::rapier::dynamics::{IslandManager, RigidBodyHandle, RigidBodySet};
use crate::rapier::geometry::{
Collider, ColliderBuilder, ColliderHandle, ColliderSet, InteractionGroups,
ActiveCollisionTypes, Collider, ColliderBuilder, ColliderHandle, ColliderSet, InteractionGroups,
};
use crate::rapier::math::Point;
use crate::rapier::pipeline::ActiveEvents;
Expand Down Expand Up @@ -206,7 +206,9 @@ pub(crate) trait ColliderFactory {
builder = builder.collision_groups(layers.into_rapier());
}

builder.build()
builder
.active_collision_types(ActiveCollisionTypes::all()) // Activate all collision types
.build()
}
}

Expand Down
40 changes: 33 additions & 7 deletions rapier/tests/events.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![cfg(any(dim2, dim3))]

use std::time::Duration;

use bevy::app::{Events, ManualEventReader};
use bevy::core::CorePlugin;
use bevy::prelude::*;
use bevy::reflect::TypeRegistryArc;
use rstest::*;

use heron_core::{CollisionEvent, CollisionShape, PhysicsSteps, RigidBody, Velocity};
use heron_rapier::RapierPlugin;
use std::time::Duration;

use utils::*;

mod utils;
Expand All @@ -29,8 +31,13 @@ fn test_app() -> App {
builder.app
}

#[test]
fn collision_events_are_fired() {
#[rstest]
#[case(RigidBody::Sensor, RigidBody::Dynamic)]
#[case(RigidBody::Sensor, RigidBody::Sensor)]
#[case(RigidBody::Sensor, RigidBody::KinematicPositionBased)]
#[case(RigidBody::Sensor, RigidBody::KinematicVelocityBased)]
#[case(RigidBody::Dynamic, RigidBody::Dynamic)]
fn collision_events_are_fired(#[case] type1: RigidBody, #[case] type2: RigidBody) {
let mut app = test_app();

let entity1 = app
Expand All @@ -40,7 +47,7 @@ fn collision_events_are_fired() {
Transform::default(),
GlobalTransform::default(),
CollisionShape::Sphere { radius: 10.0 },
RigidBody::Sensor,
type1,
))
.id();

Expand All @@ -50,12 +57,23 @@ fn collision_events_are_fired() {
.insert_bundle((
Transform::from_translation(Vec3::X * -30.0),
GlobalTransform::default(),
RigidBody::Dynamic,
type2,
CollisionShape::Sphere { radius: 10.0 },
Velocity::from_linear(Vec3::X * 30.0),
))
.id();

if type2.can_have_velocity() {
app.world
.entity_mut(entity2)
.insert(Velocity::from_linear(Vec3::X * 30.0));
} else {
app.world
.get_mut::<Transform>(entity2)
.unwrap()
.translation
.x += 30.0;
}

let mut event_reader = app
.world
.get_resource::<Events<CollisionEvent>>()
Expand All @@ -67,6 +85,14 @@ fn collision_events_are_fired() {
app.update();
events.append(&mut collect_events(&app, &mut event_reader));

if !type2.can_have_velocity() {
app.world
.get_mut::<Transform>(entity2)
.unwrap()
.translation
.x += 30.0;
}

app.update();
events.append(&mut collect_events(&app, &mut event_reader));

Expand Down

0 comments on commit 2b10f0a

Please sign in to comment.