-
-
Notifications
You must be signed in to change notification settings - Fork 260
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
Rapier context as a Component #545
Merged
Merged
Changes from 9 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
d4ff57e
working rapierContext as component, but not ideal API.
Vrixyz 3cb7676
systemparam exploration
Vrixyz a01395e
RapierContextEntityLink to know which RapierContext is referred to
Vrixyz 4e7c36c
more rapierContext conf + broken events -> added a test which should …
Vrixyz 4952ea5
fix events test
Vrixyz 7a4ba5e
better support for multi world
Vrixyz 24fd036
multiple worlds working + example
Vrixyz f900dac
progress multi world with change
Vrixyz d171fe4
cargo clippy + pr feedbacks
Vrixyz 8dd49ee
less unwraps
Vrixyz 2b262ea
ci without debug symbols
Vrixyz f849cd2
more tests + logs + better changelog
Vrixyz df3578e
more error logs in case of no rapier context
Vrixyz ac16f38
Merge remote-tracking branch 'upstream' into RapierContext_Component
Vrixyz 941b52e
fix conflict
Vrixyz 5669a8c
fix unwrap
Vrixyz 0b9ca86
fix warning
Vrixyz f4643ef
address pr feedback
Vrixyz f8b296a
address pr feedback
Vrixyz 0e46610
Merge branch 'master' into RapierContext_Component
Vrixyz 75c2195
fixed compilation
Vrixyz ccd2be7
fix doc comment
Vrixyz 9142888
fixed joints3 example
Vrixyz ba0cd76
experiment on stepping to test system ordering and consistent ecs state
Vrixyz 6d10fe6
fix compilation + warning
Vrixyz d0b07a0
better error handling when rapier context is missing
Vrixyz 2676c35
add a name for rapier context entity
Vrixyz 40be1f1
Merge branch 'master' into RapierContext_Component
Vrixyz deb0a06
rename an init function
Vrixyz cf165bd
apply pr suggestion
Vrixyz aeab8c9
apply PR suggestions
Vrixyz 07eefbb
Merge branch 'master' into RapierContext_Component
Vrixyz 4cccde1
rename rapier context system params
Vrixyz cbfe138
fix clippy (+ random typo)
Vrixyz 5dc5b2d
cargo fmt
Vrixyz d0961d0
Merge remote-tracking branch 'upstream' into RapierContext_Component
Vrixyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use bevy::{input::common_conditions::input_just_pressed, prelude::*}; | ||
use bevy_rapier3d::prelude::*; | ||
|
||
const N_WORLDS: usize = 2; | ||
|
||
fn main() { | ||
App::new() | ||
.insert_resource(ClearColor(Color::srgb( | ||
0xF9 as f32 / 255.0, | ||
0xF9 as f32 / 255.0, | ||
0xFF as f32 / 255.0, | ||
))) | ||
.add_plugins(( | ||
DefaultPlugins, | ||
RapierPhysicsPlugin::<NoUserData>::default() | ||
.with_default_world(RapierContextInitialization::NoAutomaticRapierContext), | ||
Vrixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RapierDebugRenderPlugin::default(), | ||
)) | ||
.add_systems( | ||
Startup, | ||
((create_worlds, setup_physics).chain(), setup_graphics), | ||
) | ||
.add_systems(Update, move_platforms) | ||
.add_systems( | ||
Update, | ||
change_world.run_if(input_just_pressed(KeyCode::KeyC)), | ||
) | ||
.run(); | ||
} | ||
|
||
fn create_worlds(mut commands: Commands) { | ||
for i in 0..N_WORLDS { | ||
let mut world = commands.spawn((RapierContext::default(), WorldId(i))); | ||
Vrixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if i == 0 { | ||
world.insert(DefaultRapierContext); | ||
} | ||
} | ||
} | ||
|
||
fn setup_graphics(mut commands: Commands) { | ||
commands.spawn(Camera3dBundle { | ||
transform: Transform::from_xyz(0.0, 3.0, -10.0) | ||
.looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y), | ||
..Default::default() | ||
}); | ||
} | ||
|
||
#[derive(Component)] | ||
pub struct WorldId(pub usize); | ||
|
||
#[derive(Component)] | ||
struct Platform { | ||
starting_y: f32, | ||
} | ||
|
||
fn move_platforms(time: Res<Time>, mut query: Query<(&mut Transform, &Platform)>) { | ||
for (mut transform, platform) in query.iter_mut() { | ||
transform.translation.y = platform.starting_y + -time.elapsed_seconds().sin(); | ||
} | ||
} | ||
|
||
/// Demonstrates how easy it is to move one entity to another world. | ||
fn change_world( | ||
query_context: Query<Entity, With<DefaultRapierContext>>, | ||
mut query_links: Query<(Entity, &mut RapierContextEntityLink)>, | ||
) { | ||
let default_context = query_context.single(); | ||
for (e, mut link) in query_links.iter_mut() { | ||
if link.0 == default_context { | ||
continue; | ||
} | ||
link.0 = default_context; | ||
println!("changing world of {} for world {}", e, link.0); | ||
} | ||
Vrixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pub fn setup_physics( | ||
context: Query<(Entity, &WorldId), With<RapierContext>>, | ||
mut commands: Commands, | ||
) { | ||
for (context_entity, id) in context.iter() { | ||
let id = id.0; | ||
|
||
let color = [ | ||
Hsla::hsl(220.0, 1.0, 0.3), | ||
Hsla::hsl(180.0, 1.0, 0.3), | ||
Hsla::hsl(260.0, 1.0, 0.7), | ||
][id % 3]; | ||
|
||
/* | ||
* Ground | ||
*/ | ||
let ground_size = 5.1; | ||
let ground_height = 0.1; | ||
|
||
let starting_y = (id as f32) * -0.5 - ground_height; | ||
|
||
let mut platforms = commands.spawn(( | ||
TransformBundle::from(Transform::from_xyz(0.0, starting_y, 0.0)), | ||
Collider::cuboid(ground_size, ground_height, ground_size), | ||
ColliderDebugColor(color), | ||
RapierContextEntityLink(context_entity), | ||
)); | ||
if id == 1 { | ||
platforms.insert(Platform { starting_y }); | ||
} | ||
|
||
/* | ||
* Create the cube | ||
*/ | ||
|
||
commands.spawn(( | ||
TransformBundle::from(Transform::from_xyz(0.0, 1.0 + id as f32 * 5.0, 0.0)), | ||
RigidBody::Dynamic, | ||
Collider::cuboid(0.5, 0.5, 0.5), | ||
ColliderDebugColor(color), | ||
RapierContextEntityLink(context_entity), | ||
)); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a suggestion. Bevy has these
Read<>
andWrite<>
structs. Perhaps, to make the API look more idiomatic the access structs could be named as follow?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR: ok :)
I tend to prefer suffixes to prefix, so when you're searching for a term, the alphabetical order comes better grouped.
That said, my initial naming does a bad job at putting
Default
first. For this reason, I might prefer aRapierContextDefaultRead
; but that would probably mean renamingDefaultRapierContext
toRapierContextDefault
, which might sound weird.For the idiomatic way, I wanted to keep close to
Res
/ResMut
, which is very idiomatic. I believeRead
/Write
is used for individual components, which is not exaclty the case here. I'm not sure I've seen it inSystemParam
. But I'm not sure.In any way, I don't feel too opinionated about that, so I'll just implement your suggestion 😅.