Add helper function for calculating closest points between GlobalTransform and colliders.

This commit is contained in:
Nolan Darilek 2022-08-22 15:51:07 -05:00
parent 5ba4b5272c
commit 4de359ffdb

View File

@ -471,6 +471,14 @@ impl From<&dyn PointLike> for (i32, i32) {
pub trait GlobalTransformExt {
fn yaw(&self) -> Angle;
fn closest_points(
&self,
collider: &Collider,
other: &GlobalTransform,
other_collider: &Collider,
) -> ClosestPoints;
fn collider_direction_and_distance(
&self,
collider: &Collider,
@ -485,6 +493,25 @@ impl GlobalTransformExt for GlobalTransform {
Angle::Radians(forward.y.atan2(forward.x))
}
fn closest_points(
&self,
collider: &Collider,
other: &GlobalTransform,
other_collider: &Collider,
) -> ClosestPoints {
use bevy::math::Vec3Swizzles;
let scale = PHYSICS_SCALE.read().unwrap();
let pos1 = Isometry::new(
(self.translation() / *scale).xy().into(),
self.yaw().radians(),
);
let pos2 = Isometry::new(
(other.translation() / *scale).xy().into(),
other.yaw().radians(),
);
closest_points(&pos1, &*collider.raw, &pos2, &*other_collider.raw, f32::MAX).unwrap()
}
fn collider_direction_and_distance(
&self,
collider: &Collider,
@ -501,8 +528,7 @@ impl GlobalTransformExt for GlobalTransform {
(other.translation() / *scale).xy().into(),
other.yaw().radians(),
);
let closest =
closest_points(&pos1, &*collider.raw, &pos2, &*other_collider.raw, f32::MAX).unwrap();
let closest = self.closest_points(collider, other, other_collider);
let distance = distance(&pos1, &*collider.raw, &pos2, &*other_collider.raw).unwrap() as u32;
let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" };
if distance > 0 {