@@ -2,14 +2,14 @@ local current = (...):gsub('%.[^%.]+$', '');
2
2
3
3
local Node = {};
4
4
5
- local FORCE_SPRING = - 0.01 ;
6
- local FORCE_CHARGE = 100000 ;
5
+ local FORCE_SPRING = 0.005 ;
6
+ local FORCE_CHARGE = 200 ;
7
7
8
8
local FORCE_MAX = 4 ;
9
- local NODE_SPEED = 8 ;
9
+ local NODE_SPEED = 128 ;
10
10
local DAMPING_FACTOR = 0.95 ;
11
11
12
- local DEFAULT_MASS = 0.05 ;
12
+ local DEFAULT_MASS = 3 ;
13
13
14
14
---
15
15
-- @param id - A unique id which will be used to reference this node.
@@ -46,17 +46,37 @@ function Node.new( id, x, y, anchor )
46
46
ay = clamp ( - FORCE_MAX , ay + fy , FORCE_MAX );
47
47
end
48
48
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
+
49
69
---
50
70
-- Attract this node to another node.
51
71
-- @param node - The node to use for force calculation.
52
72
--
53
73
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 );
56
76
dx = dx / distance ;
57
77
dy = dy / distance ;
58
78
59
- local strength = FORCE_SPRING * distance ;
79
+ local strength = - 1 * FORCE_SPRING * distance * 0.5 ;
60
80
applyForce ( dx * strength , dy * strength );
61
81
end
62
82
@@ -65,12 +85,12 @@ function Node.new( id, x, y, anchor )
65
85
-- @param node - The node to use for force calculation.
66
86
--
67
87
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 );
70
90
dx = dx / distance ;
71
91
dy = dy / distance ;
72
92
73
- local strength = FORCE_CHARGE * ( mass / ( distance * distance ));
93
+ local strength = FORCE_CHARGE * (( mass * node : getMass () ) / ( distance * distance ));
74
94
applyForce (dx * strength , dy * strength );
75
95
end
76
96
@@ -119,6 +139,10 @@ function Node.new( id, x, y, anchor )
119
139
mass = nmass ;
120
140
end
121
141
142
+ function self :getMass ()
143
+ return mass ;
144
+ end
145
+
122
146
return self ;
123
147
end
124
148
0 commit comments