Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Apr 9, 2024
1 parent b587512 commit ec3cf2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions jagua-rs/src/collision_detection/cd_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ impl CDEngine {
T: CollidesWith<AARectangle> + Shape,
PartialQTHaz: CollidesWith<T>,
{
//TODO: not really efficient as currently implemented, but usually not a bottleneck
let mut colliding_entities = vec![];
let mut irrelevant_hazards = irrelevant_hazards.iter().cloned().collect_vec();

Expand Down
48 changes: 28 additions & 20 deletions jagua-rs/src/geometry/primitives/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ impl Edge {
}
}

/// Returns the closest point which lies on the edge to the given point
pub fn closest_point_on_edge(&self, point: &Point) -> Point {
//from https://stackoverflow.com/a/6853926
let Point(x1, y1) = self.start;
let Point(x2, y2) = self.end;
let Point(x, y) = point;

let a = x - x1;
let b = y - y1;
let c = x2 - x1;
let d = y2 - y1;

let dot = a * c + b * d;
let len_sq = c * c + d * d;
let mut param = -1.0;
if len_sq != 0.0 {
param = dot / len_sq;
}
let (xx, yy) = match param {
p if p < 0.0 => (x1, y1), //start is the closest point
p if p > 1.0 => (x2, y2), //end is the closest point
_ => (x1 + param * c, y1 + param * d), //closest point is on the edge
};

Point(xx, yy)
}

pub fn x_min(&self) -> fsize {
fsize::min(self.start.0, self.end.0)
}
Expand Down Expand Up @@ -124,27 +151,8 @@ impl Shape for Edge {

impl DistanceFrom<Point> for Edge {
fn sq_distance(&self, point: &Point) -> fsize {
//from https://stackoverflow.com/a/6853926
let Point(x1, y1) = self.start;
let Point(x2, y2) = self.end;
let Point(x, y) = point;

let a = x - x1;
let b = y - y1;
let c = x2 - x1;
let d = y2 - y1;

let dot = a * c + b * d;
let len_sq = c * c + d * d;
let mut param = -1.0;
if len_sq != 0.0 {
param = dot / len_sq;
}
let (xx, yy) = match param {
p if p < 0.0 => (x1, y1), //start is closest point
p if p > 1.0 => (x2, y2), //end is closest point
_ => (x1 + param * c, y1 + param * d), //closest point is on the edge
};
let Point(xx, yy) = self.closest_point_on_edge(point);

let (dx, dy) = (x - xx, y - yy);
dx.powi(2) + dy.powi(2)
Expand Down

0 comments on commit ec3cf2e

Please sign in to comment.