Skip to content

Commit

Permalink
fix point gravity (#44)
Browse files Browse the repository at this point in the history
- Fixes #35
  • Loading branch information
Ughuuu authored Mar 30, 2024
1 parent 18504b8 commit 20f18b3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/bodies/rapier_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,9 @@ void RapierBody2D::update_area_override() {
void RapierBody2D::update_gravity(real_t p_step) {
ERR_FAIL_COND(!using_area_gravity);
ERR_FAIL_COND(!get_space());

if (areas.size() > 0) {
update_area_override();
}
Vector2 gravity_impulse = total_gravity * mass * p_step;

rapier2d::Handle space_handle = get_space()->get_handle();
Expand Down
5 changes: 3 additions & 2 deletions src/rapier2d-wrapper/includes/rapier2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct CollisionEventInfo {
UserData user_data2;
bool is_sensor;
bool is_started;
bool is_stopped;
bool is_removed;
};

Expand Down Expand Up @@ -229,7 +230,7 @@ void body_add_force_at_point(Handle world_handle,
const Vector *pixel_force,
const Vector *pixel_point);

void body_add_torque(Handle world_handle, Handle body_handle, Real torque);
void body_add_torque(Handle world_handle, Handle body_handle, Real pixel_torque);

void body_apply_impulse(Handle world_handle, Handle body_handle, const Vector *pixel_impulse);

Expand All @@ -238,7 +239,7 @@ void body_apply_impulse_at_point(Handle world_handle,
const Vector *pixel_impulse,
const Vector *pixel_point);

void body_apply_torque_impulse(Handle world_handle, Handle body_handle, Real torque_impulse);
void body_apply_torque_impulse(Handle world_handle, Handle body_handle, Real pixel_torque_impulse);

void body_change_mode(Handle world_handle, Handle body_handle, BodyType body_type, bool wakeup);

Expand Down
2 changes: 1 addition & 1 deletion src/rapier2d-wrapper/src/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub extern "C" fn collider_set_transform(world_handle : Handle, handle : Handle,
if let Some(extracted_shape) = scale_shape(shape, &shape_info.scale) {
new_shape = extracted_shape;
} else {
assert!(false);
//assert!(false);
// investigate why it failed
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/rapier2d-wrapper/src/physics_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ impl<'a> PhysicsHooks for PhysicsHooksCollisionFilter<'a> {
let motion_len = body2.linvel().magnitude();
let body_margin1 = pixels_to_meters(one_way_direction.pixel_body1_margin);
let max_allowed = motion_len * Real::max(body2.linvel().normalize().dot(&allowed_local_n1), 0.0) + body_margin1;
contact_is_pass_through = body2.linvel().dot(&allowed_local_n1) <= DEFAULT_EPSILON * 10.0 || dist < -max_allowed;
contact_is_pass_through = body2.linvel().dot(&allowed_local_n1) <= DEFAULT_EPSILON || dist < -max_allowed;
} else if one_way_direction.body2 {
let motion_len = body1.linvel().magnitude();
let body_margin2 = pixels_to_meters(one_way_direction.pixel_body2_margin);
let max_allowed = motion_len * Real::max(body1.linvel().normalize().dot(&allowed_local_n2), 0.0) + body_margin2;
contact_is_pass_through = body1.linvel().dot(&allowed_local_n2) <= DEFAULT_EPSILON * 10.0 || dist < -max_allowed;
contact_is_pass_through = body1.linvel().dot(&allowed_local_n2) <= DEFAULT_EPSILON || dist < -max_allowed;
}
if contact_is_pass_through {
context.solver_contacts.clear();
Expand Down
3 changes: 3 additions & 0 deletions src/rapier2d-wrapper/src/physics_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct CollisionEventInfo {
user_data2: UserData,
is_sensor: bool,
is_started: bool,
is_stopped: bool,
is_removed: bool,
}

Expand All @@ -85,6 +86,7 @@ impl CollisionEventInfo {
is_sensor: false,
is_started: false,
is_removed: false,
is_stopped: false,
}
}
}
Expand Down Expand Up @@ -250,6 +252,7 @@ impl PhysicsWorld {
event_info.is_sensor = collision_event.sensor();
event_info.is_removed = collision_event.removed();
event_info.is_started = collision_event.started();
event_info.is_stopped = collision_event.stopped();
event_info.collider1 = collider_handle_to_handle(handle1);
event_info.collider2 = collider_handle_to_handle(handle2);
event_info.user_data1 = self.get_collider_user_data(handle1);
Expand Down
6 changes: 3 additions & 3 deletions src/spaces/rapier_space_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void RapierSpace2D::collision_event_callback(rapier2d::Handle world_handle, cons
ERR_FAIL_COND(!pArea2);
pArea->on_area_enter(collider_handle2, pArea2, shape2, rid2, instanceId2, collider_handle1, shape1);
pArea2->on_area_enter(collider_handle1, pArea, shape1, rid1, instanceId1, collider_handle2, shape2);
} else {
} else if (event_info->is_stopped) {
if (pArea) {
pArea->on_area_exit(collider_handle2, pArea2, shape2, rid2, instanceId2, collider_handle1, shape1);
} else {
Expand All @@ -269,12 +269,12 @@ void RapierSpace2D::collision_event_callback(rapier2d::Handle world_handle, cons
if (event_info->is_started) {
ERR_FAIL_COND(!pArea);
pArea->on_body_enter(collider_handle2, pBody, shape2, rid2, instanceId2, collider_handle1, shape1);
} else if (pArea) {
} else if (pArea && event_info->is_stopped) {
pArea->on_body_exit(collider_handle2, pBody, shape2, rid2, instanceId2, collider_handle1, shape1);
} else {
// Try to retrieve area if not destroyed yet
pArea = space->get_area_from_rid(rid1);
if (pArea) {
if (pArea && event_info->is_stopped) {
// Use invalid body case to keep counters consistent for already removed collider
pArea->on_body_exit(collider_handle2, nullptr, shape2, rid2, instanceId2, collider_handle1, shape1, false);
}
Expand Down

0 comments on commit 20f18b3

Please sign in to comment.