Skip to content

Commit

Permalink
fix precision issue (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu authored Apr 23, 2024
1 parent 8e9d56d commit 8d000cb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/rapier2d-wrapper/includes/rapier2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,7 @@ Handle shape_create_halfspace(const Vector *normal, Real pixel_distance);

void shape_destroy(Handle shape_handle);

ContactResult shapes_contact(Handle world_handle,
ShapeInfo shape_info1,
ContactResult shapes_contact(ShapeInfo shape_info1,
ShapeInfo shape_info2,
Real pixel_margin);

Expand Down
5 changes: 1 addition & 4 deletions src/rapier2d-wrapper/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub extern "C" fn body_create(world_handle : Handle, pixel_pos : &Vector, rot :
}
}
// let default values better
let activation = rigid_body.activation_mut();
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 @@ -211,9 +210,7 @@ pub extern "C" fn body_set_can_sleep(world_handle : Handle, body_handle : Handle
assert!(body.is_some());
let body = body.unwrap();

if can_sleep && body.activation().angular_threshold == -1.0 {
let activation = body.activation_mut();
} else if !can_sleep && body.activation().angular_threshold != -1.0 {
if !can_sleep && body.activation().angular_threshold != -1.0 {
let activation = body.activation_mut();
activation.angular_threshold = -1.0;
activation.linear_threshold = -1.0;
Expand Down
13 changes: 8 additions & 5 deletions src/rapier2d-wrapper/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
use rapier2d::prelude::*;
use crate::vector::Vector;

const PIXELS_PER_METER : Real = 1.0;
const PIXELS_PER_METER : f64 = 128.0;
const METERS_PER_PIXEL : f64 = 1.0 / 128.0;

pub fn pixels_to_meters(x : Real) -> Real {
return x;
if x == 0.0 { 0.0 } else { x / PIXELS_PER_METER }
if x == 0.0 { 0.0 } else {
let res = METERS_PER_PIXEL * (x as f64);
return res as Real;
}
}

pub fn vector_pixels_to_meters(v : &Vector) -> Vector {
Vector{x: pixels_to_meters(v.x), y: pixels_to_meters(v.y)}
}

pub fn meters_to_pixels(x : Real) -> Real {
return x;
x * PIXELS_PER_METER
let res = PIXELS_PER_METER * (x as f64);
return res as Real;
}

pub fn vector_meters_to_pixels(v : &Vector) -> Vector {
Expand Down
2 changes: 1 addition & 1 deletion src/rapier2d-wrapper/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ pub extern "C" fn intersect_aabb(world_handle : Handle, pixel_aabb_min : &Vector
}

#[no_mangle]
pub extern "C" fn shapes_contact(world_handle : Handle, shape_info1 : ShapeInfo, shape_info2 : ShapeInfo, pixel_margin: Real) -> ContactResult {
pub extern "C" fn shapes_contact(shape_info1 : ShapeInfo, shape_info2 : ShapeInfo, pixel_margin: Real) -> ContactResult {
let position1 = vector_pixels_to_meters(&shape_info1.pixel_position);
let position2 = vector_pixels_to_meters(&shape_info2.pixel_position);
let margin = pixels_to_meters(pixel_margin);
Expand Down
10 changes: 5 additions & 5 deletions src/servers/rapier_body_utils_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool RapierBodyUtils2D::body_motion_recover(
rapier2d::ShapeInfo col_shape_info =
rapier2d::shape_info_from_body_shape(col_shape->get_rapier_shape(), col_shape_transform);

rapier2d::ContactResult contact = rapier2d::shapes_contact(p_space.get_handle(), body_shape_info, col_shape_info, p_margin);
rapier2d::ContactResult contact = rapier2d::shapes_contact(body_shape_info, col_shape_info, p_margin);

if (!contact.collided) {
continue;
Expand Down Expand Up @@ -187,7 +187,7 @@ void RapierBodyUtils2D::cast_motion(const RapierSpace2D &p_space, RapierBody2D &
rapier2d::ShapeInfo col_shape_info = rapier2d::shape_info_from_body_shape(col_shape->get_rapier_shape(), col_shape_transform);
// stuck logic, check if body collides in place
body_shape_info.pixel_position = { (body_shape_transform.get_origin()).x, (body_shape_transform.get_origin()).y };
rapier2d::ContactResult step_contact = rapier2d::shapes_contact(p_space.get_handle(), body_shape_info, col_shape_info, 0.0);
rapier2d::ContactResult step_contact = rapier2d::shapes_contact(body_shape_info, col_shape_info, 0.0);
if (step_contact.collided && !step_contact.within_margin) {
if (body_shape->allows_one_way_collision() && collision_body->is_shape_set_as_one_way_collision(shape_index)) {
Vector2 direction = col_shape_transform.columns[1].normalized();
Expand All @@ -211,7 +211,7 @@ void RapierBodyUtils2D::cast_motion(const RapierSpace2D &p_space, RapierBody2D &

body_shape_info.pixel_position = rapier2d::Vector{ (body_shape_transform.get_origin() + p_motion * fraction).x,
(body_shape_transform.get_origin() + p_motion * fraction).y };
rapier2d::ContactResult step_contact = rapier2d::shapes_contact(p_space.get_handle(), body_shape_info, col_shape_info, 0.0);
rapier2d::ContactResult step_contact = rapier2d::shapes_contact(body_shape_info, col_shape_info, 0.0);
if (step_contact.collided && !step_contact.within_margin) {
hi = fraction;
if ((k == 0) || (low > 0.0)) { // Did it not collide before?
Expand Down Expand Up @@ -239,7 +239,7 @@ void RapierBodyUtils2D::cast_motion(const RapierSpace2D &p_space, RapierBody2D &
body_shape_info.pixel_position =
rapier2d::Vector{ (body_shape_transform.get_origin() + p_motion * (hi + contact_max_allowed_penetration)).x,
(body_shape_transform.get_origin() + p_motion * (hi + contact_max_allowed_penetration)).y };
rapier2d::ContactResult contact = rapier2d::shapes_contact(p_space.get_handle(), body_shape_info, col_shape_info, 0.0);
rapier2d::ContactResult contact = rapier2d::shapes_contact(body_shape_info, col_shape_info, 0.0);
if (should_skip_collision_one_dir(contact, body_shape, collision_body, shape_index, col_shape_transform, p_margin, p_space.get_last_step(), p_motion)) {
continue;
}
Expand Down Expand Up @@ -313,7 +313,7 @@ bool RapierBodyUtils2D::body_motion_collide(const RapierSpace2D &p_space, Rapier
Transform2D const &col_shape_transform = collision_body->get_transform() * collision_body->get_shape_transform(shape_index);
rapier2d::ShapeInfo col_shape_info = rapier2d::shape_info_from_body_shape(col_shape->get_rapier_shape(), col_shape_transform);

rapier2d::ContactResult contact = rapier2d::shapes_contact(p_space.get_handle(), body_shape_info, col_shape_info, p_margin);
rapier2d::ContactResult contact = rapier2d::shapes_contact(body_shape_info, col_shape_info, p_margin);
if (!contact.collided) {
continue;
}
Expand Down

0 comments on commit 8d000cb

Please sign in to comment.