Skip to content

Commit

Permalink
Add missing delete points method (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu authored Apr 23, 2024
1 parent 4314293 commit 8e9d56d
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 165 deletions.
1 change: 1 addition & 0 deletions src/fluids/fluid_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ void Fluid2D::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_debug_draw", "debug_draw"), &Fluid2D::set_debug_draw);
ClassDB::bind_method(D_METHOD("get_debug_draw"), &Fluid2D::get_debug_draw);
ClassDB::bind_method(D_METHOD("delete_points", "indices"), &Fluid2D::delete_points);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_draw"), "set_debug_draw", "get_debug_draw");
}

Expand Down
10 changes: 6 additions & 4 deletions src/rapier2d-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ serde-serialize = ["rapier2d/serde-serialize", "rapier2d-f64/serde-serialize"]
parallel = ["rapier2d/parallel", "rapier2d-f64/parallel"]

[dependencies]
rapier2d = {git = "https://github.com/dimforge/rapier", branch = "master"}
rapier2d-f64 = {git = "https://github.com/dimforge/rapier", branch = "master"}
rapier2d = {git = "https://github.com/dimforge/rapier", branch = "stability-improvements"}
rapier2d-f64 = {git = "https://github.com/dimforge/rapier", branch = "stability-improvements"}
salva2d = { git = "https://github.com/dimforge/salva", branch = "master", features = ["rapier"] }

[patch.crates-io]
rapier2d = {git = "https://github.com/dimforge/rapier", branch = "master"}
rapier2d-f64 = {git = "https://github.com/dimforge/rapier", branch = "master"}
rapier2d = {git = "https://github.com/dimforge/rapier", branch = "stability-improvements"}
rapier2d-f64 = {git = "https://github.com/dimforge/rapier", branch = "stability-improvements"}
parry2d = {git = "https://github.com/dimforge/parry", branch = "internal-edges-rework"}
parry2d-f64 = {git = "https://github.com/dimforge/parry", branch = "internal-edges-rework"}

[profile.release]
codegen-units = 1
Expand Down
34 changes: 0 additions & 34 deletions src/rapier2d-wrapper/includes/rapier2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ enum class BodyType {
Static,
};

#if !defined(DEFINE_SPIN_NO_STD)
template <typename T = void>
struct Lazy;
#endif

#if defined(DEFINE_SPIN_NO_STD)
template <typename T = void>
struct Lazy;
#endif

/// A feature id where the feature type is packed into the same value as the feature index.
struct PackedFeatureId;

Expand Down Expand Up @@ -92,10 +82,6 @@ struct QueryExcludedInfo {
};

struct WorldSettings {
Real sleep_linear_threshold;
Real sleep_angular_threshold;
Real sleep_time_until_sleep;
Real solver_prediction_distance;
size_t max_ccd_substeps;
Real particle_radius;
Real smoothing_factor;
Expand Down Expand Up @@ -205,26 +191,6 @@ using CollisionModifyContactsCallback = OneWayDirection (*)(Handle world_handle,
struct SimulationSettings {
/// The timestep length (default: `1.0 / 60.0`)
Real dt;
/// 0-1: multiplier for how much of the constraint violation (e.g. contact penetration)
/// will be compensated for during the velocity solve.
/// (default `0.8`).
Real erp;
/// 0-1: the damping ratio used by the springs for Baumgarte constraints stabilization.
/// Lower values make the constraints more compliant (more "springy", allowing more visible penetrations
/// before stabilization).
/// (default `0.25`).
Real damping_ratio;
/// 0-1: multiplier for how much of the joint violation
/// will be compensated for during the velocity solve.
/// (default `1.0`).
Real joint_erp;
/// The fraction of critical damping applied to the joint for constraints regularization.
/// (default `0.25`).
Real joint_damping_ratio;
/// Amount of penetration the engine wont attempt to correct (default: `0.001m`).
Real allowed_linear_error;
/// The maximal distance separating two objects that will generate predictive contacts (default: `0.002`).
Real prediction_distance;
/// The number of solver iterations run by the constraints solver for calculating forces (default: `4`).
size_t num_solver_iterations;
/// Number of addition friction resolution iteration run during the last solver sub-step (default: `4`).
Expand Down
5 changes: 0 additions & 5 deletions src/rapier2d-wrapper/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ pub extern "C" fn body_create(world_handle : Handle, pixel_pos : &Vector, rot :
}
// let default values better
let activation = rigid_body.activation_mut();
activation.time_until_sleep = physics_world.sleep_time_until_sleep;
activation.linear_threshold = pixels_to_meters(physics_world.sleep_linear_threshold);
activation.angular_threshold = physics_world.sleep_angular_threshold;
set_rigid_body_properties_internal(&mut rigid_body, pixel_pos, rot, true);
rigid_body.user_data = user_data.get_data();
let body_handle = physics_world.rigid_body_set.insert(rigid_body);
Expand Down Expand Up @@ -216,8 +213,6 @@ pub extern "C" fn body_set_can_sleep(world_handle : Handle, body_handle : Handle

if can_sleep && body.activation().angular_threshold == -1.0 {
let activation = body.activation_mut();
activation.angular_threshold = physics_world.sleep_angular_threshold;
activation.linear_threshold = physics_world.sleep_linear_threshold;
} else if !can_sleep && body.activation().angular_threshold != -1.0 {
let activation = body.activation_mut();
activation.angular_threshold = -1.0;
Expand Down
4 changes: 3 additions & 1 deletion src/rapier2d-wrapper/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use rapier2d::prelude::*;
use crate::vector::Vector;

const PIXELS_PER_METER : Real = 100.0;
const PIXELS_PER_METER : Real = 1.0;

pub fn pixels_to_meters(x : Real) -> Real {
return x;
if x == 0.0 { 0.0 } else { x / PIXELS_PER_METER }
}

Expand All @@ -12,6 +13,7 @@ pub fn vector_pixels_to_meters(v : &Vector) -> Vector {
}

pub fn meters_to_pixels(x : Real) -> Real {
return x;
x * PIXELS_PER_METER
}

Expand Down
37 changes: 30 additions & 7 deletions src/rapier2d-wrapper/src/fluid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,39 @@ pub extern "C" fn fluid_delete_points(world_handle : Handle, fluid_handle: Handl
let fluid = fluid.unwrap();
unsafe {
let indexes_raw = std::slice::from_raw_parts(indexes, indexes_count);
for i in 0..indexes_raw.len() {
let index_raw = indexes_raw[i];
if fluid.positions.len() <= index_raw {
// create mask from array of indexes
let mut mask = vec![false; fluid.positions.len()];
for i in 0..indexes_count {
if fluid.positions.len() <= indexes_raw[i] {
continue;
}
fluid.positions.remove(index_raw);
fluid.velocities.remove(index_raw);
fluid.accelerations.remove(index_raw);
fluid.volumes.remove(index_raw);
mask[indexes_raw[i]] = true;
}
let mut i = 0;
// remove all points that are not in the mask
fluid.positions.retain(|_| {
let delete = mask[i];
i += 1;
!delete
});
let mut i = 0;
fluid.velocities.retain(|_| {
let delete = mask[i];
i += 1;
!delete
});
let mut i = 0;
fluid.accelerations.retain(|_| {
let delete = mask[i];
i += 1;
!delete
});
let mut i = 0;
fluid.volumes.retain(|_| {
let delete = mask[i];
i += 1;
!delete
});
}
}

Expand Down
18 changes: 1 addition & 17 deletions src/rapier2d-wrapper/src/physics_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ pub struct PhysicsWorld {
pub multibody_joint_set : MultibodyJointSet,
pub ccd_solver : CCDSolver,

pub sleep_linear_threshold: Real,
pub sleep_angular_threshold: Real,
pub sleep_time_until_sleep: Real,
pub solver_prediction_distance : Real,

pub active_body_callback : ActiveBodyCallback,
pub collision_filter_body_callback : CollisionFilterCallback,
pub collision_filter_sensor_callback : CollisionFilterCallback,
Expand Down Expand Up @@ -159,11 +154,6 @@ impl PhysicsWorld {
multibody_joint_set : MultibodyJointSet::new(),
ccd_solver : CCDSolver::new(),

sleep_linear_threshold : settings.sleep_linear_threshold,
sleep_angular_threshold : settings.sleep_angular_threshold,
sleep_time_until_sleep : settings.sleep_time_until_sleep,
solver_prediction_distance :settings.solver_prediction_distance,

active_body_callback : None,
collision_filter_body_callback : None,
collision_filter_sensor_callback : None,
Expand All @@ -187,13 +177,7 @@ impl PhysicsWorld {
let mut integration_parameters = IntegrationParameters::default();

integration_parameters.dt = settings.dt;
integration_parameters.erp = settings.erp;
integration_parameters.max_ccd_substeps = settings.max_ccd_substeps;
integration_parameters.damping_ratio = settings.damping_ratio;
integration_parameters.joint_erp = settings.joint_erp;
integration_parameters.joint_damping_ratio = settings.joint_damping_ratio;
integration_parameters.allowed_linear_error = settings.allowed_linear_error;
integration_parameters.prediction_distance = settings.prediction_distance;
if settings.num_solver_iterations > 0 {
integration_parameters.num_solver_iterations = NonZeroUsize::new(settings.num_solver_iterations).unwrap();
}
Expand Down Expand Up @@ -331,7 +315,7 @@ impl PhysicsWorld {
}
contact_info.pixel_distance = meters_to_pixels(contact_point.dist);
contact_info.pixel_impulse = meters_to_pixels(contact_point.data.impulse);
contact_info.pixel_tangent_impulse = meters_to_pixels(contact_point.data.tangent_impulse);
contact_info.pixel_tangent_impulse = meters_to_pixels(contact_point.data.tangent_impulse.x);

send_contact_points = contact_callback(self.handle, &contact_info, &event_info);
if !send_contact_points {
Expand Down
4 changes: 1 addition & 3 deletions src/rapier2d-wrapper/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,8 @@ pub extern "C" fn shapes_contact(world_handle : Handle, shape_info1 : ShapeInfo,
let margin = pixels_to_meters(pixel_margin);

let mut physics_engine = singleton().lock().unwrap();

let physics_world = physics_engine.get_world(world_handle);

let prediction = Real::max(physics_world.solver_prediction_distance, margin);
let prediction = Real::max(0.002, margin);

let raw_shared_shape1 = physics_engine.get_shape(shape_info1.handle).clone();
let skewed_shape1 = skew_shape(&raw_shared_shape1, shape_info1.skew);
Expand Down
31 changes: 0 additions & 31 deletions src/rapier2d-wrapper/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ use crate::vector::Vector;

#[repr(C)]
pub struct WorldSettings {
pub sleep_linear_threshold: Real,
pub sleep_angular_threshold: Real,
pub sleep_time_until_sleep: Real,
pub solver_prediction_distance : Real,
pub max_ccd_substeps: usize,
pub particle_radius: Real,
pub smoothing_factor: Real,
Expand All @@ -15,10 +11,6 @@ pub struct WorldSettings {
#[no_mangle]
pub extern "C" fn default_world_settings() -> WorldSettings {
WorldSettings {
sleep_linear_threshold : 0.1,
sleep_angular_threshold : 0.1,
sleep_time_until_sleep : 1.0,
solver_prediction_distance : 0.002,
max_ccd_substeps: 1,
particle_radius: 0.1,
smoothing_factor: 2.0,
Expand All @@ -30,29 +22,6 @@ pub struct SimulationSettings {
/// The timestep length (default: `1.0 / 60.0`)
pub dt: Real,

/// 0-1: multiplier for how much of the constraint violation (e.g. contact penetration)
/// will be compensated for during the velocity solve.
/// (default `0.8`).
pub erp: Real,
/// 0-1: the damping ratio used by the springs for Baumgarte constraints stabilization.
/// Lower values make the constraints more compliant (more "springy", allowing more visible penetrations
/// before stabilization).
/// (default `0.25`).
pub damping_ratio: Real,

/// 0-1: multiplier for how much of the joint violation
/// will be compensated for during the velocity solve.
/// (default `1.0`).
pub joint_erp: Real,

/// The fraction of critical damping applied to the joint for constraints regularization.
/// (default `0.25`).
pub joint_damping_ratio: Real,

/// Amount of penetration the engine wont attempt to correct (default: `0.001m`).
pub allowed_linear_error: Real,
/// The maximal distance separating two objects that will generate predictive contacts (default: `0.002`).
pub prediction_distance: Real,
/// The number of solver iterations run by the constraints solver for calculating forces (default: `4`).
pub num_solver_iterations: usize,
/// Number of addition friction resolution iteration run during the last solver sub-step (default: `4`).
Expand Down
30 changes: 0 additions & 30 deletions src/servers/rapier_project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ using namespace godot;

constexpr char RUN_ON_SEPARATE_THREAD[] = "physics/2d/run_on_separate_thread";
constexpr char MAX_THREADS[] = "threading/worker_pool/max_threads";
constexpr char SOLVER_ERP[] = "physics/rapier_2d/solver/erp";
constexpr char SOLVER_DAMPING_RATIO[] = "physics/rapier_2d/solver/damping_ratio";
constexpr char SOLVER_JOINT_ERP[] = "physics/rapier_2d/solver/joint_erp";
constexpr char SOLVER_JOINT_DAMPING_RATIO[] = "physics/rapier_2d/solver/joint_damping_ratio";
constexpr char SOLVER_ALLOWED_LINEAR_ERROR[] = "physics/rapier_2d/solver/allowed_linear_error";
constexpr char SOLVER_PREDICTION_DISTANCE[] = "physics/rapier_2d/solver/prediction_distance";
constexpr char SOLVER_NUM_ITERATIONS[] = "physics/rapier_2d/solver/num_iterations";
constexpr char SOLVER_NUM_ADDITIONAL_FRICTION_ITERATIONS[] = "physics/rapier_2d/solver/num_additional_friction_iterations";
constexpr char SOLVER_NUM_INTERNAL_PGS_ITERATIONS[] = "physics/rapier_2d/solver/num_internal_pgs_iterations";
Expand Down Expand Up @@ -76,12 +70,6 @@ void register_setting_ranged(
}

void RapierProjectSettings::register_settings() {
register_setting_ranged(SOLVER_ERP, 0.6, U"0.00001,1,0.00001,suffix:%");
register_setting_ranged(SOLVER_DAMPING_RATIO, 1.0, U"0.00001,1,0.00001,suffix:%");
register_setting_ranged(SOLVER_JOINT_ERP, 1.0, U"0.00001,1,0.00001,suffix:%");
register_setting_ranged(SOLVER_JOINT_DAMPING_RATIO, 1.0, U"0.0001,1,0.00001,suffix:%");
register_setting_ranged(SOLVER_ALLOWED_LINEAR_ERROR, 0.001, U"0,1,0.00001");
register_setting_ranged(SOLVER_PREDICTION_DISTANCE, 0.002, U"0,1,0.00001");
register_setting_ranged(SOLVER_NUM_INTERNAL_PGS_ITERATIONS, 1, U"1,4,or_greater");
register_setting_ranged(SOLVER_NUM_ADDITIONAL_FRICTION_ITERATIONS, 4, U"1,16,or_greater");
register_setting_ranged(SOLVER_NUM_ITERATIONS, 4, U"1,16,or_greater");
Expand Down Expand Up @@ -111,27 +99,9 @@ bool RapierProjectSettings::should_run_on_separate_thread() {
int RapierProjectSettings::get_max_threads() {
return get_setting<int>(MAX_THREADS);
}
double RapierProjectSettings::get_solver_erp() {
return get_setting<double>(SOLVER_ERP);
}
double RapierProjectSettings::get_solver_damping_ratio() {
return get_setting<double>(SOLVER_DAMPING_RATIO);
}
double RapierProjectSettings::get_solver_joint_erp() {
return get_setting<double>(SOLVER_JOINT_ERP);
}
double RapierProjectSettings::get_solver_joint_damping_ratio() {
return get_setting<double>(SOLVER_JOINT_DAMPING_RATIO);
}
double RapierProjectSettings::get_solver_allowed_linear_error() {
return get_setting<double>(SOLVER_ALLOWED_LINEAR_ERROR);
}
int RapierProjectSettings::get_solver_max_ccd_substeps() {
return get_setting<int>(SOLVER_MAX_CCD_SUBSTEPS);
}
double RapierProjectSettings::get_solver_prediction_distance() {
return get_setting<double>(SOLVER_PREDICTION_DISTANCE);
}
int RapierProjectSettings::get_solver_num_solver_iterations() {
return get_setting<int>(SOLVER_NUM_ITERATIONS);
}
Expand Down
7 changes: 0 additions & 7 deletions src/servers/rapier_project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ class RapierProjectSettings {

static bool should_run_on_separate_thread();
static int get_max_threads();
static double get_solver_erp();
static double get_solver_damping_ratio();
static double get_solver_joint_erp();
static double get_solver_joint_damping_ratio();
static double get_solver_allowed_linear_error();
static double get_solver_max_penetration_correction();
static double get_solver_prediction_distance();
static int get_solver_num_solver_iterations();
static int get_solver_num_additional_friction_iterations();
static int get_solver_num_internal_pgs_iterations();
Expand Down
Loading

0 comments on commit 8e9d56d

Please sign in to comment.