Comments (1)
Looks like this is the root cause
which gets used over here
bevy_xpbd/src/plugins/collision/broad_phase.rs
Lines 259 to 262 in 088facf
here's an updated sensor.rs
example that complies a bit more with the existing examples
Details
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use bevy_xpbd_2d::{math::*, prelude::*};
use examples_common_2d::XpbdExamplePlugin;
fn main() {
App::new()
.add_plugins((DefaultPlugins, XpbdExamplePlugin))
.insert_resource(ClearColor(Color::rgb(0.05, 0.05, 0.1)))
.insert_resource(Gravity(Vector::ZERO))
.add_event::<MovementAction>()
.add_systems(Startup, setup)
.add_systems(
Update,
(
keyboard_input,
apply_deferred,
movement,
apply_movement_damping,
apply_pressure_plate_colour,
update_velocity_text,
log_events,
)
.chain(),
)
.run();
}
#[derive(Component, Default, Reflect)]
struct Character;
#[derive(Component, Default, Reflect)]
struct PressurePlate;
#[derive(Component, Default, Reflect)]
struct CharacterVelocityText;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn((
MaterialMesh2dBundle {
mesh: meshes
.add(
shape::Capsule {
radius: 12.5,
depth: 20.0,
..default()
}
.into(),
)
.into(),
material: materials.add(ColorMaterial::from(Color::rgb(0.2, 0.7, 0.9))),
transform: Transform::from_xyz(0.0, -100.0, 0.0),
..default()
},
Character,
RigidBody::Kinematic,
SleepingDisabled,
Collider::capsule(20.0, 12.5),
Name::new("Character"),
));
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(0.0, 150.0, 0.0),
sprite: Sprite {
color: Color::WHITE,
custom_size: Some(Vec2::new(100.0, 100.0)),
..default()
},
..default()
},
PressurePlate,
Sensor,
RigidBody::Static,
SleepingDisabled,
Collider::cuboid(100.0, 100.0),
Name::new("Pressure Plate"),
));
commands.spawn((
TextBundle::from_section(
"Velocity: ",
TextStyle {
font_size: 16.0,
..default()
},
)
.with_style(Style {
position_type: PositionType::Absolute,
bottom: Val::Px(5.0),
left: Val::Px(5.0),
..default()
}),
CharacterVelocityText,
Name::new("Character Velocity Text"),
));
commands.spawn(Camera2dBundle::default());
}
#[derive(Event, Debug, Reflect)]
pub enum MovementAction {
Move(Vec2),
Stop,
}
fn keyboard_input(
mut movement_event_writer: EventWriter<MovementAction>,
keyboard_input: Res<Input<KeyCode>>,
time: Res<Time<Physics>>,
) {
if time.is_paused() {
return;
}
let left = keyboard_input.any_pressed([KeyCode::A, KeyCode::Left]);
let right = keyboard_input.any_pressed([KeyCode::D, KeyCode::Right]);
let up = keyboard_input.any_pressed([KeyCode::W, KeyCode::Up]);
let down = keyboard_input.any_pressed([KeyCode::S, KeyCode::Down]);
let space = keyboard_input.pressed(KeyCode::Space);
if space {
movement_event_writer.send(MovementAction::Stop);
return;
}
let horizontal = right as i8 - left as i8;
let vertical = up as i8 - down as i8;
let direction = Vec2::new(horizontal as Scalar, vertical as Scalar);
if direction != Vec2::ZERO {
movement_event_writer.send(MovementAction::Move(direction));
}
}
fn movement(
time: Res<Time>,
mut movement_event_reader: EventReader<MovementAction>,
mut controllers: Query<&mut LinearVelocity, With<Character>>,
) {
let delta_time = time.delta_seconds_f64().adjust_precision();
let movement_acceleration = 2000.0;
for event in movement_event_reader.read() {
for mut linear_velocity in &mut controllers {
match event {
MovementAction::Stop => {
linear_velocity.x = 0.0;
linear_velocity.y = 0.0;
}
MovementAction::Move(direction) => {
linear_velocity.x += direction.x * movement_acceleration * delta_time;
linear_velocity.y += direction.y * movement_acceleration * delta_time;
}
}
}
}
}
fn apply_movement_damping(
mut query: Query<(&mut LinearVelocity, &mut AngularVelocity)>,
time: Res<Time<Physics>>,
) {
if time.is_paused() {
return;
}
let damping_factor = 0.95;
for (mut linear_velocity, mut angular_velocity) in &mut query {
linear_velocity.x *= damping_factor;
if linear_velocity.x.abs() < 0.001 {
linear_velocity.x = 0.0;
}
linear_velocity.y *= damping_factor;
if linear_velocity.y.abs() < 0.001 {
linear_velocity.y = 0.0;
}
angular_velocity.0 *= damping_factor;
}
}
fn apply_pressure_plate_colour(
mut query: Query<(&mut Sprite, &CollidingEntities), With<PressurePlate>>,
) {
for (mut sprite, colliding_entities) in &mut query {
if colliding_entities.0.is_empty() {
sprite.color = Color::rgb(0.2, 0.7, 0.9);
} else {
sprite.color = Color::rgb(0.9, 0.7, 0.2);
}
}
}
fn update_velocity_text(
character_query: Query<&LinearVelocity, With<Character>>,
mut text_query: Query<&mut Text, With<CharacterVelocityText>>,
) {
if let Ok(velocity) = character_query.get_single() {
text_query.single_mut().sections[0].value =
format!("Velocity: {}, {}", velocity.x, velocity.y);
}
}
fn log_events(mut started: EventReader<CollisionStarted>, mut ended: EventReader<CollisionEnded>) {
// print out the started and ended events
for event in started.read() {
println!("CollisionStarted: {:?}", event);
}
for event in ended.read() {
println!("CollisionEnded: {:?}", event);
}
}
from bevy_xpbd.
Related Issues (20)
- Performance - Time to optimise? HOT 3
- Phys-bones / Dynamic Bones / Spring bones
- CollisionLayers modifiers look like in-place but are not.
- Add `AsyncSceneCombinedCollider` HOT 2
- Enabling `default-collider` without parry renders 34 compilation errors
- [Bug]: (0.4.0) Can't compile with specific crate features enabled
- [0.4.1] Colliders are initialized with wrong transform HOT 1
- Panic when running `ray_caster` example on macOS HOT 1
- inverted sign wastes hours of girls life HOT 3
- xpbd collider gen 😄 HOT 3
- issues with 2D heightfield collider HOT 1
- Rebrand Bevy XPBD and Switch Solvers HOT 2
- CollisionEnded event not sent if collider is removed in the frame following the collision start
- RayHitData not providing enough information when hitting compound colliders HOT 1
- Panic in `debug_render_axes` HOT 2
- Cylinder collider getting stuck on ledges when dropping off of them
- Awful performance with many non-physics entities HOT 3
- Rigidbodies never sleep HOT 1
- Documentation improvements for basic setup and scheduling
- 2D physics in XZ plane
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bevy_xpbd.