Fix spawning nodes within the boing radius of their peers

This commit is contained in:
Emi Simpson 2021-11-16 23:05:00 -05:00
parent e96e413c39
commit 0226922ebf
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 31 additions and 15 deletions

View File

@ -719,14 +719,30 @@ impl DelaunayDemo {
fn move_point(&mut self, node: NodeRef) {
// Update position
let mut node_inner = node.get();
node_inner.0 = node_inner.0 + node_inner.1;
let current_pos = node.get();
let mut new_pos = Node(current_pos.0 + current_pos.1, current_pos.1);
// Check if it is going to hit another point
for other in &self.nodes {
// If we are currently inside the boing radius of the node (e.g. because we spawned
// there), then move out.
if
!Arc::ptr_eq(other, &node)
&& node_inner.distance(other.get().0) < BOING_RADIUS
&& current_pos.distance(other.get().0) < BOING_RADIUS
{
let other_pos = other.get();
let direction = current_pos.0 - other_pos.0;
let offset = direction / direction.length() * BOING_RADIUS / 2.;
node.set(Node(current_pos.0 + offset, current_pos.1));
other.set(Node(other_pos.0 - offset, other_pos.1));
return self.move_point(node);
}
// If we are going to walk into the boing radius of another node, boing!
if
!Arc::ptr_eq(other, &node)
&& new_pos.distance(other.get().0) < BOING_RADIUS
{
let mut node_inner = node.get();
let mut other_inner = other.get();
@ -740,21 +756,21 @@ impl DelaunayDemo {
}
// Check for a bounce
if node_inner.0.y > self.height {
node_inner.0.y = 2.0 * self.height - node_inner.0.y;
node_inner.1.y = -node_inner.1.y;
if new_pos.0.y > self.height {
new_pos.0.y = 2.0 * self.height - new_pos.0.y;
new_pos.1.y = -new_pos.1.y;
}
if node_inner.0.y < 0.0 {
node_inner.0.y = -node_inner.0.y;
node_inner.1.y = -node_inner.1.y;
if new_pos.0.y < 0.0 {
new_pos.0.y = -new_pos.0.y;
new_pos.1.y = -new_pos.1.y;
}
if node_inner.0.x > self.width {
node_inner.0.x = 2.0 * self.width - node_inner.0.x;
node_inner.1.x = -node_inner.1.x;
if new_pos.0.x > self.width {
new_pos.0.x = 2.0 * self.width - new_pos.0.x;
new_pos.1.x = -new_pos.1.x;
}
if node_inner.0.x < 0.0 {
node_inner.0.x = -node_inner.0.x;
node_inner.1.x = -node_inner.1.x;
if new_pos.0.x < 0.0 {
new_pos.0.x = -new_pos.0.x;
new_pos.1.x = -new_pos.1.x;
}
node.set(node_inner);