Skip to content

Commit 65d78b3

Browse files
committed
Fix #1 - Adjust force calculation
1 parent b8dab69 commit 65d78b3

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

Graphoon/Node.lua

+34-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ local current = (...):gsub('%.[^%.]+$', '');
22

33
local 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

88
local FORCE_MAX = 4;
9-
local NODE_SPEED = 8;
9+
local NODE_SPEED = 128;
1010
local 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.
@@ -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;
123147
end
124148

0 commit comments

Comments
 (0)