Collide Force¶
- detroit.force_collide(radius: Callable[[SimulationNode, int, list[SimulationNode]], float] | float | None = None) ForceCollide¶
The collide force treats nodes as circles with a given radius, rather than points, and prevents nodes from overlapping. More formally, two nodes a and b are separated so that the distance between
aandbis at leastradius(a) + radius(b). To reduce jitter, this is by default a “soft” constraint with a configurable strength and iteration count.- Parameters:
radius (SimulationNodeFunction[float] | float | None) –
Radius function or constant value. If it is a function, it takes the following arguments:
node (
SimulationNode) - the node elementi (
int) - the index of the nodenodes (
list[SimulationNode]) - the list of nodes
It returns a radius value (
float). Ifradiusis not specified, the default value is a constant function which returns1.- Returns:
Force object
- Return type:
- class detroit.force.collide.ForceCollide(radius: Callable[[SimulationNode, int, list[SimulationNode]], float])¶
- initialize(nodes: list[SimulationNode], random: Callable[[None], float])¶
- set_iterations(iterations: int) ForceCollide¶
Sets the number of iterations per application to the specified number and returns this force.
Increasing the number of iterations greatly increases the rigidity of the constraint and avoids partial overlap of nodes, but also increases the runtime cost to evaluate the force.
- Parameters:
iterations (int) – Iterations value
- Returns:
Itself
- Return type:
- set_stength(strength: float) ForceCollide¶
Sets the force strength to the specified number in the range \([0,1]\) and returns this force.
Overlapping nodes are resolved through iterative relaxation. For each node, the other nodes that are anticipated to overlap at the next tick (using the anticipated positions \((x + v_x,y + v_y)\)) are determined; the node’s velocity is then modified to push the node out of each overlapping node. The change in velocity is dampened by the force’s strength such that the resolution of simultaneous overlaps can be blended together to find a stable solution.
- Parameters:
strength (float) – Strength value in the range \([0, 1]\)
- Returns:
Itself
- Return type:
- set_radius(radius: Callable[[SimulationNode, int, list[SimulationNode]], float] | float) ForceCollide¶
Sets the radius accessor to the specified number or function, re-evaluates the radius accessor for each node, and returns this force.
The radius accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the radius of each node is only recomputed when the force is initialized or when this method is called with a new radius, and not on every application of the force.
- Parameters:
radius (SimulationNodeFunction[float] | float) –
Radius function or constant value. If it is a function, it takes the following arguments:
node (
SimulationNode) - the node elementi (
int) - the index of the nodenodes (
list[SimulationNode]) - the list of nodes
It returns a radius value (
float).- Returns:
Itself
- Return type: