Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
fix scaling to work. (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu authored Oct 25, 2023
1 parent 4f03a8d commit 7e15a9f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/bodies/rapier_collision_object_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,17 @@ void RapierCollisionObject2D::_update_shape_transform(const Shape &shape) {
rapier2d::Handle space_handle = space->get_handle();

const Vector2 &origin = shape.xform.get_origin();
const Vector2 &scale = shape.xform.get_scale();
rapier2d::Vector position = { origin.x, origin.y };
rapier2d::Vector rapier_scale = { scale.x, scale.y };
real_t angle = shape.xform.get_rotation();

shape.shape->apply_rapier_transform(position, angle);
ERR_FAIL_COND(!rapier2d::is_handle_valid(space_handle));

ERR_FAIL_COND(!rapier2d::is_handle_valid(shape.collider_handle));

rapier2d::collider_set_transform(space_handle, shape.collider_handle, &position, angle);
ERR_FAIL_COND(!rapier2d::is_handle_valid(shape.shape->get_rapier_shape()));
rapier2d::collider_set_transform(space_handle, shape.collider_handle, shape.shape->get_rapier_shape(), &position, angle, &rapier_scale);
}

void RapierCollisionObject2D::_set_space(RapierSpace2D *p_space) {
Expand Down
7 changes: 6 additions & 1 deletion src/rapier2d-wrapper/includes/rapier2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ void collider_set_collision_events_enabled(Handle world_handle, Handle handle, b

void collider_set_contact_force_events_enabled(Handle world_handle, Handle handle, bool enable);

void collider_set_transform(Handle world_handle, Handle handle, const Vector *pos, Real rot);
void collider_set_transform(Handle world_handle,
Handle handle,
Handle shape_handle,
const Vector *pos,
Real rot,
const Vector *scale);

Material default_material();

Expand Down
80 changes: 73 additions & 7 deletions src/rapier2d-wrapper/src/collider.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rapier2d::na::Vector2;
use rapier2d::prelude::*;
use crate::handle::*;
use crate::user_data::*;
Expand Down Expand Up @@ -78,14 +79,79 @@ pub extern "C" fn collider_get_angle(world_handle : Handle, handle : Handle) ->
return collider.unwrap().rotation().angle();
}

fn _scale_shape(shape: &SharedShape, scale: &Vector) -> Option<SharedShape> {
let shape_type = shape.shape_type();
if shape_type == ShapeType::Ball {
let new_shape = shape.as_ball().unwrap().scaled(&Vector2::<Real>::new(scale.x, scale.y), 20).unwrap();
if new_shape.is_left() {
let shape = new_shape.unwrap_left();
return Some(SharedShape::new(shape));
} else {
let shape = new_shape.unwrap_right();
return Some(SharedShape::new(shape));
}
}
else if shape_type == ShapeType::Cuboid {
let new_shape = shape.as_cuboid().unwrap().scaled(&Vector2::<Real>::new(scale.x, scale.y));
return Some(SharedShape::new(new_shape));
}
else if shape_type == ShapeType::HalfSpace {
let new_shape = shape.as_halfspace().unwrap().scaled(&Vector2::<Real>::new(scale.x, scale.y)).unwrap();
return Some(SharedShape::new(new_shape));
}
else if shape_type == ShapeType::Polyline {
let new_shape = shape.as_polyline().unwrap().clone().scaled(&Vector2::<Real>::new(scale.x, scale.y));
return Some(SharedShape::new(new_shape));
}
else if shape_type == ShapeType::ConvexPolygon {
let new_shape = shape.as_convex_polygon().unwrap().clone().scaled(&Vector2::<Real>::new(scale.x, scale.y)).unwrap();
return Some(SharedShape::new(new_shape));
}
else if shape_type == ShapeType::Compound {
let new_shapes = shape.as_compound().unwrap().shapes();
let mut shapes_vec = Vec::<(Isometry<Real>, SharedShape)>::new();
for shape in new_shapes {
let new_shape = _scale_shape(&shape.1, scale);
if let Some(shape_extracted) = new_shape {
shapes_vec.push((shape.0, shape_extracted));
}
}
return Some(SharedShape::compound(shapes_vec));
}
return None;
}

#[no_mangle]
pub extern "C" fn collider_set_transform(world_handle : Handle, handle : Handle, pos : &Vector, rot : Real) {
let mut physics_engine = SINGLETON.lock().unwrap();
let physics_world = physics_engine.get_world(world_handle);
let collider_handle = handle_to_collider_handle(handle);
let collider = physics_world.collider_set.get_mut(collider_handle);
assert!(collider.is_some());
collider.unwrap().set_position_wrt_parent(Isometry::new(vector![pos.x, pos.y], rot));
pub extern "C" fn collider_set_transform(world_handle : Handle, handle : Handle, shape_handle : Handle, pos : &Vector, rot : Real, scale: &Vector) {
{
let mut physics_engine = SINGLETON.lock().unwrap();
let physics_world = physics_engine.get_world(world_handle);
let collider_handle = handle_to_collider_handle(handle);
let collider = physics_world.collider_set.get_mut(collider_handle);
assert!(collider.is_some());
let collider = collider.unwrap();
collider.set_position_wrt_parent(Isometry::new(vector![pos.x, pos.y], rot));
}
{
let new_shape:SharedShape;
{
let mut physics_engine = SINGLETON.lock().unwrap();
let shape = physics_engine.get_shape(shape_handle);
if let Some(extracted_shape) = _scale_shape(shape, scale) {
new_shape = extracted_shape;
} else {
return;
}
}
{
let mut physics_engine = SINGLETON.lock().unwrap();
let physics_world = physics_engine.get_world(world_handle);
let collider_handle = handle_to_collider_handle(handle);
let collider = physics_world.collider_set.get_mut(collider_handle);
assert!(collider.is_some());
collider.unwrap().set_shape(new_shape);
}
}
}

#[no_mangle]
Expand Down

0 comments on commit 7e15a9f

Please sign in to comment.