@@ -2,22 +2,22 @@ local current = (...):gsub('%.[^%.]+$', '');
22
33local Node = {};
44
5- local FORCE_SPRING = - 0.01 ;
6- local FORCE_CHARGE = 100000 ;
5+ local FORCE_SPRING = 0.005 ;
6+ local FORCE_CHARGE = 200 ;
77
88local FORCE_MAX = 4 ;
9- local NODE_SPEED = 8 ;
9+ local NODE_SPEED = 128 ;
1010local DAMPING_FACTOR = 0.95 ;
1111
12- local DEFAULT_MASS = 0.05 ;
12+ local DEFAULT_MASS = 3 ;
1313
1414---
1515-- @param id - A unique id which will be used to reference this node.
1616-- @param x - The x coordinate the Node should be spawned at (optional).
1717-- @param y - The y coordinate the Node should be spawned at (optional).
1818-- @param anchor - Wether the node should be locked in place or not (optional).
1919--
20- function Node .new ( id , x , y , anchor , ... )
20+ function Node .new ( id , x , y , anchor )
2121 local self = {};
2222
2323 local px , py = x or 0 , y or 0 ;
@@ -46,17 +46,37 @@ function Node.new( id, x, y, anchor, ... )
4646 ay = clamp ( - FORCE_MAX , ay + fy , FORCE_MAX );
4747 end
4848
49+ ---
50+ -- Calculates the manhattan distance from the node's coordinates to the
51+ -- target coordinates.
52+ -- @param tx - The target coordinate in x-direction.
53+ -- @param ty - The target coordinate in y-direction.
54+ --
55+ local function getManhattanDistance ( tx , ty )
56+ return px - tx , py - ty ;
57+ end
58+
59+ ---
60+ -- Calculates the actual distance vector between the node's current
61+ -- coordinates and the target coordinates based on the manhattan distance.
62+ -- @param dx - The horizontal distance.
63+ -- @param dy - The vertical distance.
64+ --
65+ local function getRealDistance ( dx , dy )
66+ return math.sqrt ( dx * dx + dy * dy ) + 0.1 ;
67+ end
68+
4969 ---
5070 -- Attract this node to another node.
5171 -- @param node - The node to use for force calculation.
5272 --
5373 function self :attractTo ( node )
54- local dx , dy = px - node :getX (), py - node : getY ( );
55- local distance = math.sqrt ( dx * dx + dy * dy );
74+ local dx , dy = getManhattanDistance ( node :getPosition () );
75+ local distance = getRealDistance ( dx , dy );
5676 dx = dx / distance ;
5777 dy = dy / distance ;
5878
59- local strength = FORCE_SPRING * distance ;
79+ local strength = - 1 * FORCE_SPRING * distance * 0.5 ;
6080 applyForce ( dx * strength , dy * strength );
6181 end
6282
@@ -65,12 +85,12 @@ function Node.new( id, x, y, anchor, ... )
6585 -- @param node - The node to use for force calculation.
6686 --
6787 function self :repelFrom ( node )
68- local dx , dy = px - node :getX (), py - node : getY ( );
69- local distance = math.sqrt ( dx * dx + dy * dy );
88+ local dx , dy = getManhattanDistance ( node :getPosition () );
89+ local distance = getRealDistance ( dx , dy );
7090 dx = dx / distance ;
7191 dy = dy / distance ;
7292
73- local strength = FORCE_CHARGE * ( mass / ( distance * distance ));
93+ local strength = FORCE_CHARGE * (( mass * node : getMass () ) / ( distance * distance ));
7494 applyForce (dx * strength , dy * strength );
7595 end
7696
@@ -119,6 +139,10 @@ function Node.new( id, x, y, anchor, ... )
119139 mass = nmass ;
120140 end
121141
142+ function self :getMass ()
143+ return mass ;
144+ end
145+
122146 return self ;
123147end
124148
0 commit comments