forked from dimforge/wgsparkl
-
Notifications
You must be signed in to change notification settings - Fork 0
Dynamic addition of particles #5
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
Open
ThierryBerger
wants to merge
2
commits into
sample_color_model
Choose a base branch
from
dynamic_add_particles
base: sample_color_model
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,112 @@ | ||
| use wgsparkl2d::rapier::prelude::{ColliderBuilder, RigidBodyBuilder}; | ||
| use wgsparkl_testbed2d::{wgsparkl, Callbacks, RapierData}; | ||
|
|
||
| use bevy::render::renderer::RenderDevice; | ||
| use nalgebra::{vector, Vector2}; | ||
| use wgsparkl::models::DruckerPrager; | ||
| use wgsparkl::{ | ||
| models::ElasticCoefficients, | ||
| pipeline::MpmData, | ||
| solver::{Particle, SimulationParams}, | ||
| }; | ||
| use wgsparkl2d::solver::ParticleDynamics; | ||
| use wgsparkl_testbed2d::{AppState, PhysicsContext}; | ||
|
|
||
| pub fn dynamic_demo( | ||
| device: RenderDevice, | ||
| app_state: &mut AppState, | ||
| callbacks: &mut Callbacks, | ||
| ) -> PhysicsContext { | ||
| let mut rapier_data = RapierData::default(); | ||
| let device = device.wgpu_device(); | ||
|
|
||
| let offset_y = 46.0; | ||
| let cell_width = 0.2; | ||
| let mut particles = vec![]; | ||
| let position = | ||
| vector![-20f32, 0.0] + vector![0.5, 0.5] * cell_width / 2.0 + Vector2::y() * offset_y; | ||
| let density = 1000.0; | ||
| let radius = cell_width / 4f32; | ||
| let particle_proto = Particle { | ||
| position, | ||
| dynamics: ParticleDynamics::with_density(radius, density), | ||
| model: ElasticCoefficients::from_young_modulus(10_000_000.0, 0.2), | ||
| plasticity: Some(DruckerPrager::new(10_000_000.0, 0.2)), | ||
| phase: None, | ||
| color: None, | ||
| }; | ||
| particles.push(particle_proto); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: fix bug where we access particles[0] |
||
|
|
||
| if !app_state.restarting { | ||
| app_state.num_substeps = 10; | ||
| app_state.gravity_factor = 1.0; | ||
| }; | ||
|
|
||
| let params = SimulationParams { | ||
| gravity: vector![0.0, -9.81] * app_state.gravity_factor, | ||
| dt: (1.0 / 60.0) / (app_state.num_substeps as f32), | ||
| padding: 0.0, | ||
| }; | ||
|
|
||
| /* | ||
| * Static platforms. | ||
| */ | ||
| let rb = RigidBodyBuilder::fixed().translation(vector![35.0, -1.0]); | ||
| let rb_handle = rapier_data.bodies.insert(rb); | ||
| let co = ColliderBuilder::cuboid(42.0, 1.0); | ||
| rapier_data | ||
| .colliders | ||
| .insert_with_parent(co, rb_handle, &mut rapier_data.bodies); | ||
|
|
||
| let rb = RigidBodyBuilder::fixed() | ||
| .translation(vector![-25.0, 45.0]) | ||
| .rotation(0.5); | ||
| let rb_handle = rapier_data.bodies.insert(rb); | ||
| let co = ColliderBuilder::cuboid(1.0, 52.0); | ||
| rapier_data | ||
| .colliders | ||
| .insert_with_parent(co, rb_handle, &mut rapier_data.bodies); | ||
|
|
||
| let rb = RigidBodyBuilder::fixed() | ||
| .translation(vector![95.0, 45.0]) | ||
| .rotation(-0.5); | ||
| let rb_handle = rapier_data.bodies.insert(rb); | ||
| let co = ColliderBuilder::cuboid(1.0, 52.0); | ||
| rapier_data | ||
| .colliders | ||
| .insert_with_parent(co, rb_handle, &mut rapier_data.bodies); | ||
|
|
||
| let mut tick = 0; | ||
| callbacks.0.push(Box::new( | ||
| move |_, physics: &mut PhysicsContext, _, _, queue| { | ||
| tick += 1; | ||
| if tick % 2 != 0 { | ||
| // Do not add particles each frame, to avoid clumping particles together. | ||
| return; | ||
| } | ||
| for i in 0..500 { | ||
| let mut particle = particle_proto.clone(); | ||
| particle.position += vector![1.0, 0.0] * (i as f32) * cell_width * 1.1; | ||
| physics.push_particle(queue, &particle); | ||
| } | ||
| physics.reset_graphics = true; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: cleaner way to update graphics with same strategy. |
||
| }, | ||
| )); | ||
|
|
||
| let data = MpmData::new( | ||
| device, | ||
| params, | ||
| &particles, | ||
| 100_000, | ||
| &rapier_data.bodies, | ||
| &rapier_data.colliders, | ||
| cell_width, | ||
| 60_000, | ||
| ); | ||
| PhysicsContext { | ||
| data, | ||
| rapier_data, | ||
| particles, | ||
| reset_graphics: false, | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
| use wgsparkl_testbed3d::{wgsparkl, Callbacks, RapierData}; | ||
|
|
||
| use bevy::render::renderer::RenderDevice; | ||
| use nalgebra::vector; | ||
| use wgsparkl::models::DruckerPrager; | ||
| use wgsparkl::{ | ||
| models::ElasticCoefficients, | ||
| pipeline::MpmData, | ||
| solver::{Particle, ParticleDynamics, SimulationParams}, | ||
| }; | ||
| use wgsparkl_testbed3d::{AppState, PhysicsContext}; | ||
|
|
||
| use crate::utils::default_scene; | ||
|
|
||
| pub fn dynamic_demo( | ||
| device: RenderDevice, | ||
| app_state: &mut AppState, | ||
| callbacks: &mut Callbacks, | ||
| ) -> PhysicsContext { | ||
| let mut rapier_data = RapierData::default(); | ||
| let device = device.wgpu_device(); | ||
|
|
||
| if !app_state.restarting { | ||
| app_state.num_substeps = 20; | ||
| app_state.gravity_factor = 1.0; | ||
| }; | ||
|
|
||
| let params = SimulationParams { | ||
| gravity: vector![0.0, -9.81, 0.0] * app_state.gravity_factor, | ||
| dt: (1.0 / 60.0) / (app_state.num_substeps as f32), | ||
| }; | ||
|
|
||
| default_scene::spawn_ground_and_walls(&mut rapier_data); | ||
| let cell_width = 1.0; | ||
|
|
||
| let position = vector![0.0, 10f32, 0.0]; | ||
| callbacks.0.push(Box::new( | ||
| move |_, physics: &mut PhysicsContext, _, _, queue| { | ||
| physics.data.particles.push( | ||
| queue, | ||
| &Particle { | ||
| position, | ||
| dynamics: ParticleDynamics::with_density(cell_width / 4.0, 2700.0), | ||
| model: ElasticCoefficients::from_young_modulus(2_000_000_000.0, 0.2), | ||
| plasticity: Some(DruckerPrager::new(2_000_000_000.0, 0.2)), | ||
| phase: None, | ||
| color: None, | ||
| }, | ||
| ) | ||
| }, | ||
| )); | ||
|
|
||
| let particles = vec![]; | ||
| let data = MpmData::new( | ||
| device, | ||
| params, | ||
| &particles, | ||
| 1_000, | ||
| &rapier_data.bodies, | ||
| &rapier_data.colliders, | ||
| cell_width, | ||
| 60_000, | ||
| ); | ||
| PhysicsContext { | ||
| data, | ||
| rapier_data, | ||
| particles, | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
rename with postfix
2