Fix spawning nodes within the boing radius of their peers
This commit is contained in:
parent
e96e413c39
commit
0226922ebf
46
src/main.rs
46
src/main.rs
|
@ -719,14 +719,30 @@ impl DelaunayDemo {
|
||||||
fn move_point(&mut self, node: NodeRef) {
|
fn move_point(&mut self, node: NodeRef) {
|
||||||
|
|
||||||
// Update position
|
// Update position
|
||||||
let mut node_inner = node.get();
|
let current_pos = node.get();
|
||||||
node_inner.0 = node_inner.0 + node_inner.1;
|
let mut new_pos = Node(current_pos.0 + current_pos.1, current_pos.1);
|
||||||
|
|
||||||
// Check if it is going to hit another point
|
// Check if it is going to hit another point
|
||||||
for other in &self.nodes {
|
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
|
if
|
||||||
!Arc::ptr_eq(other, &node)
|
!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 node_inner = node.get();
|
||||||
let mut other_inner = other.get();
|
let mut other_inner = other.get();
|
||||||
|
@ -740,21 +756,21 @@ impl DelaunayDemo {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for a bounce
|
// Check for a bounce
|
||||||
if node_inner.0.y > self.height {
|
if new_pos.0.y > self.height {
|
||||||
node_inner.0.y = 2.0 * self.height - node_inner.0.y;
|
new_pos.0.y = 2.0 * self.height - new_pos.0.y;
|
||||||
node_inner.1.y = -node_inner.1.y;
|
new_pos.1.y = -new_pos.1.y;
|
||||||
}
|
}
|
||||||
if node_inner.0.y < 0.0 {
|
if new_pos.0.y < 0.0 {
|
||||||
node_inner.0.y = -node_inner.0.y;
|
new_pos.0.y = -new_pos.0.y;
|
||||||
node_inner.1.y = -node_inner.1.y;
|
new_pos.1.y = -new_pos.1.y;
|
||||||
}
|
}
|
||||||
if node_inner.0.x > self.width {
|
if new_pos.0.x > self.width {
|
||||||
node_inner.0.x = 2.0 * self.width - node_inner.0.x;
|
new_pos.0.x = 2.0 * self.width - new_pos.0.x;
|
||||||
node_inner.1.x = -node_inner.1.x;
|
new_pos.1.x = -new_pos.1.x;
|
||||||
}
|
}
|
||||||
if node_inner.0.x < 0.0 {
|
if new_pos.0.x < 0.0 {
|
||||||
node_inner.0.x = -node_inner.0.x;
|
new_pos.0.x = -new_pos.0.x;
|
||||||
node_inner.1.x = -node_inner.1.x;
|
new_pos.1.x = -new_pos.1.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
node.set(node_inner);
|
node.set(node_inner);
|
||||||
|
|
Loading…
Reference in New Issue